XCOPY folder wildcard?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Valthero
Posts: 8
Joined: 09 Dec 2011 01:17

XCOPY folder wildcard?

#1 Post by Valthero » 09 Dec 2011 01:27

I'm stumped.
I am nearly completely new to batch files. I've been searching for two hours, but I cannot find a solution to my problem.
Here's what I'm trying to do: Use XCOPY to copy a file from a folder to another folder. However, I want to use a wildcard for folder names, specifically the underlined ones.

@echo off
xcopy /c /h /r /y /i "%drive%\Users\Valthero\AppData\Roaming\Macromedia\Flash Player\#SharedObjects\JMF4TL8L\games.mochiads.com\c\g\mud-and-blood-2\mudandblood2.swf\mnb21.sol" "C:\Backup"\
pause

Just how would I go about doing this? Using an asterisk gives me the error File not found - mnb21.sol.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: XCOPY folder wildcard?

#2 Post by orange_batch » 09 Dec 2011 03:04

You need to build an array of folders matching the first wildcard, then build another array of folders matching the second from the first array, then process that list through your xcopy command all with for loops. I'm pretty sure wildcards only work on the last part of a path.

Code: Select all

@echo off&setlocal enabledelayedexpansion

for /f "delims=" %%x in ('dir "%drive%\Users" /ad /b') do (
set /a userslen+=1
set "usersarray!userslen!=%drive%\Users\%%x"
)

for /l %%x in (1,1,%userslen%) do (
set "path=!usersarray%%x!\AppData\Roaming\Macromedia\Flash Player\#SharedObjects"
if exist "!path!\" for /f "delims=" %%y in ('dir "!path!" /ad /b') do (
set /a flashlen+=1
set "flasharray!flashlen!=!path!\%%y"
))

for /l %%x in (1,1,%flashlen%) do (
set path="!flasharray%%x!\games.mochiads.com\c\g\mud-and-blood-2\mudandblood2.swf\mnb21.sol"
if exist !path! xcopy !path! "C:\Backup" /c /h /r /y /i
)

pause

Untested.

For xcopy, I recommend using these switches for the most efficient backup:
/d /e /c /i /q /g /h /r /k /x /y
Last edited by orange_batch on 09 Dec 2011 20:08, edited 7 times in total.

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: XCOPY folder wildcard?

#3 Post by alan_b » 09 Dec 2011 03:10

I strongly recommend the use of the /L argument to list what will happen so you avoid wasting time and/or disc space when finding out how to drive XCOPY

I think your problem is NOT with XCOPY but with CMD.EXE

Even DIR objects to wild cards in paths

CD is prepared to take a chance with a wild path

Under 64 bit Windows 7 I get the responses below

Code: Select all

C:\>DIR "C:\Program Files *\MSBuild\Microsoft\Windows Workflow Foundation\v3.0"
The filename, directory name, or volume label syntax is incorrect.

C:\>DIR "C:\P*\MSBuild\Microsoft\Windows Workflow Foundation\v3.0"
The filename, directory name, or volume label syntax is incorrect.

C:\>CD "C:\P*\MSBuild\Microsoft\Windows Workflow Foundation\v3.0"
The system cannot find the path specified.

C:\>CD "C:\PR*\MSBuild\Microsoft\Windows Workflow Foundation\v3.0"

C:\Program Files\MSBuild\Microsoft\Windows Workflow Foundation\v3.0>
C:\Program Files\MSBuild\Microsoft\Windows Workflow Foundation\v3.0>cd \

C:\>cd "C:\Program Files (*\MSBuild\Microsoft\Windows Workflow Foundation\v3.0"

C:\Program Files (x86)\MSBuild\Microsoft\Windows Workflow Foundation\v3.0>
C:\Program Files (x86)\MSBuild\Microsoft\Windows Workflow Foundation\v3.0>cd \

C:\>dir "C:\Program Files (*\MSBuild\Microsoft\Windows Workflow Foundation\v3.0"
The filename, directory name, or volume label syntax is incorrect.

C:\>dir "C:\Program Files (x86)\MSBuild\Microsoft\Windows Workflow Foundation\v3.0"
 Volume in drive C is C_System_C
 Volume Serial Number is 3E16-BCE7

 Directory of C:\Program Files (x86)\MSBuild\Microsoft\Windows Workflow Foundation\v3.0

