Copy specific files into subfolder in batch

送分小仙女□ 提交于 2019-12-20 05:01:54

问题


I have a Windows Batch file that I'm running to move specific files into a subfolder.

@ECHO OFF
ECHO Start Copy

setlocal enabledelayedexpansion

set SOURCE_DIR=C:\Users\paul.ikeda\Support\SNDataDemo91\SolidCAD\Inventor_in
set DEST_DIR=C:\Users\paul.ikeda\Support\SNDataDemo91\SolidCAD\Inventor_in\Files to Import
set FILENAMES_TO_COPY=SN_Router_1.ipt SN_Router_2.ipt SN_Router_3.ipt

for /R "%SOURCE_DIR%" %%F IN (%FILENAMES_TO_COPY%) do (
    if exist "%%F" (
        set FILE_DIR=%%~dpF
        set FILE_INTERMEDIATE_DIR=!FILE_DIR:%SOURCE_DIR%=!
    echo file "%%F"
        xcopy /Y "%%F" "%DEST_DIR%!FILE_INTERMEDIATE_DIR!"
    )
)

ECHO.
pause

This is copying my 3 desired files into the subfolder, but then it keeps going and creates a subfolder in the subfolder, copies the 3 files again into that subsubfolder, and it's repeating for as many files are in the original source folder. I just want to copy the 3 files to copy to the destination folder and stop there.

Can anyone spot the problem? I don't know enough batch code to properly debug this.


回答1:


If you just want to copy the files then this may suit you:

@ECHO OFF
ECHO Start Copy

set "SOURCE_DIR=C:\Users\paul.ikeda\Support\SNDataDemo91\SolidCAD\Inventor_in"
set "DEST_DIR=C:\Users\paul.ikeda\Support\SNDataDemo91\SolidCAD\Inventor_in\Files to Import"
set "FILENAMES_TO_COPY=SN_Router_1.ipt SN_Router_2.ipt SN_Router_3.ipt"

pushd "%SOURCE_DIR%"
for %%F IN (%FILENAMES_TO_COPY%) do (
    echo file "%%F"
    xcopy /Y "%%F" "%DEST_DIR%\"
)
popd
ECHO. done
pause


来源:https://stackoverflow.com/questions/17822813/copy-specific-files-into-subfolder-in-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!