Computing.Net > Forums > Programming > Batch -change month # to letters

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.

Batch -change month # to letters

Reply to Message Icon

Name: LostSoul
Date: April 28, 2006 at 10:09:31 Pacific
OS: WinXP Pro
CPU/Ram: P4/1GB
Product: MPC/ClientPro
Comment:

I need help change my DIR filename from this style: 04_28_2006 (mm_dd_yyyy) to Apr_28_2006.
I am not sure how to change 04 to Apr.

Here is whaty I have so far that gets me the 04_28_2006 :
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET date=%mm%_%dd%_%yyyy%
MKDIR "D:\Test\%date%"

I am really new at BATCH writing, so any help would be greatly appreciated.
Thank you



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: April 28, 2006 at 11:06:09 Pacific
Reply:

First off, you can save yourself all those for statements. Since NT has a built-in DATE var, you can simply use substrings to get the parts you need.

In my case the DATE var is:

Sat 29-04-2006

so doing this:

set YYYYMMDD=%date:~10,4%%date:~7,2%%date:~4,2%

will set a new var called YYYYMMDD

BTW, it's not usually a good idea to alter a built-in var, like DATE.

Moving right along, I guess the simplest way to change 'numeric' month to 'named' month is with a chunk of code like this:

#####
if %MM%==01 set MM=Jan
if %MM%==02 set MM=Feb
if %MM%==03 set MM=Mar
if %MM%==04 set MM=Apr
and so on
#####



=====================================
If at first you don't succeed, you're about average.

M2Go



0

Response Number 2
Name: LostSoul
Date: April 28, 2006 at 12:25:39 Pacific
Reply:

That worked great!
Thank you Mechanix2Go!

Plus, thank you for the easier way to get the date in WinXP.


0

Response Number 3
Name: nopk73
Date: May 3, 2006 at 21:52:57 Pacific
Reply:

Mechanix2Go..

have you ever run into a situation where the command you entered above doesn't work right..

When I paste your exact line into my cmd window, then check the variable YYYYMMDD's value.. I get:

YYYYMMDD=005/

Ooo.. wait a mo.. brainstorm.. betcha is the date format..

(remote desktop back to 'defective' machine..)

yup..

so the command above is very picky about the format that the Date command returns info in..

thanks for causing the brainstorm! :)

Brett


0

Response Number 4
Name: Mechanix2Go
Date: May 4, 2006 at 01:31:43 Pacific
Reply:

Hi Brett,

Good work.

The batch below gets the date via BIOS, so the 'layout' is ignored. Unfortunately it needs CMD; won't work in DOS/w3x~Me.

When I first posted it, I said if there was enough interest, I'd work on a DOS silution. So far, there's been, as they say in the Buddhist tradition, a thunderous silence.

::== y6.bat
:: get sys YMD into vars

@echo off

:: YYYY getter
> syyyy.d echo a 100
>> syyyy.d echo mov ah,2a
>> syyyy.d echo int 21
>> syyyy.d echo.
>> syyyy.d echo p=100 2
>> syyyy.d echo n sizeYYYY
>> syyyy.d echo w
>> syyyy.d echo q
debug < syyyy.d > nul
:: OK

:: MM getter
> sMM.d echo a 100
>> sMM.d echo mov ah,2a
>> sMM.d echo int 21
>> sMM.d echo mov cx,0
>> sMM.d echo mov cl,dh
>> sMM.d echo.
>> sMM.d echo p=100 4
>> sMM.d echo n sizeMM
>> sMM.d echo w
>> sMM.d echo q
debug < sMM.d > nul
:: OK

:: DD getter
> sDD.d echo a 100
>> sDD.d echo mov ah,2a
>> sDD.d echo int 21
>> sDD.d echo mov cx,0
>> sDD.d echo mov cl,dl
>> sDD.d echo.
>> sDD.d echo p=100 4
>> sDD.d echo n sizeDD
>> sDD.d echo w
>> sDD.d echo q
debug < sDD.d > nul
:: OK
del *.d

for %%F in (sizeYYYY sizeMM sizeDD) do call :sub1 %%F
set /p YYYY=<sizeYYYY.#
set /p MM=<sizeMM.#
if %MM% LSS 10 set MM=0%MM%
set /p DD=<sizeDD.#
if %DD% LSS 10 set DD=0%DD%
del size*.*
echo YYYYMMDD=%YYYY%%MM%%DD%
goto :eof

:sub1
> %1.# echo %~z1
goto :eof
:: DONE



If at first you don't succeed, you're about average.

M2


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: Batch -change month # to letters

v.c++changing cstring to char array www.computing.net/answers/programming/vcchanging-cstring-to-char-array/14282.html

Batch change a char after nth pos www.computing.net/answers/programming/batch-change-a-char-after-nth-pos/20274.html

Batch file: Adding to file names www.computing.net/answers/programming/batch-file-adding-to-file-names/9098.html