用于在批处理脚本中自动创建文件夹树的语句结构

Statements structure to automate folder-tree creation in a batch script

我正在尝试自动创建文件夹树,我在其中请求用户年份(可以超过一年),然后我在 01 到 12(一月到十二月)内创建文件夹年文件夹。在每个月的文件夹中,我可以根据需要创建另一个文件夹,以保持组织。

当我在 Windows 环境中 运行 .bat 脚本时,我收到命令语法错误,但是查看了整个脚本,我找不到错误在哪里。我认为我想做的事情并没有那么难,因此我会把整个脚本放在下面,想象一下,通过阅读和编写脚本的基本知识,来自社区的你们能够理解我想做的事情。

@echo off
setlocal EnableDelayedExpansion
setlocal EnableExtensions
PUSHD "%~dp0"
set "curpath=%~dp0"

set /p years=Insert a year (in case more than one value, years can be separated by comma.Ex.:2018,2019):

REM For each year that the user insert(separated by comma) the loop for creates a respective folder
for %%G in (%years%) do (

    set "srcpath=%curpath%%%G"
    mkdir "!srcpath!"

    set /a loopcount=12
    set /a increment=1

    REM Beginning here the loop where will create month folder(01 to 12), inside year folder
    :beginloop
        if !increment! LSS 10 (
            set "fmtinc=0!increment!"
        ) else (
            set "fmtinc=!increment!"
        )

        mkdir "!srcpath!\!fmtinc!\DCTF"
        mkdir "!srcpath!\!fmtinc!\Despesas"
        mkdir "!srcpath!\!fmtinc!\DeSTDA"
        mkdir "!srcpath!\!fmtinc!\Folha Pagamento"
        mkdir "!srcpath!\!fmtinc!\GFIP"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NF Entrada"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NF Entrada\PDF"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NF Entrada\XML"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NF Obra"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NF Saida"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NF Saida\PDF"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NF Saida\XML"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NFS Prestados"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NFS Prestados\PDF"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NFS Prestados\XML"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NFS Tomados"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NFS Tomados\Outros Municipios"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\Relatorio Faturamento Mensal"
        mkdir "!srcpath!\!fmtinc!\RAIS"
        mkdir "!srcpath!\!fmtinc!\GIA"
        mkdir "!srcpath!\!fmtinc!\SPED"
        mkdir "!srcpath!\!fmtinc!\SPED\EFD ICMS e IPI"
        mkdir "!srcpath!\!fmtinc!\SPED\EFD Contribuições"

        set /a increment+=1
        set /a loopcount-=1

        REM While the value of decrement variable loopcount not iquals to 0(zero) the loop continues creating new folder for next month, redirecting program flow of here to :beginloop
        if !loopcount! == 0 ( goto exitloop )

        goto beginloop

    :exitloop

)
PAUSE

在那种情况下,用户必须将 运行 脚本直接放在他想要创建所有新文件夹的文件夹中。为此,开始时将脚本所在的路径存储在变量 curpath 中。

@echo off
setlocal EnableExtensions EnableDelayedExpansion
PUSHD "%~dp0"
set "curpath=%~dp0"

set /p years=Insert a year (in case more than one value, years can be separated by comma.Ex.:2018,2019):

REM For each year that the user insert(separated by comma) the loop for creates a respective folder
for %%G in (%years%) do (

    set "srcpath=%curpath%%%G"
    mkdir "!srcpath!"
    rem month numbers+100
    FOR /L %%m IN (101,1,112) DO (
      SET /a monthnumber=%%m
      FOR %%t IN (
        "DCTF"
        "Despesas"
        "DeSTDA"
        "Folha Pagamento"
        "GFIP"
        "Notas Fiscais"
        "Notas Fiscais\NF Entrada"
        "Notas Fiscais\NF Entrada\PDF"
        "Notas Fiscais\NF Entrada\XML"
        "Notas Fiscais\NF Obra"
        "Notas Fiscais\NF Saida"
        "Notas Fiscais\NF Saida\PDF"
        "Notas Fiscais\NF Saida\XML"
        "Notas Fiscais\NFS Prestados"
        "Notas Fiscais\NFS Prestados\PDF"
        "Notas Fiscais\NFS Prestados\XML"
        "Notas Fiscais\NFS Tomados"
        "Notas Fiscais\NFS Tomados\Outros Municipios"
        "Notas Fiscais\Relatorio Faturamento Mensal"
        "RAIS"
        "GIA"
        "SPED"
        "SPED\EFD ICMS e IPI"
        "SPED\EFD Contribuições"
      ) DO mkdir "!srcpath!\!monthnumber:~-2!\%%~t"
    )
)

POPD

GOTO :EOF

您的主要问题是 code block(带括号的行序列)中不允许使用标签。

可以如图所示组合 setlocal 语句。

请参阅有关 for /L 的文档提示中的 for /? 本质上,for /L %%x in (startnumber,increment,endnumber) 依次将 %%x 设置为范围内的每个数字。

monthnumber 因此设置为 101..112 - 总是 3 位数;我们想要其中的最后 2 个。请注意,必须将 元变量 %%m 分配给标准环境变量以允许子字符串化。使用 set /a 是因为这是一个数字赋值,而不是字符串赋值。

for %%t 循环将每个引用的名称依次分配给 %%t

然后所需要做的就是创建目录 - 通过将基本目录名 srcpath 与月数 monthnumber 和叶子名称 %%t 的最后 2 个字符连接起来来构建] 其中 ~%%t.

中的字符串中删除了引号 "

2>nul 附加到 mkdir 命令以抑制错误消息(例如,当目录已经存在时)