Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hidden features of Windows batch files

People also ask

What can .BAT files do?

A BAT file is a DOS batch file used to execute commands with the Windows Command Prompt (cmd.exe). It contains a series of line commands in plain text that are executed to perform various tasks, such as starting programs or running maintenance utilities within Windows.

What does %% mean in batch script?

%%i is simply the loop variable. This is explained in the documentation for the for command, which you can get by typing for /? at the command prompt.

What does * mean in batch file?

It means "all the parameters in the command line". For example, it's useful when you want to forward the command line from your batch file to another program: REM mybatchfile.cmd echo You called this with arguments: %* echo I will now forward these to the DIR command. dir %* Follow this answer to receive notifications.

What does && do in batch file?

&& runs the second command on the line when the first command comes back successfully (i.e. errorlevel == 0 ). The opposite of && is || , which runs the second command when the first command is unsuccessful (i.e. errorlevel != 0 ).


Line continuation:

call C:\WINDOWS\system32\ntbackup.exe ^
    backup ^
    /V:yes ^
    /R:no ^
    /RS:no ^
    /HC:off ^
    /M normal ^
    /L:s ^
    @daily.bks ^
    /F daily.bkf

PUSHD path

Takes you to the directory specified by path.

POPD

Takes you back to the directory you "pushed" from.


Not sure how useful this would be in a batch file, but it's a very convenient command to use in the command prompt:

C:\some_directory> start .

This will open up Windows Explorer in the "some_directory" folder.

I have found this a great time-saver.


I have always found it difficult to read comments that are marked by a keyword on each line:

REM blah blah blah

Easier to read:

:: blah blah blah