Page 1 of 1

XCOPY folder wildcard?

Posted: 09 Dec 2011 01:27
by Valthero
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.

Re: XCOPY folder wildcard?

Posted: 09 Dec 2011 03:04
by orange_batch
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

Re: XCOPY folder wildcard?

Posted: 09 Dec 2011 03:10
by alan_b
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.

Re: XCOPY folder wildcard?

Posted: 09 Dec 2011 13:59
by Valthero
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.

Re: XCOPY folder wildcard?

Posted: 09 Dec 2011 15:24
by Valthero
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:

Re: XCOPY folder wildcard?

Posted: 09 Dec 2011 15:40
by dbenham
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

Re: XCOPY folder wildcard?

Posted: 09 Dec 2011 16:07
by Valthero
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. :)

Re: XCOPY folder wildcard?

Posted: 09 Dec 2011 17:28
by Valthero
...Turns out I need to create a version with six wildcards, and despite my tinkering I cannot seem to figure it out. Help? D:

Re: XCOPY folder wildcard?

Posted: 09 Dec 2011 18:56
by orange_batch
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.

Re: XCOPY folder wildcard?

Posted: 09 Dec 2011 19:32
by Valthero
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.

Re: XCOPY folder wildcard?

Posted: 09 Dec 2011 19:47
by orange_batch
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.

Re: XCOPY folder wildcard?

Posted: 09 Dec 2011 20:59
by Valthero
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.

Re: XCOPY folder wildcard?

Posted: 09 Dec 2011 21:57
by orange_batch
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.

Re: XCOPY folder wildcard?

Posted: 10 Dec 2011 02:14
by Valthero
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: