Early in my IT development, when Windows was exciting and new, a situation arose where there was some work required with an IT department that needed super MS Excel formula skills (that’s a another story!). Every day the IT manager would open his beige computer and the second he log on the command shell would open and all his programs, emails and the like started, it was amazing… I begged to learn Magic.
It turns out that there is a little tool built into Windows, until the latest version of Windows 11. It reminds of DOS (the command line system before we have a GUI (Graphical User Interface). Batch scripts let you bundle all of these command line commands into a text file that you can then run.
Firstly, I’m not a coder, nor a command line fan, but I’m a big fan of batch scripting because it’s simple; it actually reminds me a bit of Python but uses the commands you use every day in the windows command shell. If you want to make it really powerful (and you probably will anyway), you can put your other scripting tools in the PATH command shell. It probably sounds a bit confusing if you’re not a command line fan, so in layman’s terms, adding something to the PATH system means it’s accessible through the command shell. You might have used OGR2OGR, Python, or PostGIS before and couldn’t use the Windows command shell to run things. Making sure it can run them in the command shell, do the following (example for Python):
Add the directory of the executable file to the PATH variable. Often times this needs to be done while installing Python.
The full path of python.exe can be entered by:
- Right click on This PC and go to Properties.
- By clicking on Advanced System Settings in the left menu.
- By clicking on the Environment Variables button at the bottom right.
- In the System Variables section, select the Path variable and click Edit. The next screen will display all of the directories that are currently part of the PATH variable.
- By clicking on New and entering the Python installation directory.
Once Python is in your environment, when you open the Windows command shell, you can run type your python scripts inside, or better yet, link them with batch scripts!
ALSO READ: How MIS Improves Modern Field Service Operations
Downloading multiple GIS data
Let’s download a file from Natural Earth using a batch file. Create a new folder, navigate to it, then open a text editor (Notepad ++ is best, but Simple Notepad will do)
curl “http://naciscdn.org/naturalearth/packages/natural_earth_vector.zip” -sortie naturalearth.zip
Then save the text file as “naturalearth.bat”. Double-click on the naturalearth.bat file and you’re done! There should be a new zip file in the folder. By modifying the web link in the voice tags, we can download new data and then name it after the text of “exit”.
Using the same curl line, we can download a lot of data with just one double mouse click.
Batch Data Download Using OGR2OGR
If you are using Notepad ++ then uploading multiple files or converting formats using OGR2OGR is a dream, you can use Notepad ++ to edit and copy / replace, then save the command list as a batch file you will keep the list another day whenever you want. run it again.
If you are running QGIS it is likely that you have already installed OGR2OGR and you may just need to add it to your system path. If you find that you haven’t installed it, you can still download it through FWTools.
Here we will convert 4 GeoJSON data into shapefiles. As before, open your text editor and type:
cd c / gis /
ogr2ogr -f “ESRI Shapefile” destination_data1.shp “source-data1.json”
ogr2ogr -f “ESRI Shapefile” destination_data2.shp “source-data3.json”
ogr2ogr -f “ESRI Shapefile” destination_data3.shp “source-data3.json”
ogr2ogr -f “ESRI Shapefile” destination_data4.shp “source-data4.json”
Save it as a file with the .bat suffix
cd c / gis / – this sets the file folder where the data you want to convert resides
-f “ESRI shapefile” defines the format to which you want to convert the data. If you used -f “MapInfo file”, the data would be converted to MapInfo
destination_data1.shp – this is the name of the shapefile you want. You can also add the location of the file (for example c / gis / shapefiles / shapefile1.shp)
“source-data1.json” – this is the name of the GeoJSON data, if the data was GeoPackage you would put the name and suffix here to convert it to a shapefile.
For a while when I started using GDAL and OGR2OGR I would enter each command manually into the command shell and wait for it to respond, it could take hours with a large list of data, just putting it together in a batch file I quickly found out that I could just trigger it and let it run in the background.
There are several other ways to use OGR2OGR to merge data, change the coordinate system, and even clip data. One of my favorites is using the -lco command to add SQL commands to the upload to add GIST indexes and vacuum parse when uploading PostGIS data.
Learn more about OGR2OGR and GDAL here.
Start the GIS (or other things) when the PC starts up
Every day you boot up your working machine and open ArcGIS Pro, QGIS, email and everything in between, which takes a good 30 minutes every morning. What if there was a way to automate the opening of all this software?
Again, the batch file to the rescue! Here is a script that will open QGIS, ArcGIS Pro, and Outlook 2016:
REM This opens qgis from the osgeo4w folder
cd “C: OSGeo4W bin “
REM This opens Arcgis Pro
cd “C: Program Files ArcGIS Pro bin “
REM This opens Outlook 2016
cd “C: Program Files Microsoft Office root Office16 “
REM Open an existing project or model (QGIS model example)
cd “C: GIS “
Green statements that start with “REM” are comments, as long as you put the text “REM” at the beginning, the batch file will not use it and will skip to the next line.
You can see from the above that it is relatively simple to add the location of the program or file and there is no worry in worrying about too many spaces or making sure that you have commas at the end of each line.
You can just double click on it every time you start your machine, but adding it to your startup is really simple and from there you can choose whether to use it or not.
Move towards AppData Roaming Microsoft Windows Start Menu Programs Startup folder, then drag and drop the batch file into that folder.
Now when you go to your startup apps (type this text into your startup search) you should see that the batch file is one of your startup programs
You can see the batch file is enabled at the bottom. Don’t worry if your screen doesn’t look the same. This was created under Windows 11 but also tested under Windows 10 and 11. Now, when you restart your machine, the software of your choice will open each time!
This is just a taste of what is possible with batch files. Once you get more comfortable with how it all works, there are a lot of things that you can do. If you plan to mix up some of the things discussed earlier in the article, where we open the software, upload data, and even use OGR2OGR to convert the data, imagine combining them to create one-click end-to-end solutions for some work. . Personally, I have used it to start my GIS in the morning with the business model and start all my other software as well as to automate large data lists, it saved me so much time that I am glad that there are these media to share it with you.