The batch file code for this folder renaming task:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "Counter=1"
for /F "delims=" %%I in ('dir "D:\Programmi Installati\log_*" /AD /B /ON 2^>nul') do ren "D:\Programmi Installati\%%I" "log_!Counter!" & set /A Counter+=1
endlocal
pause
The command FOR runs with cmd.exe /C
(more precise %ComSpec% /C
) in a separate command process in background the command line:
dir "D:\Programmi Installati\log_*" /AD /B 2>nul
DIR outputs to handle STDOUT of this background command process
- just the names of all directories because of option
/AD
(attribute directory)
- in bare format because of option
/B
without folder path
- matching the wildcard pattern
log_*
- in specified directory
D:\Programmi Installati
- ordered by name because of option
/ON
.
DIR would output an error message to handle STDERR if it can't find any directory entry matching these criteria. This error message is redirected to device NUL to suppress it.
Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul
. 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.
FOR captures all lines output to handle STDOUT of started command process and processes those lines after started cmd.exe
terminated itself. It is very important for this folder renaming task that FOR runs on a list of folder names captured before doing the folder renames as otherwise the directory entries would change while FOR is accessing them. For that reason for /D
can't be used in this case because of for /D
would process the list of log_*
directory entries while this list changes on each successful folder rename. The result would be directories not renamed or renamed multiple times or even an endless running loop depending on file system (NTFS or a FAT file system like FAT32 or ExFAT).
FOR with option /F
ignores empty lines which do not occur here. FOR ignores also lines starting with a semicolon because of end of line option eol=;
is the default. But all lines output by DIR start with log_
and for that reason the default end of line definition can be kept for this task.
FOR with option /F
splits up a line by default to substrings using normal space and horizontal tab as delimiters and assigns just first space/tab separated string to specified loop variable. This line splitting behavior is not wanted here because loop variable I
should hold directory name with all spaces. Therefore delims=
is used to define an empty list of delimiters to disable the line splitting behavior.
The directory name assigned to loop variable I
is referenced with %%I
and full path not output by DIR as source name of command REN. The new folder name is log_
with the counter variable value referenced using delayed environment variable expansion.
The counter variable is incremented by one using a simple arithmetic expression after renaming the directory independent on directory renaming was successful of failed for various reasons.
The command PAUSE at end is added to see the error message output by command REN if renaming a directory failed. There is nothing output except the prompt by PAUSE on all directories could be renamed successfully.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?
echo /?
endlocal /?
for /?
pause /?
ren /?
set /?
setlocal /?
See also single line with multiple commands using Windows batch file for an explanation of operator &
.
PS: I recommend the usage of the shareware file manager Total Commander which has a built-in multi-rename tool for renaming files and folders for people with no coding experience. Download, install and start Total Commander, navigate to the folder containing all these folders, press Ctrl+A to select the folders, press Ctrl+M to open multi-rename tool window and the rest is self-explaining. If you need nevertheless help, press key F1 to open the help page for multi-rename tool.