Computing.Net > Forums > Programming > Renaming files

Renaming files

Reply to Message Icon

Original Message
Name: lbtf73_99
Date: August 30, 2004 at 14:59:03 Pacific
Subject: Renaming files
OS: Windows XP Pro
CPU/Ram: P4 2.6
Comment:

I have a directory in which I need to rename files, somedays there may be 5 files with different filenames otherdays there may be 15 files with different file names. The filenames will change everyday as they have a date/time stamp appended to the file name. It doesn't make a difference which file gets renamed REPFILE.001 or REPFILE.002.I would rather not have to hard code the individual filenames into the batch file

I would like for it to do something like a dir /b and then pass that as a variable or something and then do either a rename or move file. So if a dir /b gives output of something like

CAT87P21_080821_143212
CAT45P21_SIS_080821_142332
CAT32P21_SIS_0808021_164322

Take that information and then do a

REN CAT87P21_080821_143212 REPFILE.001
REN CAT45P21_SIS_080821_142332 REPFILE.002
REN CAT32P21_SIS_0808021_164322 REPFILE.003

and so on.

Any suggestions or good information on writing batch files would also be appreciated.
Thanks
Lance


Report Offensive Message For Removal


Response Number 1
Name: IVO
Date: August 31, 2004 at 02:47:49 Pacific
Subject: Renaming files
Reply: (edit)

Save the script as REP.BAT then type REP Folder_Name (e.g. Rep C:\MyFolder\MySubFol) and enjoy (blanks not allowed!).

@Echo Off

Set Count=0
For /F %%A in ('Dir /B %~f1') Do Call :RN %~f1 %%A
Set Count=
Set Ext=
GoTo :EOF

:RN
Set /A Count=%Count%+1
Set Ext=%Count%
If %Count% lss 100 Set Ext=0%Ext%
If %Count% lss 10 Set Ext=0%Ext%
Ren %1\%2 REPFILE.%Ext%
GoTo :EOF


Report Offensive Follow Up For Removal

Response Number 2
Name: lbtf73_99
Date: August 31, 2004 at 12:35:55 Pacific
Subject: Renaming files
Reply: (edit)

SWEET!!!!! That worked fantastic thanks a lot, my final code looks like this

@ECHO OFF

CALL c:\folder\mat.bat c:\folder\receiving
CALL c:\folder\rep.bat c:\folder\reports

---------------
ECHO Please insert Receiving disk in drive A: and
---------------
PAUSE

CD "C:\folder\receiving"
:loop1
DEL a:\*.* /Q
MOVE RECFILE.* a:
IF ERRORLEVEL 1 ECHO Please insert next disk
IF ERRORLEVEL 1 PAUSE
IF ERRORLEVEL 1 GOTO loop1

---------------
ECHO Receiving Files have been copied
---------------
PAUSE

------------------
ECHO Please insert Report disk in drive A: and
------------------
PAUSE

CD "C:\folder\Reports"

:loop2
DEL a:\*.* /Q
MOVE REPFILE.* a:
IF ERRORLEVEL 1 ECHO Please insert next disk
IF ERRORLEVEL 1 PAUSE
IF ERRORLEVEL 1 GOTO loop2

---------------
ECHO Report Files have been copied
---------------
PAUSE


The reason I put call in there twice is I needed to change the file names in the receiving and reports folder. I also changed the DIR /B to DIR /B /OS in order to sort out by the size, that way if files 1-10 fit on the floppy but 11-14 were too large but 15 fit on there I wouldn't have 1-10 and 15 on 1 floppy and 11-14 on another. Once again thanks for all of the help


Report Offensive Follow Up For Removal

Response Number 3
Name: lbtf73_99
Date: September 1, 2004 at 10:11:04 Pacific
Subject: Renaming files
Reply: (edit)

Having a little problem with error handling, sometimes when I do the download I don't have any recfiles but have the repfile. right now when I run it I will get the error

A duplicate file name exists, or the file
cannot be found.
Please insert next disk
Press any key to continue . . .

This loops on and on as it should, however I thought if I introduced a IF ERROR LEVEL 2 GOTO loop2 it would work. Not the case, though. How can I define and if error level no files to copy and if error level disk is full? I'm not sure how to do this, I thought maybe I could do a set error level but I'm not sure how I could do this. Any help is greatly appreciated.
Thanks
Lance


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Renaming files

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge