Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Shell 

Execute For Command-line on Multiple Files

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.


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
You could have also just done this:
Code
FOR %%G IN (mov avi wmv mpg mpeg mp4 flv) DO (
    ECHO *.%%G
)

Much more flexibility here as you can control where you look for each specific filetype.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by john ronaldsadJul 20, 2022 06:49 PM 4 238 Jul 20, 2022 06:49 PM
Last Post: john ronaldsad
[Python] by MiguelSanchezFeb 1, 2022 08:17 AM 0 161 Feb 1, 2022 08:17 AM
Last Post: MiguelSanchez
[Python] by MiguelSanchezJan 22, 2022 07:01 AM 0 149 Jan 22, 2022 07:01 AM
Last Post: MiguelSanchez