rename files in batch program

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
geekpie
Posts: 2
Joined: 04 Aug 2009 08:36

rename files in batch program

#1 Post by geekpie » 04 Aug 2009 08:42

Hi
In a certain folder, some of the filenames are like this:
xy [Compatibility Mode].pdf
abc [Compatibility Mode].pdf

I would like to rename those files
xy.pdf
abc.pdf

So far my attempts are floundering. thanks for any advice.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 04 Aug 2009 10:58

This isn't perfect but will probably get you close. It will strip out ALL spaces in the renamed file, as it would be a fair amount of additional code to strip out only the space just before the .pdf.

Code: Select all

@echo off
setlocal enabledelayedexpansion
set "folder=c:\path\to\your\files"
for /f "tokens=*" %%a in ('dir /b /a-d "%folder%\*compatibility mode*"') do (
   for /f "tokens=1-2* delims=[]" %%b in ("%%~a") do (
      set "newname=%%~b%%~d"
      ren "%folder%\%%~a" "!newname: =!"
   )
)

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

it is a esay way without for

#3 Post by rfpd » 04 Aug 2009 16:54

Code: Select all

set /p name=
rename yourfile.pdf "%name%.pdf"




sorry if you do not want this i am portuguese. :P

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#4 Post by ghostmachine4 » 04 Aug 2009 17:43

you can use vbscript.

Code: Select all

Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder = "c:\test"
Set objFolder =objFS.GetFolder(strFolder)
For Each strFile In objFolder.Files
   strExtension = objFS.GetExtensionName(strFile)
   strFileName = strFile.Name
   If strExtension = "pdf" And InStr(strFileName,"Compatibility Mode") > 0 Then
      i = InStr(strFileName,"[")
      s = Trim(Mid(strFileName,1,i-1))
      strNewFileName = s & ".pdf"
      strFile.Name = strNewFileName
   End If
Next


save as myscript.vbs and on command line

Code: Select all

c:\test> cscript /nologo myscript.vbs

geekpie
Posts: 2
Joined: 04 Aug 2009 08:36

#5 Post by geekpie » 05 Aug 2009 03:48

Thanks that was an enormous help.

The files I'm trying to rename were created in a loop earlier in the batch program. They were created using pdfcreator.exe

PDFCreator.exe /NoStart /PFmyfile.ppt myfile.pdf

The problem is the renaming doesn't work for those created in later iterations of the loop; the PDFCreator process is still running.

"The process cannot access the file because it is being used by another process."

Is there a way I can delay the rename command?

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#6 Post by avery_larry » 05 Aug 2009 10:27

Pick a number of seconds that you want to delay, add 1 to that number, and then use this:

ping -w 800 127.0.0.1 -n 21 >nul 2>nul

Change the number 21 in the above example to the number of seconds plus 1 that you want to wait. Quick and easy.

Post Reply