This is a Windows 10 batch file question. I need to rename a series of files based on their names, but I only know part of the name for each file. With guidance from various pages on this site, I've tried the following steps. I tired the commented FOR but they didn't work. The REN command worked partially, but didn't preserve the entire original filename. I hope the following code will help you understand what I'm trying to do.
::Append 00 as a prefix to the name of whatever filename contains a specific string
::
setlocal
::set fname=
set fname=ADPMasterControl.pdf
::SITE EXAMPLE: for %a in (prefix*.txt) do @(set "fname=%a" & call ren "%fname%" "%fname:*prefix=%")
::for %a in (*ADPMasterControl.pdf) do @(set fname=%a & call ren %fname% "%fname:*prefix=%")
::for %a in (*ADPMasterControl.pdf) do @(set fname=%a & call ren %fname% "00 %fname%")
::for %a in (*ADPMasterControl.pdf) do (set fname=%a & call ren %fname% "00 %fname%")
ren *%fname% "00 *%fname%" <----------this KINDA SORTA works but it's dropping/replacing the first three characters of the original filename instead of just appending "00 " to the beginning
Desired:
ren 1234_###_1_4641_2020-07-30_ADPMasterControl.pdf "00 1234_###_1_4641_2020-07-30_ADPMasterControl.pdf"
What actually happened:
ren 1234_###_1_4641_2020-07-30_ADPMasterControl.pdf "00 4_###_1_4641_2020-07-30_ADPMasterControl.pdf"
echo . > "08 Pays to Non-Actives.txt" <---THIS WORKED FINE
pause
exit
Best Answer
For test first with
-WhatIf
If the results in the predicted executions are the expected effects, just remove
-WhatIf
and effectively the execution will take place:Some further reading:
[√] Get-Item | gi
[√] Rename-Item | ren | also -WhatIf | -New
[√] Foreach-Object | ? or %
[√] Don't fear hitting enter with whatif
For only actual folder:
For recursively:
Obs.: 1 To use in the command line, in bat, you have to double the variable
%~i
|%%~i
in for loop, yours attempts are not doing/using this:Obs.: 2 Remove the
echo\
after seeing in the outputs if it works well for what you need:Some further reading:
[√] For /F
[√] For Loop
[√] ren | rename