Nicho Hickman
Nicho Hickman

Reputation: 1

Batch file to copy files into one folder

I'm trying to run a bat to copy all the files in a list.txt. to a destination folder, without copying the folders the files are in, and I can't seem to get it to run right.

@ECHO OFF

CHCP 65001 > NUL

FOR /F "usebackq delims=" %%I IN ("C:\Users\Admin\Desktop\Test Copy batch\List.txt") DO (
    xcopy /S "C:\Users\Admin\Desktop\Test Copy batch\Source\%%I" "C:\Users\Admin\Desktop\Test Copy batch\Destination\%%I*"
)

pause

It's reading the List.txt file for a list of the file names.

Going into the Source folder and searching through all the subdirectories for those names.

Copying them and pasting them into the Destination folder.

But I want copied files to be in one singular folder.

Upvotes: 0

Views: 183

Answers (1)

aschipfl
aschipfl

Reputation: 34899

Let another for /F loop together with where get each source file and use copy to copy it into a flat directory structure:

@echo off
for /F usebackq^ delims^=^ eol^= %%I in ("C:\Users\Admin\Desktop\Test Copy batch\List.txt") do (
    for /F "delims= eol=|" %%S in ('set "PATHEXT=" ^& 2^> nul where /R "C:\Users\Admin\Desktop\Test Copy batch\Source" "%%I"') do (
        copy "%%~S" "C:\Users\Admin\Desktop\Test Copy batch\Destination\%%~nxS"
    )
)

The PATHEXT variable is temporarily cleared before the where command is running since it would regard it and could therefore additionally return unwanted files.

Upvotes: 1

Related Questions