Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 208 Vote(s) - 3.39 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to print month name in file name by using bat

#1
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.
Reply

#2
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%
Reply

#3
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
Reply

#4
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



Reply

#5
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
Reply

#6
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%
Reply

#7
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
Reply

#8
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.
Reply

#9
Or run a powershell command to format the date:

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



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through