Page 1 of 1

rename files in batch program

Posted: 04 Aug 2009 08:42
by geekpie
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.

Posted: 04 Aug 2009 10:58
by avery_larry
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: =!"
   )
)

it is a esay way without for

Posted: 04 Aug 2009 16:54
by rfpd

Code: Select all

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




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

Posted: 04 Aug 2009 17:43
by ghostmachine4
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

Posted: 05 Aug 2009 03:48
by geekpie
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?

Posted: 05 Aug 2009 10:27
by avery_larry
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.