Avatar of David Izen
David Izen
 asked on

xcopy using a text file to a folder

Hi guys

I have a text file with full file paths, listed like this example below:

G:\folder\support_team\example.pdf
G:\folder2\work centre\example.docx

and so on.

I want to use this text file so I can copy these specific files to a destination folder on a different disk, keeping the folder structure. The files are coming from an external hard drive (plugged into a server via USB) and need to be copied over to a different disk in a folder of my choice which is on the local server.

These are a list of files where they exceeded Windows 255 character limit when I did a copy of data so I'm hoping a script will also bypass that limitation.

I was trying to a variation of the below:

set src_folder=c:\whatever\
set dst_folder=c:\whatever\
for /f %%i in (File-list.txt) DO xcopy /S/E/U "%src_folder%\%%i" "%dst_folder%"

Open in new window


But having 0 success and just getting errors in CMD. This is for a Windows OS environment.

Can anyone help me out please?
Windows BatchMicrosoft DOSWindows OS

Avatar of undefined
Last Comment
BillDL

8/22/2022 - Mon
Bill Prew

So in your example, how do the %src_folder% and the full path that's in the text file work together?  Do you want to use the full path from the file, and ignore the variable?  Or take everything from the text file list except the drive letter and concatenate that to the variable, like:

c:\whatever\folder\support_team\example.pdf

Sorry to say that xcopy can't get around long file names, it's pretty hard actually.  Did the long files names occur on the destination of the copy, or is the file name that is in the text list file already longer than the limit?

~bp
Bill Prew

I don't see any syntax problems with your code.  What is the error, I suspect it's a runtime error like it can't find a file or a folder, rather than a syntax error as it scans the batch script initially.

~bp
David Izen

ASKER
I think that's where I'm lost. I don't really know how the src folder interacts with the text file, or if it even should? I just would like the text file paths to be read, then those paths with the files to be copied over to the destination folder of my choice.

The long file names happened on the destination of the copy I believe. I don't mind if I have to lose the structure as long as I can copy those files somehow if that what it takes to do this.

For example, when testing I would do this:

set src_folder=c:\
set dst_folder=c:\Test
for /f %%i in (Test.txt) DO xcopy /S/E/U "%src_folder%\%%i" "%dst_folder%"

Open in new window


The text file would have a file path of a folder I created as a test but even that just does nothing.

Thanks
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
David Izen

ASKER
Brilliant, thank you. Very nearly there now. I have one last question I hope (sorry).

This script seems to ignore spacing in the paths within the text file?

In my text file I have an example file I've made on my NAS (to test):

X:\Staff Folders\Me\Test.txt

Open in new window


The script works in that it now fully runs but it says "cannot found - "Staff" so I assume it cannot comprehend the spacing in the paths.
SOLUTION
BillDL

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
David Izen

ASKER
Thank you! That's sorted the spacing issue. Really appreciate the responses.

While the script now fully executes correctly, it says 0 files copied. Could I be missing something here? (my bat file run below)

C:\Users\David\Desktop>"new xcopy.bat"

C:\Users\David\Desktop>set src_folder=X:

C:\Users\David\Desktop>set dst_folder=C:\Users\David\Desktop\Test

C:\Users\David\Desktop>for /F "tokens=* delims=" %i in (test.txt) DO xcopy /S/E/U "X:%~pnxi" "C:\Users\David\Desktop\Test"

C:\Users\David\Desktop>xcopy /S/E/U "X:\Staff Folders\Me\Test.txt" "C:\Users\David\Desktop\Test"
0 File(s) copied

Open in new window


Is my ending message so it's definitely reading the text file correctly but not actually copying it over. Any ideas would be greatly appreciated.

Thanks
BillDL

I assume that the and in the code snippet are your attempts to apply italics to a portion of the command rather than what was actually displayed on screen.  Code snippets are unformatted text, so you can't add any formatting tags like italics.

I tend to add an ECHO into my batch files and redirect what will display on the screen to a log file (C:\Test\log.txt) for debugging purposes like this:

set src_folder=c:
set dst_folder=c:\Test
set logfile=c:\Test\log.txt
if exist "%logfile%" del "%logfile%">nul
for /f "tokens=* delims=" %%i in (Test.txt) DO echo xcopy /S/E/U "%src_folder%%%~pnxi" "%dst_folder%">>"%logfile%"

Open in new window


Looking at the commands that would have been executed will let you know whether the syntax is correct.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
David Izen

ASKER
The log file just output the xcopy command showing what I want to copy, but it still didn't actually copy anything over.

And yes sorry I forgot to remove my italic command, that was a typo !

So the log file just shows this:

xcopy /S/E/U "X:\Staff Folders\me\test.txt" "C:\Users\David\Desktop\Test"

Open in new window


I've just created an empty text file on my X drive (local NAS) and just want that to copy to my desktop folder as a test, obviously once I know this works I will try on the actual data but the principle is the same.

Thanks again
BillDL

/S /E is the same as just /E - Copies directories and subdirectories, including empty ones.
The /U switch only copies files that already exist in the destination.
Is the idea to overwrite the destination file with a new one?  If not, just lose the /U switch and see what happens.

Here's the basic XCOPY usage (taken from this old Windows XP version I am using at the moment):
xcopy_syntax.txt
David Izen

ASKER
Brilliant, thank you. I just was testing and noticed that.

Thank you very much to both of you.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
David Izen

ASKER
Excellent help. Thank you to both the guys .
BillDL

Thank you David

When it comes to doing this on real files and folders I suggest that you add an ECHO as previously suggested, and assess the commands before running it "live".