Friday, August 5, 2022

CMD : code to copy the files from 1 folder to another

 requirement : Here i have scenario like i have all the files name or image name which i need to copy over to new directory in same computer or on any external drive.

Code

@echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET "source=D:\srcdir\"
SET "source1="D:\srcdir""
SET "destination=D:\Destination"
SET "FileNameFromTExtFile=.\Path1.txt"

if not exist "D:\Destination\" mkdir D:\Destination 
rem copy Folder Structure as of source in Destination
XCOPY "%source1%" "%destination%" /t /e
 
FOR /F "usebackq eol=| delims=" %%f IN ("%FileNameFromTExtFile%") DO (
    where /Q /R %source1% %%f
rem echo !ERRORLEVEL!
    IF "!ERRORLEVEL!" == "1" (
@copy /y "%source1%%%f" "%destination%%%f" 
        )
     IF "!ERRORLEVEL!" == "2" (
        echo "%source1%%%f" is not found!
    )
)

 In above code it will copy all the files which are mentioned in Path1.txt file and it will loop through all the files and copy to Destination folder from source folder (srcdir).

It will also copy all directory structure before copying the fles.

Feel free to edit code as per your requirement.

how data need to pass in Path1.txt file?

\1C9A7356.JPG
\1C9A7360.JPG
\1C9A7364.JPG
\cam1\1C9A8147.JPG
\cam1\1C9A8163.JPG
\cam1\1C9A8165.JPG
\cam2\SHP04337.jpg
\cam2\SHP04353.jpg
\cam2\SHP04388.jpg
\cam1\DSC_3568.JPG
\cam1\DSC_3571.JPG
\cam1\DSC_3575.JPG
\cam1\DSC_3576.JPG
\cam2\SHP05260.jpg
\cam2\SHP05263.jpg
\cam2\SHP05288.jpg
\cam2\SHP05334.jpg
\cam3\1C9A8935.JPG
\cam3\1C9A8938.JPG
\cam3\1C9A8940.JPG
\cam3\1C9A8942.JPG

No comments:

Post a Comment

web stats