Help for writing a batch file to rename files.

December 16, 2011 at 02:51:04
Specs: Windows XP
Hello,
I am new to writing batch files. So require help in writnig a batch file.

I have somme PDF files in a folder C:\Documents and Settings\Desktop\Static PDF.

The names of the PDF files in the above folder are as follows:

IND Regression Pending.pdf
IND Test Pending.pdf
IND Complete.pdf...........ans so on.

Now I want to write a batch file which will rename these files with same name. The only difference in new name being that instead of spaces it should put "_".

Example: 1) IND Regression Pending.pdf should be named as
IND_Regression_Pending.pdf

2) IND Test Pending.pdf should be named as
IND_Test_Pending.pdf

3 ) IND Complete.pdf should be named as
IND_Complete.pdf
............... and so on.
I have nearly 60-70 file and all need to be renamed in same manner.
Please if anyone can help coz its very urgent. Help will be appreciated.


See More: Help for writing a batch file to rename files.

Report •


#1
Report •

#2
December 16, 2011 at 05:30:36

Report •

#3
December 16, 2011 at 15:09:06
setlocal enableDelayedExpansion
for /f "delims=" %%a in ('dir/b "C:\Documents and Settings\Desktop\Static PDF\*.pdf"') do (
set name=%%~NXa
ren "%%~Fa" "!name: =_!"
)

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
December 21, 2011 at 02:51:00
The above code is not working dnt know for wht reason. It says that the system cannot find the path specified. But thanks for the help. We have thought of another solution but not able to execute it. That the source path ,source file name, destination path and the name after copying the file be put in an xml file. and tht we can have a .bat file to read the contents and execute renamimg and copying.

Eg: we have created a xml file:
<source>C:\Documents and Settings\irsbatch\Desktop\Static PDF\</source>
<source name>IND Testing.pdf</source name>
<destination>C:\Documents and Settings\irsbatch\Desktop\Static PDF1\</destination>
<rename>IND_Testing.pdf</rename>

like this we'll put all the files to be copied and renamed.

I want a .bat file that reads the contents of this xml 1 by 1 and executes accordingly. i.e first it will go the the source path by reading data of <source> tag. Then it will pick the file mentioned in <source name> tag. then it will copy it to the destination path mentioned in the <destination> tag. And then finally rename it with the name mentioned in <rename> tag.

Even if it is done till copying its fine. Pls help.


Report •

#5
December 21, 2011 at 06:14:50
It says that the system cannot find the path specified.
That means you gave me a bad path.

That the source path ,source file name, destination path and the name after copying the file be put in an xml file.
Ick, talk about over complicating things.

Batches hate XML, and cannot parse it reliably, but that's fine. I don't know why we, as an industry, would choose to pack 3KB of metadata around 1KB of actual data.

setlocal enableDelayedExpansion
for /f "delims=" %%a in ('dir/b "C:\Documents and Settings\irsbatch\Desktop\Static PDF\*.pdf"') do (
set name=%%~NXa
ren "%%~Fa" "!name: =_!"
)

How To Ask Questions The Smart Way


Report •

#6
December 21, 2011 at 22:10:08
Even I had the same question when mt manager suggested this solution. I'll tell you the reason he gave me. See there two folders : 1. Source folder and 2. Destination folder. Every I copy some PDFs from source to destination and then rename in the way I told u earlier(replacing spaces with "_"). Today I am copying lets say 10 files. Then its easy to do this with 10 files. But may be in future my client would want me to do this with 100 files. So I cant copy and rename them all manually. So he said that me put all th data in a xml file in the format I told u. So that we can automate the coping and renaming. This is our main motto and the demand from client that automate this system. Let the batches themselves do the copying and renaming. Can we have like this that we can write a unix srcipt to parse xml file and then store its data in a txt file. And then let the batch read the txt file to execute the job??

Report •

#7
December 21, 2011 at 23:15:23
Hey Razor2.3,
I tried running ur code once again. The problem with the previous path was that there was space in the name of the folder Static PDF.
i.e The path was C:\Documents and Settings\irsbatch\Desktop\Static PDF\*.pdf
it was not able to recognise the folder as it was taking the following path:
C:\Documents and Settings\irsbatch\Desktop\Static
And thats why the error: cannot find the specified path. Now I have changed my folder name to StaticPDF. And now it is recognising the following path:
C:\Documents and Settings\irsbatch\Desktop\StaticPDF\*.pdf

However now the problem is that the names of the PDFs is changed only when I copy those PDFs in StaticPDF folder on desktop. Any idea why is it so?


The following is the code I wrote in the batch file automate.bat :

setlocal enableDelayedExpansion
for /f "delims=" %%a in ('dir/b "C:\Documents and Settings\irsbatch\Desktop\StaticPDF\*.pdf"') do (
set name=%%~NXa
ren "%%~Fa" "!name: =_!"
)
pause

The following is the errors that I get when I run the automate.bat file :



C:\Documents and Settings\irsbatch\Desktop>setlocal enableDelayedExpansion

C:\Documents and Settings\irsbatch\Desktop>for /F "delims=" %a in ('dir/b "C:\Do
cuments and Settings\irsbatch\Desktop\StaticPDF\*.pdf"') do (
set name=%~NXa
ren "%~Fa" "!name: =_!"
)

C:\Documents and Settings\irsbatch\Desktop>(
set name=IND Complete.pdf
ren "C:\Documents and Settings\irsbatch\Desktop\IND Complete.pdf" "!name: =_!"

)
The system cannot find the file specified.

C:\Documents and Settings\irsbatch\Desktop>(
set name=IND Regression Pending.pdf
ren "C:\Documents and Settings\irsbatch\Desktop\IND Regression Pending.pdf" "!n
ame: =_!"
)
The system cannot find the file specified.

C:\Documents and Settings\irsbatch\Desktop>(
set name=IND Test Pending.pdf
ren "C:\Documents and Settings\irsbatch\Desktop\IND Test Pending.pdf" "!name: =
_!"
)
The system cannot find the file specified.

C:\Documents and Settings\irsbatch\Desktop>pause
Press any key to continue . . .

However If I copy these 4 PDFs from StaticPDF folder directly on Desktop then the code is running fine and its renaming these 4 PDFs. Tried to figure out why its happening but didnt get it. Can you please help??


Report •


Ask Question