14/07/2009  05:32    <DIR>          .
14/07/2009  05:32    <DIR>          ..
14/07/2009  05:32             4,726 Workflow.Targets
14/07/2009  05:32             5,182 Workflow.VisualBasic.Targets
               2 File(s)          9,908 bytes
               2 Dir(s)  16,466,096,128 bytes free


I am actually surprised that CD took a gamble on which choice to make of C:\Program Files *\... for the command
CD "C:\PR*\MSBuild\Microsoft\Windows Workflow Foundation\v3.0

My recollection of CD under Windows XP was that it refused to gamble when faced with a choice,
and only used a wild card if there was only one valid path to corresponded.
Perhaps x64 bit CMD.EXE has a bias towards the x64 bit Program Files rather than the obsolescent 32 bit flavour,
but this is going way of topic.

Valthero
Posts: 8
Joined: 09 Dec 2011 01:17

Re: XCOPY folder wildcard?

#4 Post by Valthero » 09 Dec 2011 13:59

orange_batch wrote:You need to build an array of folders matching the first wildcard, then build another array of folders matching the second from the first array, then process that list through your xcopy command all with for loops. I'm pretty sure wildcards only work on the last part of a path.

Code: Select all

@echo off&setlocal enabledelayedexpansion

for /f "delims=" %%x in ('dir "%drive%\Users" /ad /b') do (
set /a userslen+=1
set "usersarray!userslen!=%drive%\Users\%%x"
)

for /l %%x in (1,1,%userslen%) do (
for /f "delims=" %%y in ('dir "!usersarray%%x!\AppData\Roaming\Macromedia\Flash Player\#SharedObjects" /ad /b') do (
set /a flashlen+=1
set "flasharray!flashlen!=!usersarray%%x!\AppData\Roaming\Macromedia\Flash Player\#SharedObjects\%%y"
))

for /l %%x in (1,1,%flashlen%) do (
xcopy "!flasharray%%x!\games.mochiads.com\c\g\mud-and-blood-2\mudandblood2.swf\mnb21.sol" "C:\Backup" /c /h /r /y /i
)

pause

Untested.

For xcopy, I recommend using these switches for the most efficient backup:
/d /e /c /i /q /g /h /r /k /x /y


I just tried this, and here is the full log of what happened:

The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
File not found - mnb21.sol
0 File(s) copied
\Users\Valthero\AppData\Roaming\Macromedia\Flash Player\#SharedObjects\JMF4TL8L
\games.mochiads.com\c\g\mud-and-blood-2\mudandblood2.swf\mnb21.sol
1 File(s) copied
Press any key to continue . . .

So it seems to work, though it is messy. Thanks much for the help — I can run with this, but being the picky arse that I am, I'll see if I can tidy it up a bit. :)
I may post here again if I require further assistance, as I am planning to modify this batch to support Mac, Linux, and other versions of Windows.

Valthero
Posts: 8
Joined: 09 Dec 2011 01:17

Re: XCOPY folder wildcard?

#5 Post by Valthero » 09 Dec 2011 15:24

Would there be any way to use >nul or something similar to simply hide the first six lines of what I posted above? After some searching I can't seem to find any solutions. D:

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: XCOPY folder wildcard?

#6 Post by dbenham » 09 Dec 2011 15:40

The error messages are probably occuring on stderr as opposed to stdout.

Try appending 2>nul to the command(s) that generate the error message(s).


Dave Benham

Valthero
Posts: 8
Joined: 09 Dec 2011 01:17

Re: XCOPY folder wildcard?

#7 Post by Valthero » 09 Dec 2011 16:07

Hmm..same deal. It does not appear to be working no matter where I type it.

EDIT: I just discovered the CLS command. If I place it on a line above the XCOPY command, the errors are erased from the final output.
Looks like this is as good as it gets. Thanks for all the help, everyone. :)

Valthero
Posts: 8
Joined: 09 Dec 2011 01:17

Re: XCOPY folder wildcard?

#8 Post by Valthero » 09 Dec 2011 17:28

...Turns out I need to create a version with six wildcards, and despite my tinkering I cannot seem to figure it out. Help? D:

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: XCOPY folder wildcard?

#9 Post by orange_batch » 09 Dec 2011 18:56

