So I have a huge directory structure full of shape files and I wanted to get them all into my SQL Server 2014 instance (I'm sure many earlier versions of SQL Server will work just as well with this procedure). I got tired of all the software I found to do this for one reason or another. Anyhow, I created a .bat file with the following contents:
for /R %%f in (*.shp) do C:\OSGeo4W64\bin\ogr2ogr -f "MSSQLSPATIAL" "MSSQL:server=localhost;database=MyGISDB;trusted_connection=yes" -skipfailures "%%f"
Note: -skipfailures is optional here. I also had to get ogr2ogr by installing OSGeo4W, but it may not be necessary to install all of this just to get this one executable.
Then I put this batch file in the root of the folder that contains all of my shape files. Started this .bat file in a command prompt (see another of my posts for an easy way to switch directories with the cmd window). Remember to change your working directory to the folder that contains your shape files and batch file. I typed in cmd /C MyBatchFile.bat and hit enter. The /C command causes the cmd to continue executing even if the batch file hits an error while running. You can use the /k command if you would like the cmd window to remain open when all is complete. However, for some reason I was not able to use both the /C and /k at the same time. I'm sure this is not a problem for a command line guru, which I clearly am not. Feel free to comment if you know how to use both at the same time. Hopefully this helps someone save some time.
No comments:
Post a Comment