michalss Posted September 10 Share Posted September 10 Just a quick tutorial for who wants to run any command-line tool in batch mode on all the files of a folder instead of running it manually for every single file. So, for example, if you have 1000 fsb or xwb archives and you want to run fsbext and unxwb on them with a single command, that's the trick. Create a file called file.bat in the target folder containing the following syntax: for /r %%G in ("*") do COMMAND %%G Where COMMAND must be replaced with the full command-line tool and argument you want to run, for example Where COMMAND must be replaced with the full command-line tool and argument you want to run, for example As you can see I replaced also the * (for ANY file) with *.xwb for running the tool only on the files with XWB extension. It's even possible to run multiple commands in sequence by appending &&: for /r %%G in ("*.xwb") do md %%G_output && unxwb -d %%G_output %%G You can also use this method from the cmd.exe console, you have only to replace %%G with %G. Please note that the G in %%G can be anything, so if you prefer %%a you can use it too. This topic is for anyone who wants to add other methods, tips, suggestions, examples and questions. It's something that is often asked by users so it's very important to have a topic about it. https://ss64.com/nt/for_r.html FOR /R Loop through files (Recurse subfolders) Syntax FOR /R [[drive:]path] %%parameter IN (set) DO command Key drive:path : The folder tree where the files are located. set : A set of one or more files enclosed in parentheses (file1.*, another?.log). Wildcards must be used. If (set) is a period character (.) then FOR will loop through every folder. command : The command to carry out, including any parameters. This can be a single command, or if you enclose it in (parentheses), several commands, one per line. %%parameter : A replaceable parameter: in a batch file use %%G (on the command line %G) This command walks down the folder tree starting at [drive:]path, and executes the DO statement against each matching file. If the [drive:]path are not specified they will default to the current drive:path. FOR parameters (%%A – %%Z) Read the main FOR introduction page for a full description of assigning the replaceable %%parameter. FOR parameters are used in all variations of the FOR command, it is a good idea to get up to speed with writing a basic FOR command before trying the more complex FOR / R variant. Unlike some other variants of the FOR command, with FOR /R you must include a wildcard (either * or ?) in the 'set' to get consistent results returned. In many cases you can work around this by adding a single character wildcard e.g. if you are looping through multiple folders to find the exact filename myfile.txt you could instead specify myfile.t?t FOR does not, by itself, set or clear the Errorlevel. FOR is an internal command. Examples List every .bak file in every subfolder starting at C:\temp\ For /R C:\temp\ %%G IN (*.bak) do Echo "%%G" A batch file to rename all .LOG files to .TXT in the 'demo' folder and all sub-folders: For /R C:\demo\ %%G in (*.LOG) do Echo REN "%%G" "%%~nG.TXT" Alternatively the same thing using the current directory: CD C:\demo\ For /R %%G in (*.LOG) do Echo REN "%%G" "%%~nG.TXT" (Remove the Echo from these commands to run them for real.) Change directory to each subfolder under C:\Work in turn: FOR /R "C:\Work\" %%G in (.) DO ( Pushd %%G Echo now in %%G Popd ) Echo "back home" “Just think how happy you would be if you lost everything you have right now, and then got it back again” ~ Frances Rodman Related commands: FOR - Loop commands. FOR - Loop through a set of files in one folder. FOR /D - Loop through several folders. FOR /L - Loop through a range of numbers. FOR /F - Loop through items in a text file. FOR /F - Loop through the output of a command. FORFILES - Batch process multiple files. IF - Conditionally perform a command. Equivalent PowerShell: ForEach-Object - Loop for each object in the pipeline. Equivalent bash command (Linux): for - Expand words, and execute commands. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now