Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DOS batch script for renaming files 1

Status
Not open for further replies.

djpingpong

Programmer
Jul 6, 2004
70
Hi,

I have a folder from work that contains about 4000 PDF files, each named something different
However, in each file name, somebody decided to put the number '4' before the ".pdf"
So, my filenames are "myfile4.pdf", "yourfile4.pdf", "testfile4,pdf", etc....

Is there a way that I can write a simple DOS batch script to remove the '4' from the file name and keep everything else the same?

I REALLY don't wanna rename 4000+ files manually

thanks
 
Google (other search engines are available"

free batch file renamer

Robert Wilensky:
We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true.

 
This sounds like a job for a VB program, although one place where I worked, they had a VB program to do a simular task once a day, and it took several hours. (It invovolved only 400-500 files.) I wrote a COBOL program to do it, and that took less than 1/10 second. The COBOL program does however require the COBOL libraries. If you wold like, I could rewrite the program to do what you need and send it and the libraries to you.
 
See if this is what you are trying to do?

Rename at Nth character in Command Line (dos)
thread779-1515548
 
The thread that linney linked is pretty close in concept and should point you in the right direction to do this with a batch file using a FOR loop. The question is are there any other occurences of the character 4?
Code:
dir *4*.*
 
Unfortunately,to the batch file processor, * matches from the specified point to the end of the name, less the extension. So *4. is treated as *.
 
How about something like this?...

Code:
@echo off
dir /b *.pdf|find /i "4.pdf">%temp%\~PDFLst1.tmp
for /f "tokens=1 delims=*" %%a in (%temp%\~PDFLst1.tmp) do call :SubRoutine "%%a"
if exist %temp%\~PDFLst1.tmp del %temp%\~PDFLst1.tmp
goto end
:SubRoutine
set SrcFileName=%~1
set DstFileName=%SrcFileName:~0,-5%.pdf
ren %SrcFileName% %DstFileName%
:end
 
webrabbit said:
Unfortunately,to the batch file processor, * matches from the specified point to the end of the name, less the extension. So *4. is treated as *.
Not on any version of XP that I've ever used. I think that back in the early versions of DOS that might have been true, but certainly is not anymore.


Even simpler than CaptainCommandLine's version of essentially the same thing. Remove the echo and pause statements if you don't want to see it happen. The possible problem with this (that I hinted at above), would be if one of these files has multiple 4's in the name -for example file4us4.pdf
Code:
@echo off
target= [COLOR=green][i]set full path to target folder[/i][/color]
for /F "tokens=1 delims=4" %%i in ('dir /b "%target%\*4.pdf"') do (
  echo renaming %%i4.pdf to %%i.pdf
  ren %%i4.pdf %%i.pdf
  )
echo done
pause
 
Actually, here's a modified version that should check for the possible problem with multiple 4's. This is all untested, so be sure to test before running against the 'real' folder.

Code:
@echo off
target= [COLOR=green][i]set full path to target folder[/i][/color]
if exist  "%target%\*4*4.pdf" goto problem
for /F "tokens=1 delims=4" %%i in ('dir /b "%target%\*4.pdf"') do (
  echo renaming %%i4.pdf to %%i.pdf
  ren %%i4.pdf %%i.pdf
  )echo done
pause
goto :eof

:problem
dir /b "%target%\*4.pdf" >> %userprofile%\Desktop\problemnames.txt
echo Problem filename(s) were found, fix these manually first
echo List of files is in text file problemnames on your desktop
pause
goto :eof
 
thanks SMAH,

it helped me out a lot...
saved me tonnes of time

thanks everyone who replied so quickly
 
You're welcome. For any future readers, I just noticed an error in the problem subroutine of the last batch file. The problemnames.txt file creation line should be:
Code:
dir /b "%target%\[!]*4[/!]*4.pdf" >> %userprofile%\Desktop\problemnames.txt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top