1.

Solve : Batch file to add date to file name?

Answer»

Hello Folks,

Have a small problem that I'm hoping I could get some assistance with.  I currently have a batch file (if memory serves, it seems I originally obtained the idea from this site) that takes the date and adds it to an archive file that's created on a daily basis.  This works fine when the regional short date format in Windows is setup for M/d/yyyy.  However, we have another location which is using the short date format of dd-MMM-yy in which case the same batch file will not work.  I've tried adjusting the date variables in the existing batch file, but I'm not getting anywhere. 

Is it possible someone might be able to tell me which settings I need to adjust to accommodate the other date format of dd-MMM-yy so the batch file could do it's thing?

Existing file:
Code: [Select]echo off
:: setting MM to equal the current numeric month
set MM=%date:~4,2%

:: putting all the possible months into a variable which will be parsed in the for loop
set month_text="01 JAN" "02 FEB" "03 MAR" "04 APR" "05 MAY" "06 JUN" "07 JUL" "08 AUG" "09 SEP" "10 OCT" "11 NOV" "12 DEC"

:: for loop is parsing the month_text variable and using the find command with the numeric month to set the month variable
for %%I in (%month_text%) do echo %%I |find "%MM%" &&set MM=%%~I
:: Need to remove the NUMBER and space
echo Date: %date%
echo Today: %date:~7,2% %MM:~3% %date:~10,4%
SET Month=%DATE:~4,2%
if %Month%==01 set Month=JAN
if %Month%==02 set Month=FEB
if %Month%==03 set Month=MAR
if %Month%==04 set Month=APR
if %Month%==05 set Month=MAY
if %Month%==06 set Month=JUN
if %Month%==07 set Month=JUL
if %Month%==08 set Month=AUG
if %Month%==09 set Month=SEP
if %Month%==10 set Month=OCT
if %Month%==11 set Month=NOV
if %Month%==12 set Month=DEC
echo %Month%
For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do (
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set All=%date:~7,2% %MM:~3% %%C
)
 
ECHO Compressing Daily Inventory/Receiving Files and Powering Off
ECHO off
7z a -t7z "Warehouse 7C - %ALL%.7z" -x!*.7z -mx9 -mmt
shutdown /s

In all file names, I wish for the date to appear as DD MMM YYYY [ie: 02 NOV 2014]

Thanks so much,

KenThis should work on XP Pro and above.
Code: [Select]ECHO off
:: GET DATE and TIME
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"Goodness, what a speedy and helpful reply.  That works marvelous.  It's also nice to have the time variables as well for future reference.

Tested as follows (to, hopefully, display the abbreviated month in uppercase):

Code: [Select]ECHO off

SET Month=%DATE:~3,3%
if %Month%==01 set Month=JAN
if %Month%==02 set Month=FEB
if %Month%==03 set Month=MAR
if %Month%==04 set Month=APR
if %Month%==05 set Month=MAY
if %Month%==06 set Month=JUN
if %Month%==07 set Month=JUL
if %Month%==08 set Month=AUG
if %Month%==09 set Month=SEP
if %Month%==10 set Month=OCT
if %Month%==11 set Month=NOV
if %Month%==12 set Month=DEC

:: GET DATE and TIME
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"

echo DD-MMM-YYYY: %DD% %Month% %YYYY%
pause

The only downside I can find is when the regional date format is set for M/d/yyyy, the month is displayed ENTIRELY in uppercase as I wish.  However, if the date format is set for dd-MMM-yy, only the first character is in uppercase.  Any way of getting the entire month to be uppercase? 

Thank you so much.

KenTry this:

Code: [Select]ECHO off
:: GET DATE and TIME
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"

if %MM%==01 set Month=JAN
if %MM%==02 set Month=FEB
if %MM%==03 set Month=MAR
if %MM%==04 set Month=APR
if %MM%==05 set Month=MAY
if %MM%==06 set Month=JUN
if %MM%==07 set Month=JUL
if %MM%==08 set Month=AUG
if %MM%==09 set Month=SEP
if %MM%==10 set Month=OCT
if %MM%==11 set Month=NOV
if %MM%==12 set Month=DEC

