How to Execute For Command-line on Multiple Files
With this mini tutorial i would give examples of how For command can be executed for multiple files. For command-line conditionally perform a command several times on several files or individual file.
The %UserProfile% is a environment variable which holds a value of a directory path of the user profile (C:\Users\{username} or "Documents and Settings"). You can replace the %UserProfile% variable with any directory path that you want to execute For command and copy all movie files to X:\Movies directory. Above For command-line will copy all movie files with .mov, .avi, .wmv, .mpg, .mpeg, .mp4, .flv extensions. The asterisk symbol (*) is known as place holder for filenames or folders.
When you execute the above "For" command-line right from the Command Prompt by copying it and pasting it will work, but when you create a batch file using this code, won’t work, because when you are using it in a batch file, you are supposed to use %% preceding the variable name, in this case the following code will work if you try to execute as a batch.
Below For command-line finds all *.mp2 files in current directory changes its .mp2 extension to .mpeg extension. The "%~na.mpeg" command expands %a variable to a file name only and appends .mpeg extension.
Below For command-line renames all .chm files store at "C:\Windows\Help\mui\041F" directory, the %nxa.tr expands only file name and extension from %a variable and adds ".tr" as a file extension.
Below For command-line use for combining all text files in same directory into one text file. The directory of the Command Prompt run forum is where For command-line is executed for all text files in that directory. You can use CD command to change Command Prompt directory path (CD C:\Users).
Code:
For /r %UserProfile% %c in (*.mov *.avi *.wmv *.mpg *.mpeg *.mp4 *.flv) do Copy %c X:\Movies
The %UserProfile% is a environment variable which holds a value of a directory path of the user profile (C:\Users\{username} or "Documents and Settings"). You can replace the %UserProfile% variable with any directory path that you want to execute For command and copy all movie files to X:\Movies directory. Above For command-line will copy all movie files with .mov, .avi, .wmv, .mpg, .mpeg, .mp4, .flv extensions. The asterisk symbol (*) is known as place holder for filenames or folders.
When you execute the above "For" command-line right from the Command Prompt by copying it and pasting it will work, but when you create a batch file using this code, won’t work, because when you are using it in a batch file, you are supposed to use %% preceding the variable name, in this case the following code will work if you try to execute as a batch.
Code:
For /r %UserProfile% %%c in (*.mov *.avi *.wmv *.mpg *.mpeg *.mp4 *.flv) do Copy %%c X:\Movies
Below For command-line finds all *.mp2 files in current directory changes its .mp2 extension to .mpeg extension. The "%~na.mpeg" command expands %a variable to a file name only and appends .mpeg extension.
Code:
For /r %a in (*.mp2) do Ren "%a" "%~na.mpeg"
Below For command-line renames all .chm files store at "C:\Windows\Help\mui\041F" directory, the %nxa.tr expands only file name and extension from %a variable and adds ".tr" as a file extension.
Code:
For /r C:\Windows\Help\mui\041F %a in (*.chm) do Ren %a %~nxa.tr
Below For command-line use for combining all text files in same directory into one text file. The directory of the Command Prompt run forum is where For command-line is executed for all text files in that directory. You can use CD command to change Command Prompt directory path (CD C:\Users).
Code:
For %a in (*.txt) do type %a > Combined.txt