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.
rename files in batch program
Moderator: DosItHelp
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
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
Code: Select all
set /p name=
rename yourfile.pdf "%name%.pdf"
sorry if you do not want this i am portuguese.
-
- Posts: 319
- Joined: 12 May 2006 01:13
you can use vbscript.
save as myscript.vbs and on command line
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
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?
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?
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa