Batch File Cheat Sheet



Batch Files File Input @echo off echo Bill file echo Gate file set /P fileco ntent =file echo First Line:%filec ontent% set /P var=file FOR /f%%f in (file) Do (echo%%f) REM print each line in file Variables setlocal set MYENV=abc endlocal Only active in the current batch file (local) set MYENV=abc creates or changes an enviro. Batch Files File Input @echo off echo Bill file echo Gate file set /P fileco ntent =file echo First Line:%filec ontent% set /P var=file FOR /f%%f in (file) Do (echo%%f) REM print each line in file Variables setlocal set MYENV=abc endlocal Only active in the current batch file (local) set MYENV=abc creates or changes an enviro. FastPictureViewer Professional 2.0 Cheat Sheet. Ctrl+C / Shift+Ctrl+C Copy image files / Copy image pixels D Toggle XMP metadata window. Ctrl+F Batch File Processor (File Utilities) Ctrl+I EXIF Statistics Information Ctrl+L Geo-Clustering Processor.

Recently, I had some tasks at work that required me to write a lot of batch files.
Here are some things about batch files that I either learned or re-discovered in the process:

Batch

Don’t use blanks when assigning values to variables

This works:

Mac app store hack. This does not work(no error message - %foo% is just empty afterwards):

Bat

This one took me some time to figure out. I’m used to Visual Studio and the Access VBA editor, which both automatically insert these blanks into my code if I don’t do it myself. So when I wrote batch files with a simple text editor, I inserted these blanks too, without thinking.

Execute .exe and save the output to a variable

Windows Batch File Cheat Sheet

This line saves the output of foo.exe to a variable named %bar%:

(Credits for this go to user “lesmana” at Stack Overflow)

Disclaimer: I don’t try to understand it, I just copy/paste it every time I need it.
(The whole usage of the for command looks equally complicated - to see it in all its beauty, type for /? and read the help.)

Get the path and file name of the currently running batch file

There are other commands to get more file information, but to me the most important ones are these:

  • %~dp0 outputs the path of the current batch file
  • %~nx0 outputs the name of the current batch file

To see it in action, put this into a batch file:

When the batch file is c:test.bat, it will output this:

Batch Script Cheat Sheet

The number zero at the end of %~dp0 and %~nx0 refers to %0, which is the currently executing batch file.
Likewise, if you pass a file as parameter %1, you can get the name of that file by using %~nx1 instead of %~nx0.

%~dp0 and %~nx0 are actually combined commands (you can also use %~n0 to get just the file name without extension, and %~x0 to get just the extension)
To see the complete list of commands, open the command line and type in call /?.

Temporarily add a folder to the %PATH% variable

When you need to refer files from a certain folder in a batch file several times and you don’t want to copy/paste the complete path every time, you can just add the folder to the %PATH% variable.
When you don’t want to (or aren’t allowed to) do this permanently on the machine, you can set the %PATH% variable in the batch file, which lasts only for the execution time of the batch file.

The following line appendsc:foo to the existing content of the %PATH% variable:

Batch File Cheat Sheet Excel

Afterwards, the %PATH% variable contains everything it contained before plusc:foo, which is probably what you want.

Batch File Cheat Sheet 2020

A second (but inferior) way would be:

This is probably not what you want, because it replaces the whole content of the %PATH% variable by c:foo !
In other words, %PATH% will only contain c:foo afterwards.

Batch File Cheat Sheet

Beware: you won’t notice the difference as long as you only need files from c:foo.
But as soon as you need some other command-line tool like git or hg(or some Windows helper like regsvr32.exe), your batch file won’t find them anymore because you just deleted their locations from the %PATH%.

Variables are global in nested batch file calls

Take a look at these two batch files:

batch1.bat:

batch2.bat:

The first batch file calls the second one, so variables with the same name are shared between the two.

This means that the last line in the first batch file (echo %var%) will output bar and not foo !