我正在尝试自动创建文件夹树,在其中我要求用户输入年份(可以是多个),然后在Year文件夹中创建01到12(一月到十二月)的文件夹。在每个月的文件夹中,我可以根据需要创建另一个文件夹,以保持组织结构。
当我在Windows环境中运行.bat脚本时,收到命令语法错误,但是查看了整个脚本,但找不到错误出在哪里。我认为我想做的事情并不那么困难,因此我将整个脚本放在下面,以为您具有读写脚本的基本知识,就能使社区的人们理解我想做的事情。 / p>
@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中存储脚本所在的路径。
答案 0 :(得分: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
与月份号monthnumber
和叶名%%t
的后2个字符连接在一起来构建目录~
从"
的字符串中删除引号%%t
。
在2>nul
命令后附加mkdir
以禁止显示错误消息(例如,当目录已经存在时)