简体   繁体   中英

batch script to copy all usr files from sub folders to separate folders

I'm trying to create a batch script which will copy all usr files from the sub folders inside a directory into 2 separate folders and then delete the files from their original places. So far I have done:

for /R S:\Customer Services\EDI\NET EDI %%f in (*.usr) do copy T:\edidata\netedi\Outboxtest

for /R S:\Customer Services\EDI\NET EDI %%f in (*.usr) do copy T:\edidata\netedi\Outbox backup

This however is not copying the files and nothing is happening, where have I gone wrong here? And what would I need to add to delete the files from their original folders as well?

Edit:

From how I was reading the answers I thought it should become:

for /R "S:\Customer Services\EDI\NET EDI" %%f in (*.usr) do copy %%F "T:\edidata\netedi\Outboxtest"

for /R "S:\Customer Services\EDI\NET EDI" %%f in (*.usr) do copy %%F "T:\edidata\netedi\Outbox backup"

but this still is not working. I'm guessing I just haven't understood properly what was being said

Edit 2:

I added the PAUSE command at the end of the script to see what was happening when it was run and this is what comes up:

S:\Customer Services\EDI\NET EDI>for /R "S:\Customer Services\EDI\NET EDI" %f in (*.usr) do copy %F "T:\edidata\netedi\Outboxtest"

S:\Customer Services\EDI\NET EDI>copy %F "T:\edidata\netedi\Outboxtest"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy %F "T:\edidata\netedi\Outboxtest"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy %F "T:\edidata\netedi\Outboxtest"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy %F "T:\edidata\netedi\Outboxtest"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy %F "T:\edidata\netedi\Outboxtest"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy %F "T:\edidata\netedi\Outboxtest"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy %F "T:\edidata\netedi\Outboxtest"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>for /R "S:\Customer Services\EDI\NET EDI" %f in (*.usr) do copy %F "T:\edidata\netedi\Outbox backup"

S:\Customer Services\EDI\NET EDI>copy %F "T:\edidata\netedi\Outbox backup"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy %F "T:\edidata\netedi\Outbox backup"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy %F "T:\edidata\netedi\Outbox backup"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy %F "T:\edidata\netedi\Outbox backup"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy %F "T:\edidata\netedi\Outbox backup"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy %F "T:\edidata\netedi\Outbox backup"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy %F "T:\edidata\netedi\Outbox backup"
The system cannot find the file specified.

Hopefully the last edit.

Now I've got

for /R "S:\Customer Services\EDI\NET EDI" %%G in (*.usr) do copy %%G "T:\edidata\NetEDI\Outboxtest"

for /R "S:\Customer Services\EDI\NET EDI" %%G in (*.usr) do copy %%G "T:\edidata\NetEdi\Outbox backup"

PAUSE

but it's still not working right, the output given is:

S:\Customer Services\EDI\NET EDI>for /R "S:\Customer Services\EDI\NET EDI" %G in (*.usr) do copy %G "T:\edidata\NetEDI\Outboxtest"

S:\Customer Services\EDI\NET EDI>copy S:\Customer Services\EDI\NET EDI\gentest.usr "T:\edidata\NetEDI\Outboxtest"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy S:\Customer Services\EDI\NET EDI\Asda\asdatest.usr "T:\edidata\NetEDI\Outboxtest"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy S:\Customer Services\EDI\NET EDI\Costcutter\CCINV24052020.usr "T:\edidata\NetEDI\Outboxtest"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy S:\Customer Services\EDI\NET EDI\Costcutter\cctest.usr "T:\edidata\NetEDI\Outboxtest"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy S:\Customer Services\EDI\NET EDI\McColls Local Account\mccollstest.usr "T:\edidata\NetEDI\Outboxtest"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy S:\Customer Services\EDI\NET EDI\Scotmid\scotmidtest.usr "T:\edidata\NetEDI\Outboxtest"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy S:\Customer Services\EDI\NET EDI\Tesco\tescotest.usr "T:\edidata\NetEDI\Outboxtest"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>for /R "S:\Customer Services\EDI\NET EDI" %G in (*.usr) do copy %G "T:\edidata\NetEdi\Outbox backup"

S:\Customer Services\EDI\NET EDI>copy S:\Customer Services\EDI\NET EDI\gentest.usr "T:\edidata\NetEdi\Outbox backup"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy S:\Customer Services\EDI\NET EDI\Asda\asdatest.usr "T:\edidata\NetEdi\Outbox backup"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy S:\Customer Services\EDI\NET EDI\Costcutter\CCINV24052020.usr "T:\edidata\NetEdi\Outbox backup"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy S:\Customer Services\EDI\NET EDI\Costcutter\cctest.usr "T:\edidata\NetEdi\Outbox backup"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy S:\Customer Services\EDI\NET EDI\McColls Local Account\mccollstest.usr "T:\edidata\NetEdi\Outbox backup"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy S:\Customer Services\EDI\NET EDI\Scotmid\scotmidtest.usr "T:\edidata\NetEdi\Outbox backup"
The system cannot find the file specified.

S:\Customer Services\EDI\NET EDI>copy S:\Customer Services\EDI\NET EDI\Tesco\tescotest.usr "T:\edidata\NetEdi\Outbox backup"
The system cannot find the file specified.

So what I'm gathering is that it is now getting the files but, it can't find the folders to copy them to? The file destination is correct for them both, I've double-checked so not sure what else could be wrong.

There are at least three problems in your commands.

  1. You are providing just one argument to the copy command. It needs source and destination . You probably forgot to use the %%F argument provided by for .

     for /R <path> %%f in (*.usr) do copy %%FT:\edidata\netedi\Outboxtest
  2. Another problem is that your path contains spaces and you are not escaping them. You should surround your path by double quotes:

     "S:\Customer Services\EDI\NET EDI"

    The same problem most likely applies to T:\edidata\netedi\Outbox backup . The current command

    for /RS:\Customer Services\EDI\NET EDI %%f in (*.usr) do copy T:\edidata\netedi\Outbox backup

    would copy from T:\edidata\netedi\Outbox to .\backup .

    You must use quotes for your parameter, too, as it expands to a path containing spaces in your case!

     ... do copy "%%f" T:\edidata\netedi\Outboxtest

    It's generally better to use underscores ( _ ) or hyphens ( - ) instead of spaces in paths, then you don't need to escape the spaces in each command.

  3. The %%f argument is case-sensitive , it cannot be referenced as %%F .

I believe you should do:

... copy %%f T:\edidata\netedi\Outboxtest\ (or ...Outbox\test\)

... and what's the story of T:\edidata\netedi\Outbox backup? Shouldn't that be:

... copy %%f T:\edidata\netedi\Outbox\backup\

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM