4

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

3 Answers3

3
  • For PowerShell one line:

For test first with -WhatIf

Get-item "C:\Folder\Files\*ADPMasterControl.pdf" | Foreach-Object { Rename-Item $_.FullName -NewName ('00 '+$_.Name) -WhatIf }

Or,... ::

gi "C:\Folder\Files*ADPMasterControl.pdf" | % { ren $.fullname -new ('00 '+$.name) -WhatIf }

If the results in the predicted executions are the expected effects, just remove -WhatIf and effectively the execution will take place:

Get-item "C:\Folder\Files\*ADPMasterControl.pdf" | Foreach-Object { Rename-Item $_.FullName -NewName ('00 '+$_.Name) -WhatIf  }

Or,... ::

gi "C:\Folder\Files*ADPMasterControl.pdf" | % { ren $.fullname -new ('00 '+$.name) -WhatIf }



  • For cmd/bat/command-line

For only actual folder:

cd /d "d\your\root\folder\where\files\tree\start\"
for %i in (*ADPMasterControl.pdf)do echo\rename "%~fi" "00 %~nxi"

For recursively:

cd /d "d\your\root\folder\where\files\tree\start\"
for /r %i in (*ADPMasterControl.pdf)do echo\rename "%~fi" "00 %~nxi"

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:

cd /d "d\your\root\folder\where\files\tree\start\"
for %%i in (*ADPMasterControl.pdf)do echo\rename "%%~fi" "00 %%~nxi"
cd /d "d\your\root\folder\where\files\tree\start\"
for /r %%i in (*ADPMasterControl.pdf)do echo\rename "%%~fi" "00 %%~nxi"

Obs.: 2 Remove the echo\ after seeing in the outputs if it works well for what you need:

cd /d "d\your\root\folder\where\files\tree\start\"
for %%i in (*ADPMasterControl.pdf)do echo\rename "%%~fi" "00 %%~nxi"
cd /d "d\your\root\folder\where\files\tree\start\"
for /r %%i in (*ADPMasterControl.pdf)do echo\rename "%%~fi" "00 %%~nxi"

Io-oI
  • 9,237
1

In Windows 10 you should be using PowerShell.

$SearchName = 'ADPMasterControl.pdf'
$SearchPath = "*$SearchName"
gci $SearchPath | Rename-Item -NewName { '00 {0}' -f $_.Name }

gci / Get-ChildItem

Rename-Item

Keith Miller
  • 10,694
  • 1
  • 20
  • 35
1

This line works:

for %%a in (*ADPMasterControl.pdf) do (set fname=%%a & call ren %%fname%% "00 %%fname%%")

Many thanks to all.