Command Shell Scripting Question - Copy Specific Files in TXT file to another location maintaining the path

SweetkSysAdmin 1 Reputation point
2021-09-25T23:10:42.83+00:00

Hi

I'm not sure if this is the correct place to be posting this type of question, but I'm going to just give it a shot! Here we go. :)

I've been tasked with copying files from one folder to another folder using a list of files specified in a text file (.txt). We can't copy the entire folder for certain reasons.

So I've written a batch file that uses the for command and xcopy command to try and accomplish this, and it was partially successful. However, I have run into a few issues...

My Batch File:

cd c:\users\xyz.user\desktop
for /f "delims=" %%i in (ListOfFiles.txt) do Echo F|xcopy "%%i" "\share2\%%i*.*" /z /y >> logs.txt
pause

(This would copy the file and recreate the path and then put the copied file into a new folder that was named the same as the file.)

ex. Example of Existing File Tree Structure

\share\1\xyz.user\desktop\files\example1
picture1.jpg
picture2.jpg
text.txt
\share\1\xyz.user\desktop\files\example2
picture1.jpg
picture2.jpg
text.txt
\share\1\xyz.user\desktop\files\example3
picture1.jpg
picture2.jpg
text.txt

Example Contents of ListOfFiles.txt :

\share\1\xyz.user\desktop\files\example1\picture2.jpg
\share\1\xyz.user\desktop\files\example1\text.txt
\share\1\xyz.user\desktop\files\example2\picture2.jpg
\share\1\xyz.user\desktop\files\example3\picture2.jpg
It would create the following new structure:
\share2\share\1\xyz.user\desktop\files\example1\picture2.jpg
\share2\share\1\xyz.user\desktop\files\example1\text.txt
\share2\share\1\xyz.user\desktop\files\example2\picture2.jpg
\share2\share\1\xyz.user\desktop\files\example3\picture2.jpg

Issue #1. If I use the xcopy command to copy the files into one specific folder it will overwrite any files with the same name and there are several of these. So instead of doing that, I wrote the following batch file to recreate the path by using the %%i variable in the destination path, and it was working until I had files that were named too long.

I was thinking I could then use potentially a nested For Statement that would store each line (file path) in the %%i variable and then use a seperate file with just the path for the destination file path (%%j Variable):

for /f "delims=" %%i in (ListOfFiles.txt) do (
for /f "delims=" %%j in (ListOfPathsOnly.txt) do (
Echo F|xcopy "%%i" "\share\2\%%j*.*" /h /z /y >> \share2\nestedfortestlogs.txt
)
)
pause
Example Contents of List of Paths:
\share\1\xyz.user\desktop\files\example1\
\share\1\xyz.user\desktop\files\example1\
\share\1\xyz.user\desktop\files\example2\
\share\1\xyz.user\desktop\files\example3\

This doesn't seem to be working. I think what it's doing is getting the first file in the ListOfFiles.txt and it doesn't get the next file in the text file.

It's been sporadic and not really producing the same results..or I might have spent too much time messing with the script. (Brain scrambled..lol)

Does anyone have any tips for maintaining the file tree structure without having to use the file name as a folder or any other Ideas on a way that I could try to accomplish this?

I don't have much experience with command prompt and this was one of the first scripts I built from scratch.

Thanks in Advance!
SweetkSysAdmin

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes