Brandon Mac
asked on
batch file compare 2 folder locations and copy files that are the same to folder
I prefer to do this with batch, I've done lots of research and feel that I have like an 80-85% solution I just need some help to get me over the top and clean it up a bit and add some stuff I don't have working yet
batch file to compare 2 folders checking to see if files exist in both folders
if the file exists in folder A and in folder B then copy it to an archive folder
log the file that is being copied and add the YYYYMMDD to the filename
>>>>>>>start script
@echo on
rem To add YYYYMMDD stamp to files copied to the archive folder
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set week=%%i
set day=%%j
set month=%%k
set year=%%l
)
setlocal
rem set variables for folder and network share location to compare files
set movedir=c:\move
set netdir= "\\name\folder"
set archivedir= c:\archive
rem set variable for log folder and log file
set logfile=c:\logpath\logfile .txt
rem check for the archive directory if it doesn't exist create the directory
if not exist "c:\archive" ( echo Creating Archive directory
mkdir "c:\archive"
)
rem map network path
echo Mapping Network location X:
net use x: "%netdir%"
echo Compare Move directory to a Network share
rem Check all files in folder move and Network share with a for loop ???
if the files exist in both directories then copy to archive and add a date stamp to name and log the files that were copied to the archive folder to the c:\logpath\logfile.txt
rem I believe this copy command would work I also had robocopy written see below
xcopy "c:\foldera\test.txt" "c:\archive\test%year%%mon th%%day%.t xt"
or
robocopy "%movedir%" "%archivedir%" *.* /PURGE /S /NP /R:5 /LOG+:%logfile% /TS /FP
rem disconnect network share
net use x: /d
The part I'm struggling with is the for loop to check the files in the move DIR and the network share location
for /f "delims=" %%a in ('dir /%move% /a-d "%netdir%" 2^>NUL') do if not exist "%movedir%%%a" echo %%a does not exist in "movedir" then execute copy from network share to the movedir
is the for loop even close to what I need here I've searched and tried to manipulate what I've found about all this to put it together
Thanks for your help
batch file to compare 2 folders checking to see if files exist in both folders
if the file exists in folder A and in folder B then copy it to an archive folder
log the file that is being copied and add the YYYYMMDD to the filename
>>>>>>>start script
@echo on
rem To add YYYYMMDD stamp to files copied to the archive folder
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set week=%%i
set day=%%j
set month=%%k
set year=%%l
)
setlocal
rem set variables for folder and network share location to compare files
set movedir=c:\move
set netdir= "\\name\folder"
set archivedir= c:\archive
rem set variable for log folder and log file
set logfile=c:\logpath\logfile
rem check for the archive directory if it doesn't exist create the directory
if not exist "c:\archive" ( echo Creating Archive directory
mkdir "c:\archive"
)
rem map network path
echo Mapping Network location X:
net use x: "%netdir%"
echo Compare Move directory to a Network share
rem Check all files in folder move and Network share with a for loop ???
if the files exist in both directories then copy to archive and add a date stamp to name and log the files that were copied to the archive folder to the c:\logpath\logfile.txt
rem I believe this copy command would work I also had robocopy written see below
xcopy "c:\foldera\test.txt" "c:\archive\test%year%%mon
or
robocopy "%movedir%" "%archivedir%" *.* /PURGE /S /NP /R:5 /LOG+:%logfile% /TS /FP
rem disconnect network share
net use x: /d
The part I'm struggling with is the for loop to check the files in the move DIR and the network share location
for /f "delims=" %%a in ('dir /%move% /a-d "%netdir%" 2^>NUL') do if not exist "%movedir%%%a" echo %%a does not exist in "movedir" then execute copy from network share to the movedir
is the for loop even close to what I need here I've searched and tried to manipulate what I've found about all this to put it together
Thanks for your help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Bill,
Sorry one other thing if I decide I want to move the duplicates instead of copy to the archive and indicate the date moved to archive in the log what would I change?
Sorry one other thing if I decide I want to move the duplicates instead of copy to the archive and indicate the date moved to archive in the log what would I change?
You can use the drive letter of a mapped drive, yes. But you can also use the network path as well, no need to map a drive just for this copy.
Yes, you could use the MOVE command instead of the COPY command to move a file, but that would only move one of the existing files. If you want to delete the other one that would be an additional command using DEL. Keep in mind that nothing you have proposed in the script so far compares content, just the fact that the same name exists in both folders.
»bp
Yes, you could use the MOVE command instead of the COPY command to move a file, but that would only move one of the existing files. If you want to delete the other one that would be an additional command using DEL. Keep in mind that nothing you have proposed in the script so far compares content, just the fact that the same name exists in both folders.
»bp
ASKER
Bill,
Thanks. The comparing is just checking to see if the filename exists in each folder or location. I could take it a step further if the script doesnt already do it which I'm not sure if it does. Here is example
Folder A Test.txt 2kb
FolderB test.txt 6kb
Is the script doing this type of check or simply checking the block that test is folder in both folders?
Thanks. The comparing is just checking to see if the filename exists in each folder or location. I could take it a step further if the script doesnt already do it which I'm not sure if it does. Here is example
Folder A Test.txt 2kb
FolderB test.txt 6kb
Is the script doing this type of check or simply checking the block that test is folder in both folders?
The scripts as coded just checks that the file with that name (case insensitive) exists in both folders. It does not check file date/times, size, or content...
»bp
»bp
ASKER
Thanks Bill for your help, sorry for delay in closing this.
ASKER
Thanks
ASKER
Thanks so much for helping with getting me on the right track. Do I need to do anything different if I change one of the paths from local to a network share.
I want to add mapping the share using it for comparing the files and then disconnect the drive.
set addmap= net use x: "\\location\folder"
set removemap = net use x: /d
if I change the netdir to a network path that is okay?