Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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_164322Take 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.003and so on.
Any suggestions or good information on writing batch files would also be appreciated.
Thanks
Lance

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

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
---------------
PAUSECD "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
------------------
PAUSECD "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

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

![]() |
![]() |
![]() |

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