echo DD-MMM-YYYY: %DD% %Month% %YYYY%
pause Quote from: KenK13 on November 03, 2014, 07:24:37 AM


The only downside I can find is when the regional date format is set for M/d/yyyy, the month is displayed entirely in uppercase as I wish.  However, if the date format is set for dd-MMM-yy, only the first character is in uppercase.  Any way of getting the entire month to be uppercase? 

Thank you so much.

Ken
Was not the point of your question that you wanted one way to get the date in a consistent format so that you could use your SET commands to set the Month Abbreviation in upper case?  Why are you still using the DATE command?Thank you foxidrive, your solution works perfect!

Quote from: foxidrive on November 03, 2014, 07:29:28 AM
Try this:

Code: [Select]ECHO off
:: GET DATE and TIME
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"

if %MM%==01 set Month=JAN
if %MM%==02 set Month=FEB
if %MM%==03 set Month=MAR
if %MM%==04 set Month=APR
if %MM%==05 set Month=MAY
if %MM%==06 set Month=JUN
if %MM%==07 set Month=JUL
if %MM%==08 set Month=AUG
if %MM%==09 set Month=SEP
if %MM%==10 set Month=OCT
if %MM%==11 set Month=NOV
if %MM%==12 set Month=DEC

echo DD-MMM-YYYY: %DD% %Month% %YYYY%
pause
Quote from: Squashman on November 03, 2014, 07:37:09 AM
Was not the point of your question that you wanted one way to get the date in a consistent format so that you could use your SET commands to set the Month Abbreviation in upper case?  Why are you still using the DATE command?

Sorry, I guess I'm not understanding you clearly.  Yes, I want to get a consistent date displayed as: DD MMM YYYY (with abbreviated month name in uppercase).  However, when I displayed the date using the code you originally provided, the result was displayed as a numeric month.

For example, taking your code as is and displaying the output:
Code: [Select]ECHO off
:: GET DATE and TIME
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"

echo %DD% %MM% %YYYY%

Displayed results: 03 11 2014

That was why I went back and used a portion of the code I originally had.  This enables me to obtain the non-numeric date. [ie: NOV, not 11].

Thank you for your help.

You had the code to translate the numeric month to the abbreviated month. Did not THINK I had to add that to the code I gave you.  Seemed fairly obvious to me.Not sure why you think it's necessary to belittle me here, but if that's what makes you happy so be it.


YEAH, it was obvious to me as well.  That's why I did indeed include the month code to translate the months.  I was just curious why they had not all been uppercase.  And you made a choice to get on my case about using DATE instead of SET.  I used what worked properly for me in my particular case.  Thanks to the great help of foxidrive, I now have what works awesome.


I didn't get on your case about using the date command. I ASKED you a very black and white question. You obviously knew that the regional settings affected the output of the date command and I gave you a consistent date output that wasn't affected by the regional settings and could be used to translate into the month abbreviation using your existing set commands. You then asked again about using the date command again so I asked why you were still trying to use it. Quote from: KenK13 on November 03, 2014, 07:24:37 AM
Code: [Select]
SET Month=%DATE:~3,3%
if %Month%==01 set Month=JAN
if %Month%==02 set Month=FEB
if %Month%==03 set Month=MAR
if %Month%==04 set Month=APR
if %Month%==05 set Month=MAY
if %Month%==06 set Month=JUN
if %Month%==07 set Month=JUL
if %Month%==08 set Month=AUG
if %Month%==09 set Month=SEP
if %Month%==10 set Month=OCT
if %Month%==11 set Month=NOV
if %Month%==12 set Month=DEC


The code you showed is both peculiar and broken.  Your code essentially uses a numeric month to give you a three letter month, and
with Squashman's code it works perfectly.

The first line above sets the month variable to a THREE character string and then you proceed to compare it with a TWO character string - so it will do nothing and the original month variable will remain TOTALLY unchanged.

That is why it failed.


I should add that batch is peculiar too and if the string (of two numerals) happened to have a leading or trailing space then it could match in the compares. 


Discussion

No Comment Found