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%"
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
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:\Testfor /f %%i in (Test.txt) DO xcopy /S/E/U "%src_folder%\%%i" "%dst_folder%"
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:\Testset logfile=c:\Test\log.txtif exist "%logfile%" del "%logfile%">nulfor /f "tokens=* delims=" %%i in (Test.txt) DO echo xcopy /S/E/U "%src_folder%%%~pnxi" "%dst_folder%">>"%logfile%"
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.
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".
c:\whatever\folder\support
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