修改后批处理文件不起作用

修改后批处理文件不起作用

我有一个批处理文件,用于在其他客户端文件夹中创建文件夹。我添加了一个额外的文件夹,现在这个批处理文件不起作用了。我是不是漏掉了什么?

@echo off

set Dir=y:\(Directory of companies)
set Year=(The year I want the folders added to)


setlocal enabledelayedexpansion

for /f "delims=" %%D in ('dir /ad/b !Dir!') do (
    if not exist "!Dir!\%%D\!Year!" (
    md "!Dir!\%%D\!Year!"
    md "!Dir!\%%D\!Year!\Tax"
    md "!Dir!\%%D\!Year!\Tax\Estimated_Tax"
    md "!Dir!\%%D\!Year!\Tax\Info_for_tax_return"  
    md "!Dir!\%%D\!Year!\Year_End_Planning"
    )
  )
)

Info_for_tax_return是我添加的新行,导致 bat 文件停止工作。感谢您的帮助。谢谢

答案1

您的块中的右括号比左括号多for

for /f "delims=" %%D in ('dir /ad/b !Dir!') do ( **<-1**
    if not exist "!Dir!\%%D\!Year!" ( **<-2**
    md "!Dir!\%%D\!Year!"
    md "!Dir!\%%D\!Year!\Tax"
    md "!Dir!\%%D\!Year!\Tax\Estimated_Tax"
    md "!Dir!\%%D\!Year!\Tax\Info_for_tax_return"  
    md "!Dir!\%%D\!Year!\Year_End_Planning"
    ) **<-1**
  ) **<-2**
) **<-3**

删除多余的右括号并重试:

for /f "delims=" %%D in ('dir /ad/b !Dir!') do (
    if not exist "!Dir!\%%D\!Year!" (
        md "!Dir!\%%D\!Year!"
        md "!Dir!\%%D\!Year!\Tax"
        md "!Dir!\%%D\!Year!\Tax\Estimated_Tax"
        md "!Dir!\%%D\!Year!\Tax\Info_for_tax_return"  
        md "!Dir!\%%D\!Year!\Year_End_Planning"
    )
)

答案2

笔记:

  • setlocal disabledelayedexpansion因为你不需要启用延迟扩展
  • set "Year=2015"用双引号括起来,以确保没有额外的不需要的空格
  • set "Dir=y:\Directory of companies"以及
  • md "%Dir%\%%D\%Year%" 2>nul如果目标目录存在,则隐藏错误消息(将其重定向到 hell with 2>nul)。因此您不需要测试它是否存在。
  • if not exist "!Dir!\%%D\!Year!" (...)不允许创建目录 whed 添加了一行并启动了多次......

该脚本应该适合您。

@echo off
setlocal disabledelayedexpansion

set "Dir=y:\Directory of companies"
set "Year=2015"
:: (The year I want the folders added to)

for /f "delims=" %%D in ('dir /ad/b %Dir%') do (
    md "%Dir%\%%D\%Year%" 2>nul
    md "%Dir%\%%D\%Year%\Tax" 2>nul
    md "%Dir%\%%D\%Year%\Tax\Estimated_Tax" 2>nul
    md "%Dir%\%%D\%Year%\Tax\Info_for_tax_return" 2>nul  
    md "%Dir%\%%D\%Year%\Year_End_Planning" 2>nul
)
endlocal

相关内容