Here is how to loop files in windows with batch and execute some commands.
This becomes really handy if you want to convert some files, change file extensions, compress files or anything else.
FOR /R “C:\MyDir\” %%G in (*.txt) do echo %%G
/R means traverse through all subfolders.
The first argument is the base folder to start in.
%%G is a variable, where the letter G can be changed according to your needs.
(*.txt) is the file filter.
This could also be expanded to execute multiple commands with the use of ( )
FOR /R “C:\MyDir\” %%G in (*.txt) do (
If you want to chose a parameter that you gave the script, use %1 – %9 to select the parameter number.
%0 is the batch filename.
To access the variables beyong %9, you need to use the shift
Shift is able to shift the parameters to the left.
shifts all to the left, and doesn’t alter %0–%2 but all the following parameters.
%3 will be set to the former value of %4, %4 to %5, %5 to %6… and %9 to the before inaccessible %10.
If you want to alter the variables, you could use one of the following codes (the number “1” needs to be replaced by your variable name… G for example):
Expands %1 and removes any surrounding quotation marks (“”).
Expands %1 to a fully qualified path name.
Expands %1 to a drive letter.
Expands %1 to a path.
Expands %1 to a file name.
Expands %1 to a file extension.
Expanded path contains short names only.
Expands %1 to file attributes.
Expands %1 to date and time of file.
Expands %1 to size of file.
Searches the directories listed in the PATH environment variable and expands %1 to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found, this modifier expands to the empty string.
Expands %1 to a drive letter and path.
Expands %1 to a file name and extension.
Searches the directories listed in the PATH environment variable for %1 and expands to the drive letter and path of the first one found.
Expands %1 to a dir-like output line.