0Day Forums
how to print month name in file name by using bat - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: .bat & .wsf & .cmd (https://0day.red/Forum-bat-wsf-cmd)
+--- Thread: how to print month name in file name by using bat (/Thread-how-to-print-month-name-in-file-name-by-using-bat)



how to print month name in file name by using bat - antigonus521230 - 07-23-2023

I want to use the month name in the file name.
i am exporting the data from sql server the name of the file should use the system date if the month is 3 three then it should print the file name as febact,if the month is 4 then it should print marchact.

Thanks,
Ravi.


RE: how to print month name in file name by using bat - jennawhp - 07-23-2023

WMIC doesn't seem to work on my Win XP - it's not included in XP Home edition.

This isn't the most elegant solution, but should work.

@echo off
rem 0,2 for mm/dd/yyyy or 3,2 for dd/mm/yyyy
set month-num=%date:~3,2%
if %month-num%==01 set mo-name=jan
if %month-num%==02 set mo-name=feb
if %month-num%==03 set mo-name=mar
if %month-num%==04 set mo-name=apr
if %month-num%==05 set mo-name=may
if %month-num%==06 set mo-name=jun
if %month-num%==07 set mo-name=jul
if %month-num%==08 set mo-name=aug
if %month-num%==09 set mo-name=sep
if %month-num%==10 set mo-name=oct
if %month-num%==11 set mo-name=nov
if %month-num%==12 set mo-name=dec
echo build filename using %mo-name%

A bit better :

@echo off
rem 0,2 for mm/dd/yyyy or 3,2 for dd/mm/yyyy
set month-num=%date:~3,2%
rem remove any leading zero :
IF "%month-num:~0,1%"=="0" SET month-num=%month-num:~1%
FOR /f "tokens=%month-num%" %%a in ("jan feb mar apr may jun jul aug sep oct nov dec") do set mo-name=%%a
echo build filename using %mo-name%



RE: how to print month name in file name by using bat - lick458844 - 07-23-2023

Try this (hopefully independent from local settings):

@echo off &setlocal
for /f "tokens=2*" %%a in ('reg query "HKCU\Control Panel\International" /v sShortDate^|find "REG_SZ"') do set "ssShortDate=%%b"
reg add "HKCU\Control Panel\International" /f /v sShortDate /d "ddd MMM" >nul
set "dowlm=%date%"
reg add "HKCU\Control Panel\International" /f /v sShortDate /d "dd MM yyyy" >nul
set "cdate=%date%"
reg add "HKCU\Control Panel\International" /f /v sShortDate /d "%ssShortDate%" >nul
for /f "tokens=1-2" %%i in ("%dowlm%") do set "dow=%%i"&set "lmonth=%%j"
for /f "tokens=1-3" %%i in ("%cdate%") do set "day=0%%i"&set "month=0%%j"&set "year=%%k"
set "day=%day:~-2%"
set "month=%month:~-2%"
echo.%dow%, %day%.%lmonth%.%year%
endlocal
Pause


RE: how to print month name in file name by using bat - Sircavetto1 - 07-23-2023

Piggy-backing on AjV Jsy's post, here is what I use to set a variable for the name of the previous month. I use this code in a batch file to create a new folder with the name of the previous month - I use this for backups and automated month end reporting.

@echo off
set Year=%date:~10,4%
set Month=%date:~4,2%
:: This script sets the variable "PrvMonth" to display the name of the previous month.
if %Month%==01 set PrvMonth=December

if %Month%==02 set PrvMonth=January

if %Month%==03 set PrvMonth=February

if %Month%==04 set PrvMonth=March

if %Month%==05 set PrvMonth=April

if %Month%==06 set PrvMonth=May

if %Month%==07 set PrvMonth=June

if %Month%==08 set PrvMonth=July

if %Month%==09 set PrvMonth=August

if %Month%==10 set PrvMonth=September

if %Month%==11 set PrvMonth=October

if %Month%==12 set PrvMonth=November






RE: how to print month name in file name by using bat - dathaxjnojmzmo - 07-23-2023

You have not provided the format of your file name. The Batch file below just converts the month of current date:

@echo off
setlocal EnableDelayedExpansion
set m=100
for %%m in (January February March April May June July August September October November December) do (
set /A m+=1
set month[!m:~-2!]=%%m
)
rem Change tokens=2 for DD/MM/YYYY date format
for /F "tokens=1 delims=/" %%m in ("%date%") do (
set monthName=!month[%%m]!
)
echo %monthName%

If you want month name have constant length (ie: 3 letters):

@echo off
setlocal EnableDelayedExpansion
for /F "tokens=1 delims=/" %%m in ("%date%") do (
set /A "m=(1%%m%%100-1)*3"
)
set month=JanFebMarAprMayJunJulAugSepOctNovDec
set monthName=!month:~%m%,3!
echo %monthName%


Antonio



RE: how to print month name in file name by using bat - katzen371 - 07-23-2023

cls
@ECHO OFF
TITLE PRINT_MONTH NAME

rem To get valuse from local machine

for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set year=%dt:~0,4%
set month=%dt:~4,2%
set day=%dt:~6,2%

rem set month name for the corresponding numbers

if %month%==01 set monthname=JANUARY
if %month%==02 set monthname=FEBRUARY
if %month%==03 set monthname=MARCH
if %month%==04 set monthname=APRIL
if %month%==05 set monthname=MAY
if %month%==06 set monthname=JUNE
if %month%==07 set monthname=JULY
if %month%==08 set monthname=AUGUST
if %month%==09 set monthname=SEPTEMBER
if %month%==10 set monthname=OCTOBER
if %month%==11 set monthname=NOVEMBER
if %month%==12 set monthname=DECEMBER

rem print month name

echo %monthname%


RE: how to print month name in file name by using bat - jobinaehir - 07-23-2023

I worked off of @aacini and their answer. I just bypassed the second FOR loop and went straight for the month code.

@echo off
setlocal EnableDelayedExpansion

set m=100
for %%m in (January
February
March
April
May
June
July
August
September
October
November
December
) do (
set /a m+=1
set month[!m:~-2!]=%%m
)

set monthNow=%date:~3,3%
set monthNow=%monthNow: =%
set monthName=!month[%monthNow%]!

echo %monthName%

pause


RE: how to print month name in file name by using bat - Progez745 - 07-23-2023

I know, that this question is pretty old. However, I do not like unneccessary loops or multiple case if statements when only one can qualify anyway. You might want to try this instead:

setlocal ENABLEDELAYEDEXPANSION

set "mo=%date:~3,2%"
if "%mo:~0,1%"=="0" set "mo=%mo:~1%"
set names=JanFebMarAprMayJunJulAugSepOctNovDec
set /a "pos = 3 * %mo%" - 3
set "ti=!names:~%pos%,3!"

echo %ti%
pause

For *mo* you will get 1...12, for *pos* an index 0,3...33 into the string "JanFeb...Dec", and, assuming it is May (*mo*=5), *ti* will then output:

May

Hope it still helps, if not the OP, then someone else.

**Note:** not locale independent, you must know where from to pick your month digits.


RE: how to print month name in file name by using bat - compliancy355 - 07-23-2023

Or run a powershell command to format the date:

`for /f "tokens=*" %%i in ('PowerShell -Command "Get-Date -format 'ddMMMyyyy'"') do echo %%i`