?
Solved

rename files with current date in a bat file

Posted on 2007-10-15
16
Medium Priority
?
5,766 Views
Last Modified: 2013-11-14
i'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\messaging\fix.AAAA.csv" %GETSYSDATE%.csv
ren "\\myserver\public\dba\messaging\fix.BBBB.csv" %GETSYSDATE%.csv
ren "\\myserver\public\dba\messaging\fix.CCCC.csv" %GETSYSDATE%.csv
ren "\\myserver\public\dba\messaging\fix.DDDD.csv" %GETSYSDATE%.csv
ren "\\myserver\public\dba\messaging\fix.EEEE.csv" %GETSYSDATE%.csv
ren "\\myserver\public\dba\messaging\fix.FFFF.csv" %GETSYSDATE%.csv
ren "\\myserver\public\dba\messaging\fix.GGGG.csv" %GETSYSDATE%.csv
ren "\\myserver\public\dba\messaging\fix.HHHH.csv" %GETSYSDATE%.csv
ren "\\myserver\public\dba\messaging\fix.IIII.csv" %GETSYSDATE%.csv
ren "\\myserver\public\dba\messaging\other_file.csv" %GETSYSDATE%.csv >>.\messagingfileslog.txt
0
Comment
Question by:dbaSQL
  • 7
  • 6
  • 2
  • +1
16 Comments
 
LVL 9

Expert Comment

by:ghostdog74
ID: 20077459
a thought, are you mapped to a drive? if yes , use the drive letter...
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 20077549
You are renaming all the files with the same name. Not allowed.

Try this (untested) ...

0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 20077551
@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%%HH%%NN%%SS%%MS%

REM Create the log file.
ECHO Renaming started %TodaysDateTime% > ./messagingfileslog.txt

REM Define the location of the files
SET Source=\\myserver\public\dba\messaging

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% - %%F.csv" DEL "%Source%\%TodaysDateTime% - %%F.csv"
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% - %%F.csv" DEL "%Source%\%TodaysDateTime% - %%F.csv" (
                  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
      )
)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 40

Expert Comment

by:Richard Quadling
ID: 20077554
Oops.
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 20077564
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%%HH%%NN%%SS%%MS%

REM Create the log file.
ECHO Renaming started %TodaysDateTime% > ./messagingfileslog.txt

REM Define the location of the files
SET Source=\\myserver\public\dba\messaging

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% - %%F.csv" DEL "%Source%\%TodaysDateTime% - %%F.csv"
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% - %%F.csv" (
                  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
      )
)
0
 
LVL 17

Author Comment

by:dbaSQL
ID: 20077609
i am trying this now, rq.  can you help me make it in this format?   YYYYMMDD.filename.csv ?
0
 
LVL 17

Author Comment

by:dbaSQL
ID: 20077672
still isn't finding them -- it is logging properly, though -- 1/2 way there

Renaming started 2007101508051175
Could not find "\\myserver\public\dba\messaging\file1.csv.csv"
Could not find "\\myserver\public\dba\messaging\fix.AAAA.csv.csv"
Could not find "\\myserver\public\dba\messaging\fix.BBBB.csv.csv"
Could not find "\\myserver\public\dba\messaging\fix.CCCC.csv.csv"
Could not find "\\myserver\public\dba\messaging\fix.DDDD.csv.csv"
Could not find "\\myserver\public\dba\messaging\fix.EEEE.csv.csv"
Could not find "\\myserver\public\dba\messaging\fix.FFFF.csv.csv"
Could not find "\\myserver\public\dba\messaging\fix.GGGG.csv.csv"
Could not find "\\myserver\public\dba\messaging\fix.HHHH.csv.csv"
Could not find "\\myserver\public\dba\messaging\fix.IIII.csv.csv"
Could not find "\\myserver\public\dba\messaging\otherfile.csv.csv"
0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 1600 total points
ID: 20077688
@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\dba\messaging

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%.%%F.csv" DEL "%Source%\%TodaysDateTime%.%%F.csv"
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%.%%F.csv" (
                  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
      )
)
0
 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 20077772
take a close look at that log - it's got the extension .csv TWICE - remove one
0
 
LVL 17

Author Comment

by:dbaSQL
ID: 20077791
oh my goodness.  i didn't see that
0
 
LVL 17

Author Comment

by:dbaSQL
ID: 20077815
perfect!
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 20077821
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\dba\messaging

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%.%%F" DEL "%Source%\%TodaysDateTime%.%%F"
REM Rename it.
            REN "%Source%\%%F" "%TodaysDateTime%.%%F"
REM Check to see that we did all of that OK.
            IF EXIST "%Source%\%TodaysDateTime%.%%F" (
                  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
      )
)
0
 
LVL 17

Author Comment

by:dbaSQL
ID: 20077851
no worries, rq, i just changed it in here:

REM Define the filenames
SET Files=fix.AAAA fix.BBBB fix.CCCC fix.DDDD fix.EEEE fix.FFFF fix.GGGG fix.HHHH fix.IIII other_file

that's should be ok, too. Yes?
0
 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 20077911
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.
0
 
LVL 17

Author Comment

by:dbaSQL
ID: 20077979
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.  
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 20077995
[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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Loops Section Overview

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question