我正在尝试创建一个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"
)
这显然也不起作用。
有人可以就如何产生预期结果向我提供帮助吗?
感谢您的帮助。
此致 斯科特
答案 0 :(得分:1)
for /r %location% %f in ("dir %location% /a:D") do (
您将for /r
(递归处理)的语法与for /f
(文件处理/命令替换)的语法混合在一起。此外,如果您在批处理文件中运行此操作,则需要将循环变量%
上的%%f
加倍。{/ p>
如果要直接在%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"
)
答案 1 :(得分: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)