0

我正在尝试创建一个 Windows 命令行脚本,它允许我在网络驱动器上存在的每个文件夹下重新创建一个目录结构。我最初是在 CMD 中创建代码,然后在获得所需结果时将其转换为批处理文件。

目前我有以下内容:

set location=\\READYSHARE\USB_Storage\Global

for /r %location% %f in ("dir %location% /a:D") do (

MD "%location%\Archived Tenants"
MD "%location%\Current tenant -"
MD "%location%\Current tenant -\Tenancy Sign up Pack"
MD "%location%\Owners\"
MD "%location%\Owners\Advertising Photos"
MD "%location%\Owners\Invoices"
MD "%location%\Owners\Quotes"
MD "%location%\Owners\Routine Inspections"
)

显然,这并没有给我想要的输出,因为文件夹是在 \READYSHARE\USB_Storage\Global 下创建的,而不是为 \READYSHARE\USB_Storage\Global 中包含的所有子目录创建的。所以后来我把它改成:

set location=\\READYSHARE\USB_Storage\Global

for /r %location% %f in ("dir %location% /a:D") do (

MD "%f\Archived Tenants"
MD "%f\Current tenant -"
MD "%f\Current tenant -\Tenancy Sign up Pack"
MD "%F\Owners\"
MD "%f\Owners\Advertising Photos"
MD "%f\Owners\Invoices"
MD "%f\Owners\Quotes"
MD "%f\Owners\Routine Inspections"
)

这显然也不起作用。

有人可以为我提供有关如何产生所需结果的帮助吗?

感谢您的帮助。

问候,斯科特

4

2 回答 2

2
for /r %location% %f in ("dir %location% /a:D") do (

您将for /r(递归处理)的语法与for /f(文件处理/命令替换)的语法混为一谈。此外,如果您在批处理文件中运行它,则需要%将循环变量 ( %%f) 加倍。

如果要直接在下面创建文件夹结构,%location%请执行以下操作:

@echo off

setlocal

set "location=\\READYSHARE\USB_Storage\Global"

md "%location%\Archived Tenants"
md "%location%\Current tenant -"
md "%location%\Current tenant -\Tenancy Sign up Pack"
md "%location%\Owners\"
md "%location%\Owners\Advertising Photos"
md "%location%\Owners\Invoices"
md "%location%\Owners\Quotes"
md "%location%\Owners\Routine Inspections"

如果要在所有顶级文件夹下创建文件夹结构,%location%请执行以下操作:

@echo off

setlocal

set "location=\\READYSHARE\USB_Storage\Global"

for /d %%d in (%location%\*) do (
  md "%%d\Archived Tenants"
  md "%%d\Current tenant -"
  md "%%d\Current tenant -\Tenancy Sign up Pack"
  md "%%d\Owners\"
  md "%%d\Owners\Advertising Photos"
  md "%%d\Owners\Invoices"
  md "%%d\Owners\Quotes"
  md "%%d\Owners\Routine Inspections"
)

如果要在%location%(包括它们的子文件夹)的所有子文件夹下创建文件夹结构,请执行以下操作:

@echo off

setlocal

set "location=\\READYSHARE\USB_Storage\Global"

for /f "delims=" %%d in ('dir /b /a:d /s "%location%"') do (
  md "%%d\Archived Tenants"
  md "%%d\Current tenant -"
  md "%%d\Current tenant -\Tenancy Sign up Pack"
  md "%%d\Owners\"
  md "%%d\Owners\Advertising Photos"
  md "%%d\Owners\Invoices"
  md "%%d\Owners\Quotes"
  md "%%d\Owners\Routine Inspections"
)
于 2013-08-05T07:50:02.407 回答
1

您可以使用 FOR 命令和 /F 选项来解析 DIR 命令的输出,然后使用 %a 变量作为子目录的名称。

for /f %a in ('dir /a:D /B %location%') do (
  MD "%location%\%a\Archived Tenants"
  MD "%location%\%a\Current tenant -"
  MD "%location%\%a\Current tenant -\Tenancy Sign up Pack"
  MD "%location%\%a\Owners\"
  MD "%location%\%a\Owners\Advertising Photos"
  MD "%location%\%a\Owners\Invoices"
  MD "%location%\%a\Owners\Quotes"
  MD "%location%\%a\Owners\Routine Inspections"
)

注意:当您将其翻译成批处理文件时,您需要复制变量名称中的 % (%a > %%a)

于 2013-08-05T07:55:11.593 回答