Home > OS >  Copy list of files to specific folder locations
Copy list of files to specific folder locations

Time:03-26

I have ~10,000 files in one folder. I want to copy specific files to specific folders based on a text file. Is it possible to use a semicolon delimited text file where the first part of each line is the source path & file name and the second part is the destination path?

C:\Files\File1.txt;C:\Folder1
C:\Files\File2.txt;C:\Folder2
C:\Files\File3.txt;C:\FolderN\

What would the code look like? Is there a better way to achieve the same result?

I have an existing bat file I use to copy all of the files listed in a text file to one specific folder location (below) but in this case I need to send different files to different folders and I would rather not run my bat file 50 times, changing the destination path in the bat file each time...

for /f %%f in (%1) do ( copy %%f G:\Files\PutFilesHere )

CodePudding user response:

for /f "tokens=1,2delims=;" %%f in (%1) do ( copy "%%f" "%%g" )

see for /? from the prompt for documentation

CodePudding user response:

It looks like this:

@echo off
setlocal enableextensions enabledelayedexpansion

for /f "usebackq delims=; tokens=1,2" %%I in (`type filelist.txt`) do (
    copy /y %%I %%J
)
goto :eof

The for loop is told to use the result of a command (usebackq, for command type filelist.txt), to split at each ;, and to take elements #1 and #2. The fist is named as the variable (%%I, take care, case sensitive), and the second is the next letter, so %%J. Then, the copy is trivial.

  • Related