You are renaming all the files with the same name. Not allowed.
Try this (untested) ...
Main Topics
Browse All Topicsi've got a bat file, trying to rename files in a certain directory, just to include the date, such that new files w/the same name will not overwrite them. i'm trying to test it now, then i would schedule it to run on a daily basis
i don't think i'm doing the bat file correctly. first, when invoked, it just gives me a 'can't find the specified file' message for each file. then, it pumps out a blank log
can anybody help me 1st get the rename corrected, and then get the logging corrected, please? here's a copy of the bat file:
@ECHO OFF
CLS
SET CURRENTDATE=
SET GETSYSDATE=
FOR /F " tokens=1,2,3* delims=/, " %%i IN ('date /T') DO SET CURRENTDATE=%%i%%j%%k%%l
FOR /F " tokens=1,2,3* " %%i IN ('echo %CURRENTDATE%') DO SET GETSYSDATE=%%j%%k%%l
ren "\\myserver\public\dba\mes
ren "\\myserver\public\dba\mes
ren "\\myserver\public\dba\mes
ren "\\myserver\public\dba\mes
ren "\\myserver\public\dba\mes
ren "\\myserver\public\dba\mes
ren "\\myserver\public\dba\mes
ren "\\myserver\public\dba\mes
ren "\\myserver\public\dba\mes
ren "\\myserver\public\dba\mes
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
@ECHO OFF
REM Default to tokens 1 to 3.
SET UnivDateTokens=1-3
REM Determine the first token.
FOR /F "tokens=1 delims=.:/-, " %%A IN ('DATE /T') DO SET UnivDatePart1=%%A
REM If the first token is a letter then use tokens 2 to 4 instead.
IF "%UnivDatePart1:~0,1%" GTR "9" SET UnivDateTokens=2-4
REM Extract the current date and put it into the yy, mm, dd variables depending upon the system date format.
FOR /F "tokens=%UnivDateTokens% delims=.:/-, " %%A in ('DATE /T') DO (
FOR /F "skip=1 tokens=2-4 delims=/-,()." %%D IN ('ECHO.^|DATE') DO (
SET %%D=%%A
SET %%E=%%B
SET %%F=%%C
SET UnivDatePart1=
SET UnivDateTokens=
)
)
REM Extract the time into hh, mm, nn, ms variables.
FOR /F "tokens=1-4 delims=/:. " %%A IN ("%TIME%") DO (
SET HH=%%A
SET NN=%%B
SET SS=%%C
SET MS=%%D
)
IF %HH% LEQ 9 (
SET /A HH+=0
SET HH=0%HH%
)
REM Create a shortcut to the current date/time in CCYYMMDDHHNNSSMS format.
SET TodaysDateTime=%YY%%MM%%DD
REM Create the log file.
ECHO Renaming started %TodaysDateTime% > ./messagingfileslog.txt
REM Define the location of the files
SET Source=\\myserver\public\d
REM Define the filenames
SET Files=fix.AAAA.csv fix.BBBB.csv fix.CCCC.csv fix.DDDD.csv fix.EEEE.csv fix.FFFF.csv fix.GGGG.csv fix.HHHH.csv fix.IIII.csv other_file.csv
REM For each file ...
FOR %%F IN (%Files%) DO (
REM If it exists.
IF EXIST "%Source%\%%F.csv" (
REM Delete any potential backup.
IF EXIST "%Source%\%TodaysDateTime%
REM Rename it.
REN "%Source%\%%F.csv" "%TodaysDateTime% - %%F.csv"
REM Check to see that we did all of that OK.
IF EXIST "%Source%\%TodaysDateTime%
ECHO Renamed "%Source%\%%F.csv" to "%TodaysDateTime% - %%F.csv" >> ./messagingfileslog.txt
) ELSE (
REM Couldn't rename it.
ECHO Could not rename "%Source%\%%F.csv" to "%TodaysDateTime% - %%F.csv" >> ./messagingfileslog.txt
)
) ELSE (
REM Couldn't find it.
ECHO Could not find "%Source%\%%F.csv">> ./messagingfileslog.txt
)
)
Line 55 edited - sorry.
@ECHO OFF
REM Default to tokens 1 to 3.
SET UnivDateTokens=1-3
REM Determine the first token.
FOR /F "tokens=1 delims=.:/-, " %%A IN ('DATE /T') DO SET UnivDatePart1=%%A
REM If the first token is a letter then use tokens 2 to 4 instead.
IF "%UnivDatePart1:~0,1%" GTR "9" SET UnivDateTokens=2-4
REM Extract the current date and put it into the yy, mm, dd variables depending upon the system date format.
FOR /F "tokens=%UnivDateTokens% delims=.:/-, " %%A in ('DATE /T') DO (
FOR /F "skip=1 tokens=2-4 delims=/-,()." %%D IN ('ECHO.^|DATE') DO (
SET %%D=%%A
SET %%E=%%B
SET %%F=%%C
SET UnivDatePart1=
SET UnivDateTokens=
)
)
REM Extract the time into hh, mm, nn, ms variables.
FOR /F "tokens=1-4 delims=/:. " %%A IN ("%TIME%") DO (
SET HH=%%A
SET NN=%%B
SET SS=%%C
SET MS=%%D
)
IF %HH% LEQ 9 (
SET /A HH+=0
SET HH=0%HH%
)
REM Create a shortcut to the current date/time in CCYYMMDDHHNNSSMS format.
SET TodaysDateTime=%YY%%MM%%DD
REM Create the log file.
ECHO Renaming started %TodaysDateTime% > ./messagingfileslog.txt
REM Define the location of the files
SET Source=\\myserver\public\d
REM Define the filenames
SET Files=fix.AAAA.csv fix.BBBB.csv fix.CCCC.csv fix.DDDD.csv fix.EEEE.csv fix.FFFF.csv fix.GGGG.csv fix.HHHH.csv fix.IIII.csv other_file.csv
REM For each file ...
FOR %%F IN (%Files%) DO (
REM If it exists.
IF EXIST "%Source%\%%F.csv" (
REM Delete any potential backup.
IF EXIST "%Source%\%TodaysDateTime%
REM Rename it.
REN "%Source%\%%F.csv" "%TodaysDateTime% - %%F.csv"
REM Check to see that we did all of that OK.
IF EXIST "%Source%\%TodaysDateTime%
ECHO Renamed "%Source%\%%F.csv" to "%TodaysDateTime% - %%F.csv" >> ./messagingfileslog.txt
) ELSE (
REM Couldn't rename it.
ECHO Could not rename "%Source%\%%F.csv" to "%TodaysDateTime% - %%F.csv" >> ./messagingfileslog.txt
)
) ELSE (
REM Couldn't find it.
ECHO Could not find "%Source%\%%F.csv">> ./messagingfileslog.txt
)
)
still isn't finding them -- it is logging properly, though -- 1/2 way there
Renaming started 2007101508051175
Could not find "\\myserver\public\dba\mes
Could not find "\\myserver\public\dba\mes
Could not find "\\myserver\public\dba\mes
Could not find "\\myserver\public\dba\mes
Could not find "\\myserver\public\dba\mes
Could not find "\\myserver\public\dba\mes
Could not find "\\myserver\public\dba\mes
Could not find "\\myserver\public\dba\mes
Could not find "\\myserver\public\dba\mes
Could not find "\\myserver\public\dba\mes
Could not find "\\myserver\public\dba\mes
@ECHO OFF
REM Default to tokens 1 to 3.
SET UnivDateTokens=1-3
REM Determine the first token.
FOR /F "tokens=1 delims=.:/-, " %%A IN ('DATE /T') DO SET UnivDatePart1=%%A
REM If the first token is a letter then use tokens 2 to 4 instead.
IF "%UnivDatePart1:~0,1%" GTR "9" SET UnivDateTokens=2-4
REM Extract the current date and put it into the yy, mm, dd variables depending upon the system date format.
FOR /F "tokens=%UnivDateTokens% delims=.:/-, " %%A in ('DATE /T') DO (
FOR /F "skip=1 tokens=2-4 delims=/-,()." %%D IN ('ECHO.^|DATE') DO (
SET %%D=%%A
SET %%E=%%B
SET %%F=%%C
SET UnivDatePart1=
SET UnivDateTokens=
)
)
REM Create a shortcut to the current date/time in CCYYMMDD format.
SET TodaysDateTime=%YY%%MM%%DD
REM Create the log file.
ECHO Renaming started %TodaysDateTime% > ./messagingfileslog.txt
REM Define the location of the files
SET Source=\\myserver\public\d
REM Define the filenames
SET Files=fix.AAAA.csv fix.BBBB.csv fix.CCCC.csv fix.DDDD.csv fix.EEEE.csv fix.FFFF.csv fix.GGGG.csv fix.HHHH.csv fix.IIII.csv other_file.csv
REM For each file ...
FOR %%F IN (%Files%) DO (
REM If it exists.
IF EXIST "%Source%\%%F.csv" (
REM Delete any potential backup.
IF EXIST "%Source%\%TodaysDateTime%
REM Rename it.
REN "%Source%\%%F.csv" "%TodaysDateTime%.%%F.csv"
REM Check to see that we did all of that OK.
IF EXIST "%Source%\%TodaysDateTime%
ECHO Renamed "%Source%\%%F.csv" to "%TodaysDateTime%.%%F.csv"
) ELSE (
REM Couldn't rename it.
ECHO Could not rename "%Source%\%%F.csv" to "%TodaysDateTime%.%%F.csv"
)
) ELSE (
REM Couldn't find it.
ECHO Could not find "%Source%\%%F.csv">> ./messagingfileslog.txt
)
)
Sorry. I added the list of names without fix.***.csv. THen added other_file.csv, and fix.****.csv, but forgot the .csv bit in the main code.
Try this ...
@ECHO OFF
REM Default to tokens 1 to 3.
SET UnivDateTokens=1-3
REM Determine the first token.
FOR /F "tokens=1 delims=.:/-, " %%A IN ('DATE /T') DO SET UnivDatePart1=%%A
REM If the first token is a letter then use tokens 2 to 4 instead.
IF "%UnivDatePart1:~0,1%" GTR "9" SET UnivDateTokens=2-4
REM Extract the current date and put it into the yy, mm, dd variables depending upon the system date format.
FOR /F "tokens=%UnivDateTokens% delims=.:/-, " %%A in ('DATE /T') DO (
FOR /F "skip=1 tokens=2-4 delims=/-,()." %%D IN ('ECHO.^|DATE') DO (
SET %%D=%%A
SET %%E=%%B
SET %%F=%%C
SET UnivDatePart1=
SET UnivDateTokens=
)
)
REM Create a shortcut to the current date/time in CCYYMMDD format.
SET TodaysDateTime=%YY%%MM%%DD
REM Create the log file.
ECHO Renaming started %TodaysDateTime% > ./messagingfileslog.txt
REM Define the location of the files
SET Source=\\myserver\public\d
REM Define the filenames
SET Files=fix.AAAA.csv fix.BBBB.csv fix.CCCC.csv fix.DDDD.csv fix.EEEE.csv fix.FFFF.csv fix.GGGG.csv fix.HHHH.csv fix.IIII.csv other_file.csv
REM For each file ...
FOR %%F IN (%Files%) DO (
REM If it exists.
IF EXIST "%Source%\%%F" (
REM Delete any potential backup.
IF EXIST "%Source%\%TodaysDateTime%
REM Rename it.
REN "%Source%\%%F" "%TodaysDateTime%.%%F"
REM Check to see that we did all of that OK.
IF EXIST "%Source%\%TodaysDateTime%
ECHO Renamed "%Source%\%%F" to "%TodaysDateTime%.%%F" >> ./messagingfileslog.txt
) ELSE (
REM Couldn't rename it.
ECHO Could not rename "%Source%\%%F" to "%TodaysDateTime%.%%F" >> ./messagingfileslog.txt
)
) ELSE (
REM Couldn't find it.
ECHO Could not find "%Source%\%%F">> ./messagingfileslog.txt
)
)
RQuadling did a lot of work on this - but I have to ask the question - couldn't you have gotten the same results without all the fancy logging by just turning echo on? Logging and friendly error messages are nice, but when coding, it's a lot faster to just switch echo on and review the output. At least in my opinion. There's nothing "wrong" with RQ's methods, but it's just a bit excessive and unnecessary in my opinion.
Well, I understand what you're saying, but.... I'm a dba, I don't know the bat files as well as probably both of you do, and I don't know that I could have gotten the same simply by turning on echo. Surely I can, but again, I don't know the bat files much beyond the absolute basics. Hence, the inquiry. RQ's suggestion worked both on the rename and the fancy logging, which is exaclty what I was looking for.
[dbaSQL] - yes. That should be fine. Whatever is easiest for you really. If all your files are .csv then what you've done is fine.
That line COULD also be written as ...
SET Files=other_file
FOR %%L IN (A B C D E F G H I) DO SET Files=!Files! fix.%%L%%L%%L%%L
If you feel particularly crazy. This does mean you could add J K L M just by adding the 4 letters!
[leew] - The reason I log things like this is that I run my scripts unattended. Habit. The script I did is based upon some code I already have and I never run this manually - Windows Scheduler does, so no screen output.
If you are running these scripts by hand, then yes, you can remove the echo on, but the output will also include all the code.
And in loops, especially embedded loops like these, that can be REALLY awkward.
But, as you said, it is not "wrong" either way.
I also have a small wrapper app which takes the log files and emails them to me. Saves me having to look through lots of log files on different servers.
Business Accounts
Answer for Membership
by: ghostdog74Posted on 2007-10-15 at 05:38:22ID: 20077459
a thought, are you mapped to a drive? if yes , use the drive letter...