Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top