Valthero sorry, xcopy is trying to find that file which may or may not exist in each of the paths... easy fix. Also, when 2>nul fails, >nul 2> or >nul 2>&1 should work.

Try the code above again, I made the necessary changes.

Provide the path in question with all wildcards marked and I'll see what I can do.

Valthero
Posts: 8
Joined: 09 Dec 2011 01:17

Re: XCOPY folder wildcard?

#10 Post by Valthero » 09 Dec 2011 19:32

With the changes, I recieve the output:

The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
\Users\geek chic\AppData\Roaming\Macromedia\Flash Player\#SharedObjects\JMF4TL8L
\games.mochiads.com\c\g\mud-and-blood-2\mudandblood2.swf\mnb21.sol
1 File(s) copied
Press any key to continue . . .


The path for the file in question is:
[WILDCARD]\Users\[WILDCARD]\AppData\Roaming\Macromedia\FlashPlayer\#SharedObjects\[WILDCARD]\localhost\FromMemory\[WILDCARD]\100\[WILDCARD].swf\mnb21.sol
I somehow managed to miscount the number of wildcards. I should go to bed earlier.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: XCOPY folder wildcard?

#11 Post by orange_batch » 09 Dec 2011 19:47

Oh pff, it's the second set of arrays too. I overlooked it. Edited again.

Wildcard for drive letters too... Ok, here's your new script:

Code: Select all

@echo off&setlocal enabledelayedexpansion

for %%x in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
if exist "%%x:\Users\" (
set /a driveslen+=1
set "drivesarray!driveslen!=%%x:\Users"
))

call :nextarray drives users ""
call :nextarray users shared "\AppData\Roaming\Macromedia\Flash Player\#SharedObjects"
call :nextarray shared from "\localhost\FromMemory"
call :nextarray from _100 "\100"

for /l %%x in (1,1,%_100len%) do (
set source="!_100array%%x!\mnb21.sol"
if exist !source! xcopy !source! "C:\Backup" /c /h /r /y /i
)

pause
goto :eof

:: Use with - call :nextarray "from array" "to array" "\folder\folder"
:: The location of backslashes in "\folder\folder" must be preserved.
:nextarray
for /l %%x in (1,1,!%~1len!) do (
set "source=!%~1array%%x!%~3"
if exist "!source!\" for /f "delims=" %%y in ('dir "!source!" /ad /b') do (
set /a %~2len+=1
set "%~2array!%~2len!=!source!\%%y"
))
exit/b

Should work... again untested. With the subroutine you should be able to adapt it however you want, provided you initialize and finalize the script properly.

The drive letters part could be made more complex, to list only physical hard drives so it's not spinning up optical drives or anything, but then you need to implement VBScript with WMI.
Last edited by orange_batch on 09 Dec 2011 22:11, edited 2 times in total.

Valthero
Posts: 8
Joined: 09 Dec 2011 01:17

Re: XCOPY folder wildcard?

#12 Post by Valthero » 09 Dec 2011 20:59

That's odd. It doesn't seem to recognize XCOPY.

'xcopy' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . .


Thanks for your help so far, by the way. You've done far more than I could have by myself.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: XCOPY folder wildcard?

#13 Post by orange_batch » 09 Dec 2011 21:57

K, I'll test it myself.

Oh man. :roll: I overwrote the path environment variable. Code fixed.

PS: If C:\Backup doesn't exist and you want to avoid xcopy from asking if it's a file or directory, change xcopy to echo(d|xcopy and add >nul after all the switches. This is safe even if Backup does exist.

Valthero
Posts: 8
Joined: 09 Dec 2011 01:17

Re: XCOPY folder wildcard?

#14 Post by Valthero » 10 Dec 2011 02:14

Thanks — it works like a charm!

C:\Users\Valthero\AppData\Roaming\Macromedia\Flash Player\#SharedObjects\JMF4TL
8L\localhost\FromMemory\D7BAED74924D435D84DB8A1E89E7AE3A\100\4BC37A1F293B47169DB
C8019C50075F5.swf\mnb21.sol
1 File(s) copied
Press any key to continue . . .


I'm pretty sure that's all the help I'll need. Thank you so very much for your time, patience, and knowledge. I'll be sure to credit you for this. :mrgreen:

Post Reply