

An error message output in case of current directory does not contain any non-hidden file is suppressed by redirecting it from handle STDERR to device NUL with 2>nul. If "!FileName:(%Year%)=!" = "!FileName!" ren "%~1" "!NewName!%~x1"įOR executes the following command line with using a separate command process started in background with cmd.exe /C: dir /A-D-H /B 2>nul | C:\Windows\System32\findstr.exe /R /C:"19" /C:"20"ĭIR outputs with the used options all names of non-hidden files in current directory with just file name + extension and without file path.

Setlocal EnableExtensions DisableDelayedExpansionįor /F "eol=| delims=" %%I in ('dir /A-D-H /B 2^>nul ^| %SystemRoot%\System32\findstr.exe /R /C:"19" /C:"20"') do call :RenameFile "%%I" Well, in real I use Total Commander for nearly all file management tasks.īut it was interesting to develop the code for this very special file renaming task with all the limitations Windows command processor has because of not being designed for such tasks. I use since more than 20 years Total Commander (shareware) for file/folder renaming tasks which makes it possible with its built-in multi-rename tool to easily rename files and folders with just a few clicks on which the results can be viewed before really running the multi-rename and which even supports undo after having done the multi-rename. So I can output a list of matching file names, but from there I do not know how to target the grouped characters that findstr matched in order to parse the full file name into the 3 chunks I believe I would need: the substring preceding the matched group, the group itself, and the substring following the group. So far, I have only been able to match the file names containing a year string with the following code: off Notice that if it's already wrapped in parentheses, I'd prefer not to double them up. In a windows batch file, I would like to rename files containing a 4-digit year (ex: "1999") in the filename by simply wrapping the year string in parentheses.
