我一直在尝试创建一个基本的批处理脚本,该脚本将创建当前年份的文件夹(例如 2014 年),然后创建当前月份的子文件夹(6 月等...)。建立这些后,我需要根据林中的不同域(例如 Homer、Janeway、Autobots 域)创建文件夹。
当我输入日期时,我得到以下信息(在我们所有的系统上都是相同的):
2014 年 6 月 30 日星期一
这是我到目前为止的代码:
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\六月\荷马
可能有更好的方法,但我相信这会起作用;
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
这就是我会做的方式:
@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
)
有关阵列管理的更多详细信息,请参阅这篇文章。
另一种方式
@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 根据月份编号检索正确的行),并且对于每个域,相应的文件夹是创建的。
您的原始代码有一组额外的不需要的百分比。例如,
md %yearfoldername%\%%month%%\Homer
应改为 md %yearfoldername%\%month%\Homer
。
注意MD命令可以用一条命令创建多个文件夹。您不需要以下内容:
md %yearfoldername%
md %yearfoldername%\%month%
因为下面的命令将创建上述两个文件夹。
md %yearfoldername%\%month%\Homer
根据配置,您对 DATE 命令的解析可能会在某些计算机上失败,因为日期取决于区域设置。听起来您已经意识到这一点,因为您声明“(在我们所有系统上都是相同的)”。
我编写了一个名为 getTimestamp.bat 的混合 JScript/批处理实用程序,它使得在任何 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
您可以尝试使用以下代码片段创建文件夹,首先按年份创建文件夹,然后按每年的月份创建文件夹,并进一步在每个月份文件夹中放置具有某些名称(如 A、B、C、D)的文件夹。
@echo off
REM Creates year-wise folders (from 2023 to 2030)
REM Change the directory link to your set directory after mkdir
for /l %%y in (2023,1,2030) do (
mkdir "X:\......\Yearwise\%%y"
REM Creates month folders inside each year folder and again Folder named A, B, C, D inside each Month Folder
for %%m in (January February March April May June July August September October November December) do (
for %%b in (A B C D) do (
mkdir "X:\......\Yearwise\%%y\%%m\%%b"
)
)
)
如果您不需要子文件夹 A B C D,只需将上面的行更改为:
for %%m in (January February March April May June July August September October November December) do (
mkdir "X:\......\Yearwise\%%y\%%m\%%b"
)
)
太棒了!我在这里看到了答案,一切对我来说都很有用。在我回到这里查看回复之前,我最终解决了这个问题。非常感谢大家的帮助。我仍在学习这些东西,所以所有的回复都向我展示了不同的做事方式。这是我最终所做的(基于我有限的知识,所以代码很冗长):
@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