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
4
  • Your task cannot be done with a simple REN command by itself - see How does the Windows RENAME command interpret wildcards? for the reason why.
    – dbenham
    Aug 2, 2020 at 12:44
  • The solution is trivial with my JREN.BAT regular expression rename utility: jren "^" "00 " /fm "*ADPMasterControl.pdf"
    – dbenham
    Aug 2, 2020 at 12:50
  • Thank you for the reference to JREN.but but I got it in my head the very first time I learned about regular expressions that the whole think is just too complicated. After a few attempts to use regex, combined with comments from various people in linux forums over the years that I was using the wrong commands / switches / assumptions / version / &c. for what I wanted to do (like there would be a way to even know!), I gave up on trying to understand. Today I delve here very rarely and batch files work well for me. But I will check out your utility.
    – Steev43230
    Aug 2, 2020 at 16:41
  • It is true that if you don't understand how to use regular expressions, then JREN is pretty much useless. Regex syntax can look similar to wildcards like you use with normal renaming, but they operate very differently. You definitely need to study the specs. Simple patterns are easy to grasp, but they can get very complicated very quickly. Complicated - but powerful.
    – dbenham
    Aug 2, 2020 at 19:04

3 Answers 3

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"

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

3
  • Thanks. Based on your reference to the page on REN wildcards, I guess a for loop is need just to [reliably] run a command on just one file.
    – Steev43230
    Aug 2, 2020 at 16:34
  • Not really. In PowerShell, the pipeline often eliminates the need for looping. But with renaming, if the new name is based on the old name, the <Path[]> | ren -NewName <ScriptBlock> is the "cleanest" code. Aug 2, 2020 at 19:52
  • I triedfor %%i in (*ADPMasterControl.pdf) do echo\rename "%%~fi" "00 %%nxi" but what
    – Steev43230
    Aug 4, 2020 at 10:48
1

This line works:

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

Many thanks to all.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .