使用批处理创建文件夹和子文件夹

时间:2013-07-30 16:10:14

标签: batch-file

我目前正在尝试使用批处理文件创建包含子文件夹的文件夹 - 我使用下面的代码但它不起作用 - txt文件有一个主文件夹列表,然后其他行是应该的子文件夹在每个文件夹中创建。

@echo off
for /F "tokens=*" %%* in (jobid.txt) 
do(mkdir 
"C:\joblist\%%*" 
"C:\joblist\%%*\Loss Sheet" 
"C:\joblist\%%*\Authorization" 
"C:\joblist\%%*\Certificate of Satisfaction"
"C:\joblist\%%*\Drying Lobs-Outlines"
"C:\joblist\%%*\Lead Documentation"
"C:\joblist\%%*\Photos"
"C:\joblist\%%*\Waiver of Liability"
"C:\joblist\%%*\Mold Reports-Documentation"
"C:\joblist\%%*\Bank-Mortgage Doc"
"C:\joblist\%%*\Invoice"
"C:\joblist\%%*\Payment-Checks"
"C:\joblist\%%*\Job Costs-Vendors"
"C:\joblist\%%*\Emails"
"C:\joblist\%%*\Notes"
)

当我运行批处理文件时没有任何反应并且没有创建文件夹

2 个答案:

答案 0 :(得分:2)

试试这个:

@ECHO OFF &SETLOCAL
for /F "delims=" %%a in (jobid.txt) DO (
    FOR  %%x IN (
    "C:\joblist\%%a"
    "C:\joblist\%%a\Loss Sheet"
    "C:\joblist\%%a\Authorization"
    "C:\joblist\%%a\Certificate of Satisfaction"
    "C:\joblist\%%a\Drying Lobs-Outlines"
    "C:\joblist\%%a\Lead Documentation"
    "C:\joblist\%%a\Photos"
    "C:\joblist\%%a\Waiver of Liability"
    "C:\joblist\%%a\Mold Reports-Documentation"
    "C:\joblist\%%a\Bank-Mortgage Doc"
    "C:\joblist\%%a\Invoice"
    "C:\joblist\%%a\Payment-Checks"
    "C:\joblist\%%a\Job Costs-Vendors"
    "C:\joblist\%%a\Emails"
    "C:\joblist\%%a\Notes"
    ) DO (
    ECHO MD "%%~x"
    )
)

如果看起来不错,请查看输出并删除echo之前的单词MD

Tihs是一个很好的问题,我喜欢它。

答案 1 :(得分:0)

我认为您不了解for中循环变量的使用。 *在命令窗口(DOS框)中具有特殊含义;这是一张通配符。您还需要为每个文件夹重复命令mkdir,而不仅仅是第一个文件夹。

请尝试使用实际的变量名称:

@echo off
for /F "tokens=*" %%i in (jobid.txt) do (
  mkdir "C:\joblist\%%*" 
  mkdir "C:\joblist\%%*\Loss Sheet" 
  mkdir "C:\joblist\%%*\Authorization" 
  mkdir "C:\joblist\%%*\Certificate of Satisfaction"
  mkdir "C:\joblist\%%*\Drying Lobs-Outlines"
  mkdir "C:\joblist\%%*\Lead Documentation"
  mkdir "C:\joblist\%%*\Photos"
  mkdir "C:\joblist\%%*\Waiver of Liability"
  mkdir "C:\joblist\%%*\Mold Reports-Documentation"
  mkdir "C:\joblist\%%*\Bank-Mortgage Doc"
  mkdir "C:\joblist\%%*\Invoice"
  mkdir "C:\joblist\%%*\Payment-Checks"
  mkdir "C:\joblist\%%*\Job Costs-Vendors"
  mkdir "C:\joblist\%%*\Emails"
  mkdir "C:\joblist\%%*\Notes"
)

(正如注意,mdmkdir的简写,因此您可以用mkdir替换所有md命令,而不是每行保存三次击键。无论哪种方式都相同,更长的名称可能更清楚,以备将来参考。)