0

我正在尝试自动化我的文件夹树创建,在那里我请求用户年份(可能不止一个),然后我在年份文件夹中创建从 01 到 12(1 月到 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 中。

4

1 回答 1

0
@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 /?从提示中查看有关本质上的文档,for /L依次设置为范围内的每个数字for /L %%x in (startnumber,increment,endnumber)%%x

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

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

然后,所需要做的就是创建目录 - 通过将基本目录名称连接srcpath到月份编号的最后 2 个字符monthnumber和叶名称%%t中,从.~"%%t

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

于 2019-08-25T00:03:44.830 回答