我一直在尝试创建一个基本的批处理脚本,该脚本将为当前年份(例如2014年)创建文件夹,然后创建当月(6月等)的子文件夹。一旦建立起来,我需要根据我们森林中的不同域创建文件夹(例如Homer,Janeway,Autobots域)。
当我输入日期时,我得到以下内容(它在我们所有的系统中都是相同的):
星期一06/30/2014
这是我到目前为止的代码:
set yearfoldername=%date:~-4%
set month=%date:~-10,2%
set 01=January
set 02=February
set 03=March
set 04=April
set 05=May
set 06=June
set 07=July
set 08=August
set 09=September
set 10=October
set 11=November
set 12=December
md %yearfoldername%
md %yearfoldername%\%%month%%
md %yearfoldername%\%%month%%\Homer
md %yearfoldername%\%%month%%\Janeway
md %yearfoldername%\%%month%%\Autobots
就目前而言,它只会创建如下所示的文件夹: 2014 \%,当月%\荷马 代替 2014 \六月\荷马
答案 0 :(得分:2)
可能有更好的方法,但我相信这会有效;
set yearfoldername=%date:~-4%
set month=%date:~-10,2%
if "%month%" EQU "01" set asd=January
if "%month%" EQU "02" set asd=February
if "%month%" EQU "03" set asd=March
if "%month%" EQU "04" set asd=April
if "%month%" EQU "05" set asd=May
if "%month%" EQU "06" set asd=June
if "%month%" EQU "07" set asd=July
if "%month%" EQU "08" set asd=August
if "%month%" EQU "09" set asd=September
if "%month%" EQU "10" set asd=October
if "%month%" EQU "11" set asd=November
if "%month%" EQU "12" set asd=December
md %yearfoldername%
md %yearfoldername%\%asd%
md %yearfoldername%\%asd%\Homer
md %yearfoldername%\%asd%\Janeway
md %yearfoldername%\%asd%\Autobots
答案 1 :(得分:1)
这就是我这样做的方式:
@echo off
setlocal EnableDelayedExpansion
rem Define the array of month names
set m=100
for %%a in (January February March April May June
July August September October November December) do (
set /A m+=1
set monthName[!m:~-2!]=%%a
)
for /F "tokens=2,4 delims=/ " %%a in ("%date%") do (
set yearFolderName=%%b
set month=%%a
)
md %yearFolderName%
md %yearFolderName%\!monthName[%month%]!
for %%a in (Homer Janeway Autobots) do (
md %yearFolderName%\!monthName[%month%]!\%%a
)
有关阵列管理的更多详细信息,请参阅this post。
答案 2 :(得分:1)
另一种方式
@echo off
setlocal enableextensions disabledelayedexpansion
:: Data section : table of months
::month:01:January
::month:02:February
::month:03:March
::month:04:April
::month:05:May
::month:06:June
::month:07:July
::month:08:August
::month:09:September
::month:10:October
::month:11:November
::month:12:December
:: Code Section
for /f "tokens=2,4 delims=/ " %%a in ( "%date%"
) do for /f "tokens=3 delims=: " %%c in ( 'findstr /l /c:"::month:%%a" "%~f0"'
) do for %%d in ( Homer Janeway Autobots
) do md "%%b\%%c\%%d"
endlocal
它拆分日期以检索必填字段,从批处理文件中声明的数据部分中检索字段名称(使用findstr根据月份编号检索正确的行),并为每个字段创建相应文件夹的域。
答案 3 :(得分:0)
真棒!我在这里看到了答案,所有这些对我来说都很有用。在我回到这里查看回复之前,我最终解决了这个问题。非常感谢大家的帮助。我还在学习这些东西,所以所有回复都向我展示了不同的做事方式。这就是我最终做的事情(基于我有限的知识,因此代码啰嗦):
@echo off
REM This batch file Will create a tree structure based on year, month and domain
set year=%date:~-4%
:Month-Name-Creation
if %date:~-10,2%==01 goto Jan
if %date:~-10,2%==02 goto Feb
if %date:~-10,2%==03 goto Mar
if %date:~-10,2%==04 goto Apr
if %date:~-10,2%==05 goto Maybe
if %date:~-10,2%==06 goto Jun
if %date:~-10,2%==07 goto Jul
if %date:~-10,2%==08 goto Aug
if %date:~-10,2%==09 goto Sep
if %date:~-10,2%==10 goto Oct
if %date:~-10,2%==11 goto Nov
if %date:~-10,2%==12 goto Dec
:Jan
md %year%\January
set month=January
goto Domains
:Feb
md %year%\February
set month=February
goto Domains
:Mar
md %year%\March
set month=March
goto Domains
:Apr
md %year%\April
set month=April
goto Domains
:Maybe
md %year%\May
set Month=May
goto Domains
:Jun
md %year%\June
set month=June
goto Domains
:Jul
md %year%\July
set month=July
goto Domains
:Aug
md %year%\August
set month=August
goto Domains
:Sep
md %year%\September
set month=September
goto Domains
:Oct
md %year%\October
set month=October
goto Domains
:Nov
md %year%\November
set month=November
goto Domains
:Dec
md %year%\December
set month=December
goto Domains
:Domains
cd %year%\%month%
md Homer Janeway Autobots
再次感谢大家的回复。
/ R
TrikerII
答案 4 :(得分:0)
您的原始代码有一组额外的不需要的百分比。例如,md %yearfoldername%\%%month%%\Homer
应该是md %yearfoldername%\%month%\Homer
。
请注意,MD命令可以使用一个命令创建多个文件夹。您不需要以下内容:
md %yearfoldername%
md %yearfoldername%\%month%
因为下面的命令会创建上述两个文件夹。
md %yearfoldername%\%month%\Homer
根据配置,您在某些计算机上解析DATE命令可能会失败,因为日期取决于区域设置。听起来你知道这一点,因为你声明“(在我们所有的系统上它都是一样的)”。
我写了hybrid JScript/batch utility called getTimestamp.bat,这使得在任何Windows机器上使用日期和时间都非常方便,无论语言环境设置如何。该实用程序是纯脚本,可以在XP之后的任何Windows机器上运行。完整的文档嵌入在脚本中。
只要getTimestamp.bat位于当前目录中,或者更好,位于PATH中的某个位置,那么只需以下所有内容:
@echo off
call getTimestamp -f {yyyy}\{month} -r yrMo
for %%F in (Homer Janeway Autobots) do md "%yrMo%\%%F" 2>nul
我将错误输出重定向到NUL,以防任何文件夹已经存在。
很容易使脚本适应任何语言。这是西班牙语的解决方案:
@echo off
call getTimestamp -f {yyyy}\{month} -r yrMo -month "enero febrero marzo abril mayo junio julio agosto septiembre octubre noviembre diciembre"
for %%F in (Homer Janeway Autobots) do md "%yrMo%\%%F" 2>nul