1

Need a batch script that recursively searches the whole C: drive for a specific file by file name and then renames the file

This is what I have so far. It appears to find the file but then it does not rename it.

@echo off

for /r "delims=" %%i in ('dir /s /b /a-d "C:\test.pdf"') do (ren "%%i" test2.pdf)
jarnosz
  • 243
  • 1
  • 18
snarf
  • 55
  • 1
  • 4
  • Possible duplicate of [Rename all files in a directory with a Windows batch script](http://stackoverflow.com/questions/9383032/rename-all-files-in-a-directory-with-a-windows-batch-script) – shoover Apr 04 '16 at 23:28
  • 1
    `for /F "delims=" %%i in ('dir /s /b /a-d "C:\test.pdf"') do …` rather than `for /r` – JosefZ Apr 04 '16 at 23:56

1 Answers1

1

Windows Server 2003 and later (i.e. anything after Windows XP 32 bit) provide the where.exe,it matches all types of files. It will even accept wildcards. Try where /? for help.

@echo off
setlocal enableDelayedExpansion
Set Location=C:\
Set PatternName=test.pdf
for /f "delims=" %%F in ('Where /R %Location% /F %PatternName%') do (
    set MyFilename=%%F 
    ren !MyFilename! "test2.pdf"
)
pause
Hackoo
  • 18,337
  • 3
  • 40
  • 70