Batch: Copy files from txt file into one folder

后端 未结 3 460
我在风中等你
我在风中等你 2021-01-07 01:51

I am attempting to create a batch file to copy several files listed in a text file to a new folder. I have found several threads relating to this, but I can still not get th

3条回答
  •  难免孤独
    2021-01-07 02:26

    Start reading HELP FOR and then try the following at the command prompt

    FOR /F %a in (input.txt) DO @ECHO COPY %a c:\newfolder\%~nxa
    

    you can see that %a gets expanded to the actual line in the input file, and that %~nxa is a way to extract the name and the extension from the file.

    After careful testing, move the command to your BAT file, replace %a to%%a, and remove the ECHO command

    @echo off
    SET destfolder=c:\newfolder 
    FOR /F "delims=" %%a IN (input.txt) DO COPY "%%a" "%destfolder%\%%~nxa"
    

    notice the wraping of the names with quotes "; and the inclusion of the "delims=" option; both are needed in case filenames contain blanks.

    Finally be careful with possible name duplicates in the destination folder. If that is possible, you need to find an strategy to cope with such collisions. But this can be the subject of another SO question, can't it?

提交回复
热议问题