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.
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.
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.
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.
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.
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 :
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.
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.
(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:
(Explanation in more details can be found in an archived Microsoft KB article.)
Three things to know:
%1
, %2
, ...Two percent signs with any characters in between them are interpreted as a variable:
echo %myvar%
%%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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With