Your question, your audience. Choose who sees your identity—and your question—with question security.
@echo off
set source="C:\forms update"
set dest="C:\vendors"
set filelist="%temp%\filelist.txt"
REM Make destination file listing to save doing for each file - "filename.ext","path"
(for /f "tokens=*" %%A in ('dir /b /s /a-d %dest%\*.*') do echo "%%~nxA","%%~dpA") > %filelist%
REM loop through files in source dir, then look in above file list for directory to copy
for /f "tokens=*" %%s in ('dir /b /a-d %source%\*.*') do (
echo Looking for files to update with %%s
for /f "tokens=1,2 delims=," %%f in ('find "%%~s" ^<%filelist%') do (
echo ... updating in %%~g directory... do robocopy here ...
)
)
@echo off
setlocal enabledelayedexpansion
set Source=C:\Forms update
set Mask=*.csv
set TargetRoot=C:\Master
for %%s in ("%Source%\%Mask%") do (
set Found=False
for /f "delims=" %%f in ('dir /s /b /a:-d "%TargetRoot%\%%~nxs" 2^>NUL') do (
set Found=True
ECHO copy "%%s" "%%~dpf"
if errorlevel 1 (
echo ERROR: File '%%~nxs' could not be copied to target.
) else (
echo OK: File '%%~nxs' copied to '%%~dpf'.
)
)
if !Found!==False (
echo ERROR: File '%%~nxs' not found in target.
)
)
ERROR: File 'test-notthere.txt' not found in target.
1 File(s) copied.
OK: File 'test.txt' copied to 'D:\Temp\B\'.
Oh, and whether the script processes one file or more doesn't really matter.
@echo off
set source="C:\forms update"
set dest="C:\vendors"
set filelist="%temp%\filelist.txt"
REM Make destination file listing to save doing for each file - "filename.ext","path"
(for /f "tokens=*" %%A in ('dir /b /s /a-d %dest%\*.*') do echo "%%~nxA","%%~dpA") > %filelist%
REM loop through files in source dir, then look in above file list for directory to copy
for /f "tokens=*" %%s in ('dir /b /a-d %source%\*.*') do (
echo Looking for files to update with %%s
for /f "tokens=1,2 delims=," %%f in ('find "%%~s" ^<%filelist%') do (
echo ... updating in %%~g directory...
xcopy "%source%\%%~f" "%%~g" /y /d
)
)
@echo off
set source="C:\forms update"
set dest="C:\vendors"
set filelist="%temp%\filelist.txt"
REM Make destination file listing to save doing for each file - "filename.ext","path"
(for /f "tokens=*" %%A in ('dir /b /s /a-d %dest%\*.*') do echo "%%~nxA","%%~dpA") > %filelist%
REM loop through files in source dir, then look in above file list for directory to copy
for /f "tokens=*" %%s in ('dir /b /a-d %source%\*.*') do (
echo Looking for files to update with %%s
for /f "tokens=1,2 delims=," %%f in ('findstr /b /l /i "\"%%~s\"," "%filelist%"') do (
xcopy "%source%\%%~f" "%%~g" /y /d | find "1 File(s) copied" && echo ... updating in %%~g directory...
)
)
D:\>new
Looking for files to update with y.txt
1 File(s) copied
... updating in d:\temp\ directory...
1 File(s) copied
... updating in d:\temp\x\ directory...
1 File(s) copied
... updating in d:\temp\x\z\ directory...
D:\>new
Looking for files to update with y.txt
D:\>
4) Once completed the script will move the sources files into completed folder.would'nt it be better to replace XCOPY by MOVE?
@echo off
set source="C:\forms update"
set dest="C:\vendors"
set filelist="%temp%\filelist.txt"
REM Make destination file listing to save doing for each file - "filename.ext","path"
(for /f "tokens=*" %%A in ('dir /b /s /a-d %dest%\*.*') do echo "%%~nxA","%%~dpA") > %filelist%
if NOT "%~1"=="" (
echo Looking for files to update with "%~1"
for /f "tokens=1,2 delims=," %%f in ('findstr /b /l /i "\"%~1\"," "%filelist%"') do (
xcopy "%source%\%%~f" "%%~g" /y /d | find "1 File(s) copied" && echo ... updating in %%~g directory...
)
) ELSE (
REM loop through files in source dir, then look in above file list for directory to copy
for /f "tokens=*" %%s in ('dir /b /a-d %source%\*.*') do (
echo Looking for files to update with %%s
for /f "tokens=1,2 delims=," %%f in ('findstr /b /l /i "\"%%~s\"," "%filelist%"') do (
xcopy "%source%\%%~f" "%%~g" /y /d | find "1 File(s) copied" && echo ... updating in %%~g directory...
)
)
)
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Join the community of 500,000 technology professionals and ask your questions.