For that reason for can't be used directly in this case because of for would process the list of * * directory entries while this list changes on each successful file rename. It is very important for this file renaming task that FOR runs on a list of file names captured before doing the file renames as otherwise the directory entries would change while FOR is accessing them. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.įOR captures all lines output to handle STDOUT of started command process and processes those lines after started cmd.exe terminated itself. Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. This error message is redirected to device NUL to suppress it. in current directory which can but must not be the directory of the batch file.ĭIR would output an error message to handle STDERR if it can't find any directory entry matching these criteria.matching the wildcard pattern * * which matches any file name with at least one space inside.in bare format because of option /B without file path.just the names of all non-hidden files because of option /A-D-H (attribute not directory and not hidden).The command FOR runs with cmd.exe /C (more precise %ComSpec% /C) in a separate command process in background the command line: dir "* *" /A-D-H /B 2>nulĭIR outputs to handle STDOUT of this background command process I suggest following batch code for this task: offįor /F "delims=" %%A in ('dir "* *" /A-D-H /B 2^>nul') do for /F "tokens=1*" %%B in ("%%A") do ren "%%A" "%%C" We rename files now: %%~fA is the full path where filename currently processed is and !filename:%%B =! means to take filename environment variable, search for string "%%B " (first part of filename and a space) and replace it with an empty string actually nothing!.We don't really need to specify it here, but it is good to make it clearer. We need to access the first token to substract it later. Now we make another for loop to parse a string ( /F): the filename ( %%A).We could use the variable name of the loop ( %%A), but variables have an advantage: %var:search=replace%, or even !var:search=replace! which we need here. We need to set a variable here with the filename.Now we make a for loop to parse the output ( /F) of the dir /b /A-D command which lists all items in current working directory ( %cd%), excluding directories ( /A-D).You must use delayed expansion always inside these code blocks. We use it only here, as we have to access variables inside a for loop which is a code block. setlocal EnableDelayedExpansion enables delayed expansion.Which is more accurate and renames all files correctly only if they have the format mentioned in the beginning. I suggest the following code for the task: off It is unsecure, because if the filename contains spaces, the file will be rename incorrectly. This is the shortest way, but not the most accurate one. If the format of the filename would be the same for all the files in the folder, then you can try: offįor /F "delims=" %%A IN ('dir /b /A-D') do (
0 Comments
Leave a Reply. |