Computing.Net > Forums > Programming > Renaming files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Renaming files

Reply to Message Icon

Name: lbtf73_99
Date: August 30, 2004 at 14:59:03 Pacific
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



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: August 31, 2004 at 02:47:49 Pacific
Reply:

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


0

Response Number 2
Name: lbtf73_99
Date: August 31, 2004 at 12:35:55 Pacific
Reply:

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


0

Response Number 3
Name: lbtf73_99
Date: September 1, 2004 at 10:11:04 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Renaming files

Renaming files using a batch www.computing.net/answers/programming/renaming-files-using-a-batch/11819.html

Renaming files www.computing.net/answers/programming/renaming-files/11768.html

rename file extentions www.computing.net/answers/programming/rename-file-extentions/15634.html