我想在文件名中使用月份名称。 我从sql server导出数据,如果月份是3,那么文件的名称应该使用系统日期然后它应该打印文件名为febact,如果月份是4那么它应该打印marchact。
谢谢, 拉维。
答案 0 :(得分:6)
WMIC似乎不适用于我的Win XP - 它不包含在XP家庭版中。
这不是最优雅的解决方案,但应该可行。
@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%
好一点:
@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%
答案 1 :(得分:6)
您尚未提供文件名的格式。下面的批处理文件只转换当前日期的月份:
@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%
如果您希望月份名称具有恒定长度(即:3个字母):
@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%
安东尼奥
答案 2 :(得分:2)
我知道,这个问题很老了。但是,我不喜欢不必要的循环或多个case if语句,只有一个可以有资格。您可能想尝试这样做:
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
对于 mo ,你会得到1 ... 12, pos 索引0,3 ... 33到字符串" JanFeb ...... Dec",并且,假设它是May( mo = 5), ti 将输出:
May
希望它仍有帮助,如果不是OP,那么其他人。
注意:非地区独立,您必须知道从哪里选择您的月份数字。
答案 3 :(得分:1)
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%
答案 4 :(得分:1)
我为@aacini和他们的答案工作了。我绕过了第二个FOR循环并直接进入了月份代码。
@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
答案 5 :(得分:0)
试试这个(希望独立于本地设置):
@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
答案 6 :(得分:0)
在AjV Jsy的帖子上备用小猪,这是我用来设置上个月名称的变量。我在批处理文件中使用此代码来创建一个名为上个月的新文件夹 - 我将其用于备份和自动月末报告。
@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