Reputation: 462
I am attempting to create a windows command line script that will allow me to re-create a directory structure under each of the folders that exist on my network drive. I am initially creating the code in CMD and will then convert it to a batch file when I have the desired outcome.
At present I have the following:
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"
)
Obviously this doesn't give me the desired output because the folders are being created under \READYSHARE\USB_Storage\Global and not for all sub directories contained within \READYSHARE\USB_Storage\Global. So then I changed this to:
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"
)
Which obviously doesn't work either.
Can someone please offer me assistance on how to produce the desired result?
Thank you for your help.
Regards, Scott
Upvotes: 1
Views: 6360
Reputation: 200573
for /r %location% %f in ("dir %location% /a:D") do (
You're mixing up the syntax of for /r
(recursive processing) with the syntax of for /f
(file processing/command substitution). Also, if you're running this in a batch file you need to double the %
on the loop variable (%%f
).
If you want to create the folder structure directly under %location%
do this:
@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"
If you want to create the folder structure under all top-level folders of %location%
do this:
@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"
)
If you want to create the folder structure under all subfolders of %location%
(including their subfolders) do this:
@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"
)
Upvotes: 2
Reputation: 435
You can use FOR command with /F option to parse the output of DIR command, and then use the %a variable as the name of the subdirectory.
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"
)
Note: when you will translate this into batch file you'll need to duplicate the % in the variable name (%a > %%a)
Upvotes: 1