1.

Solve : XCopy folders with relative paths!?

Answer»

Hi All

I would like to copy folders recursively from one location to another. The problem is that the folder names are changing slightly, but it retains something unique all the time. Below is an example of what the folders look like:

Copy:
\\server\path\10.200.10.777_Unique_name_lang\Something_else

-the numbers in front of the _Unique_name are changing all the time

Paste:
\\server\path\Test

I'm trying to automate this and I was wondering if it can be done using batch programming. Any help would be greatly appreciated!

SoncuPlease post the details on what part(s) of the path are static, and which part(s) change. The more details we have the better we can help you script a solution.Sorry I was a bit too evasive!

Here is the explanation in detail:

Path to copy:

\\server\path\10.200.10.777_Unique_name_lang\Something_else

10.200.10.777 - only these numbers change
_Unique_name_lang\Something_else - this is static

Copy the above path to:

\\server\path\Test

I hope this is clear enough! Thanks for the help!

Soncu It would be possible to use wildcards, provided the nonstatic DATA keeps the same format:

Example:
Code: [Select]xcopy \\server\path\10.???.??.???_Unique_name_lang\Something_else \\server\path\Test /s /e

You can WILDCARD as much or as little of the nonstatic data as you need.

Note: Suggest you use the ? wildcard instead of *. Less likely to run into problems.

8-)Sorry guys, your suggestion didn't seem to work. I'm using Windows XP Pro version. Any other suggestions?

"xcopy \\server\path\10..??._Unique_name_lang\Something_else \\server\path\Test /s /e"

Here is the code I have:

echo off
SET sourcedirMSI=K:\lang\10.0.1245.7777_Unique_name_lang\Something_else\FILE.txt
set sourcedirCAB=K:\lang\10.0.1245.7777_Unique_name_lang\Something_else\file.xml
set backupdir=C:\Test
for /f "tokens=*" %%a in ('dir "%sourcedirMSI%" "%sourcedirCAB%" /s/b') do (
xcopy "%%a" "%backupdir%%%~PA" /s/e
)
eof

The scenario is like this: The numbers in front are variables, they change often, so I want to find every folder that has _Unique_name_lang and file.txt/xml in it and copy it to C:\Test. At the moment I can only do a search in the lang folder for file.txt/xml, but this returns these files from all the folder and I only need the ones that contain _Unique_name_lang. I hope this makes sense!

Thanks
Soncu I must be missing something. I thought the numbers kept changing, but based on your code:
Quote

set sourcedirMSI=K:\lang\10.0.1245.7777_Unique_name_lang\Something_else\file.txt
they are set in stone. Would you not need to use the wildcards in the DIR command to create a collection of files or directories for the XCOPY to operate on?

Like I said, maybe I'm missing something. :-?Sorry Sidewinder, you're right! From that example it looks like is a set path! I just wanted to show how the folder looks with the full path. My humble apologies. Indeed, those numbers are changing. I need something like a wildcard or anything that would ignore those numbers and look only at what's after it _Unique_name_lang\Something_else\file.txt, copies only file.txt, including the dirs it come from too, into C:\test.. I hope I'm not getting more confusing...

Thanks
SoncuHow about this:

Code: [Select]set sourcedirMSI=Something_else\file.txt
set sourcedirCAB=Something_else\file.xml
set backupdir=C:\Test
FOR /f "tokens=*" %%a in ('dir /ad /b \\server\path\*^|findstr _Unique_name_lang$') do (
if exist "%%a.\%sourcedirMSI%" xcopy "%%a.\%sourcedirMSI%" "%backupdir%" /s /e
if exist "%%a.\%sourcedirCAB%" xcopy "%%a.\%sourcedirMSI%" "%backupdir%" /s /e
)

DOS IT HELP?Thanks for the help DOSItHelp but unfortunately your suggestion didn't work! Here is an example of the full path:

\\server\path\10.0.1234.7777_Unique_name_lang\Something_else

I need to bypass "10.0.1234.7777", read "_Unique_name_lang" and xcopy all the files with file.txt/xml found in all the subdirectories.

Thanks,
SoncuHow about this?

Code: [Select]set backupdir=C:\Test
FOR /f "tokens=*" %%a in ('dir /s /b \\server\path\*^|findstr _Unique_name_lang\\') do (
if /i "%%~nxa"=="file.txt" xcopy "%%a" "%backupdir%" /s /e
if /i "%%~nxa"=="file.xml" xcopy "%%a" "%backupdir%" /s /e
)[highlight]; )[/highlight]Hi DosItHelp

That works fine, but it only copies the files in the C:\Test folder in the root. I need the directories recursively as well. The /s/e switches don't seem to work.

Thanks again
Soncu How about this?

Code: [Select]set backupdir=C:\Test
FOR /f "tokens=*" %%a in ('dir /b /ad \\server\path\*^|findstr _Unique_name_lang$') do (
xcopy "%%a.\file.txt?" "%backupdir%" /s/y
xcopy "%%a.\file.xml?" "%backupdir%" /s/y
)[highlight]; )[/highlight]Thanks a lot DosItHelp! With a bit of tweaking it worked fine! How about making it to extract the files newer than 2 days without setting a fixed date!

Cheers
Soncu

Soncu,
This will be a little more tricky. Try this:

set TmpFile=%temp%.\%~n0.tmp
type NUL>"%TmpFileList%"
&REM makes sure tmpfile is empty

set backupdir=C:\Test
FOR /f "tokens=*" %%a in ('dir /b /ad \\server\path\*^|findstr _Unique_name_lang$') do (
xcopy "%%a.\file.txt?" "%backupdir%" /s/y/L&REM L-option Displays files that would be copied
xcopy "%%a.\file.xml?" "%backupdir%" /s/y/L&REM L-option Displays files that would be copied
)>"%TmpFile%"

SETLOCAL ENABLEDELAYEDEXPANSION
set /a AgeInDays=2
call:jdate tnow "%date%"&REM get today in julian days
for /f "tokens=*" %%F in ("%TmpFile%") do (
call:ftime tfile "%%F"&REM get file time in julian days
set /a diff=tnow-tfile
if !diff! LEQ %limit% xcopy "%%~F" "%backupdir%%%~F"
)

PAUSE
GOTO:EOF
:: Goto http://dostips.cmdtips.com/DtTipsDateTime.php
:: Copy :ftime :jdate :date2jdate functions below here
[/b]

You will need to get the functions [highlight]:ftime[/highlight] [highlight]:jdate[/highlight] and [highlight]:date2jdate[/highlight] from:
[highlight]http://dostips.cmdtips.com/DtTipsDateTime.php[/highlight]

Hope this helps


Discussion

No Comment Found