TimeZ
TimeZ

Reputation: 3

Copy file to multiple folders with specific foldername (batch file)

I have sub folder like : c:\main\bot01\sb0102 -> sb0199 , c:\main\bot99\sb9901 -> sb9999.

How can I make a batch file to copy 1 file to every folder start with "sb" ? as there is many "sb" folders, I cant make command for every line of them like this:

xcopy "C:\test.txt" "c:\main\bot01\sb0102" /y

Upvotes: 0

Views: 2107

Answers (2)

user6811411
user6811411

Reputation:

You can use two nested counting for /l loops,

  • the first running from 101 to 199 to maintain a leading zero.
  • and using substrings to get only the last 2/4 digits what requires delayed expansion.

:: Q:\Test\2019\01\06\SO_54064719.cmd
@Echo off&SetLocal EnableDelayedExpansion

set "Sourcefile=C:\test.txt"
for /l %%B in (101,1,199) do (
    set bot=%%B
    Echo ---- bot !bot:~-2! ----
    for /l %%S in (1,1,99) do (
        Set /A sb=bot*100+%%S
        echo Copy /B /Y "%Sourcefile%" "C:\main\bot!bot:~-2!\sb!sb:~-4!"
    )
)

> Q:\Test\2019\01\06\SO_54064719.cmd
---- bot 01 ----
Copy /B /Y "C:\test.txt" "C:\main\bot01\sb0101"
Copy /B /Y "C:\test.txt" "C:\main\bot01\sb0102"
...
Copy /B /Y "C:\test.txt" "C:\main\bot01\sb0198"
Copy /B /Y "C:\test.txt" "C:\main\bot01\sb0199"
---- bot 02 ----
Copy /B /Y "C:\test.txt" "C:\main\bot02\sb0201"
Copy /B /Y "C:\test.txt" "C:\main\bot02\sb0202"
...

If the output looks OK, remove the echo in front of copy.

Just to show a powershell solution which allows a range in several levels

Get-ChildItem C:\main\bot[0-9][0-9]\sb[0-9][0-9][0-9][0-9] -Dir|ForEach-Object{
    Copy-Item C:\test.txt -Destination $_
}

To be on topic wrapped in cmdline/batch

powershell -NoP -C "Get-ChildItem C:\main\bot[0-9][0-9]\sb[0-9][0-9][0-9][0-9] -Dir|ForEach-Object{Copy-Item C:\test.txt -Destination $_}"

Upvotes: 2

Compo
Compo

Reputation: 38719

Here's my comment as an answer

From a batch file:

@For /D %%A In ("C:\main\bot01\sb*") Do @Copy /Y "C:\test.txt" "%%A" >Nul 2>&1

From the Command Prompt:

For /D %A In ("C:\main\bot01\sb*") Do @Copy /Y "C:\test.txt" "%A" >Nul 2>&1

Upvotes: 0

Related Questions