Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between % and %% in a cmd file?

Tags:

batch-file

cmd

I recently included a line similar to this in a .cmd file:

for /f %%f in ('dir /b .\directory\*.sql') DO sqlcmd -b -o ".\directory\output\%%f.txt" -i ".\directory\%%f"

Originally I had only used %f, and it would work fine when run on the command line, but not when run through the file. When I switched to %%f, it worked in the file. Just wondering what the difference is.

like image 419
Tyler Avatar asked Jan 24 '13 19:01

Tyler


People also ask

What does %% mean in CMD?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What does %% mean in a batch file?

In your scenario, the %%A is a placeholder for what the "for" loop is iterating over (which the /D indicates directories). So each iteration of the loop, %%A is one of the directories. You'll see %% instead of % in batch code. You'll see % instead of %% used in your command prompt.

What does %1 mean in a batch file?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.

Why do we use @echo off?

The ECHO-ON and ECHO-OFF commands are used to enable and disable the echoing, or displaying on the screen, of characters entered at the keyboard. If echoing is disabled, input will not appear on the terminal screen as it is typed. By default, echoing is enabled.

What is the difference between CMD and bat?

It is comparable to a .BAT file, which is additionally in many instances used to keep a batch of executable commands.CMD is the command immediate in the gadget and the programming language is referred to as DOS batch language. This file type was produced by means of Microsoft to be used for the implementation of Windows NT command scripts. 2. BAT :

What is the format of CMD file?

CMD scripts are written in the command prompt. Any type of script is run in command prompt and the extension is .cmd file. Microsoft’s batch processing uses BAT files and is stored in .bat format. Commands and utilities along with the programming language make CMD unique.

What is the use of CMD file in Windows?

CMD files are newer version came into existence for batch files. The older systems will not know about CMD files and will run them partially which will throw an error for the batch files saved as CMD. CMD is the command prompt in the system and the programming language is called DOS batch language. Nowadays CMD is used by many.

What do the percent signs mean in batch files?

(Explanation in more details can be found in an archived Microsoft KB article.) Three things to know: The percent sign is used in batch files to represent command line parameters: %1, %2,... Two percent signs with any characters in between them are interpreted as a variable:


2 Answers

(Explanation in more details can be found in an archived Microsoft KB article.)

Three things to know:

  1. The percent sign is used in batch files to represent command line parameters: %1, %2, ...
  2. Two percent signs with any characters in between them are interpreted as a variable:

    echo %myvar%

  3. Two percent signs without anything in between (in a batch file) are treated like a single percent sign in a command (not a batch file): %%f

Why's that?

For example, if we execute your (simplified) command line

FOR /f %f in ('dir /b .') DO somecommand %f

in a batch file, rule 2 would try to interpret

%f in ('dir /b .') DO somecommand %

as a variable. In order to prevent that, you have to apply rule 3 and escape the % with an second %:

FOR /f %%f in ('dir /b .') DO somecommand %%f
like image 150
marapet Avatar answered Oct 17 '22 23:10

marapet


In DOS you couldn't use environment variables on the command line, only in batch files, where they used the % sign as a delimiter. If you wanted a literal % sign in a batch file, e.g. in an echo statement, you needed to double it.

This carried over to Windows NT which allowed environment variables on the command line, however for backwards compatibility you still need to double your % signs in a .cmd file.

like image 18
Neil Avatar answered Oct 17 '22 22:10

Neil