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