Link to home
Start Free TrialLog in
Avatar of mstumpo
mstumpoFlag for United States of America

asked on

Selective Copy and overwrite

I have a problem where a vendor copied files and did not preserve the date and time.  Some files have subsequently changed which cannot be overwritten.  Essentially I am looking for a way to do the following:

If <AFILE> is dated <DATE>
copy file with same name from <SOURCE>
overwriting <AFILE>

Source and destination paths will be the same EXCEPT for Drive Letter.


Thanks in advance for any help.
Avatar of Bill Prew
Bill Prew

So you only want to copy files with a last modified date of a specific day that you will specify, from the source folder to the dest folder?

Or is it only one specific file name that is involved which you will also specify?

~bp
Avatar of mstumpo

ASKER

Files in the destination folder IF they have a specific date will be overwritten by the files in the source folder.
Are those all the same date, so we can't, say, copy all files that newer from the source directory as they will actually be older?  Can you give us an example of what the file dates look like.

If it is a specific day, It might be worth actually making a file of all the affected files from your destination directory and then using that to copy the source files again, though could always be the case that another file was edited on that same day?

e.g. this as a batch file would collect all entries for the date that you enter into a CSV file we can work from.

@echo off
pushd c:\mystartdir
dir *.
set /p MyDate=Enter date to look for in the same format as dir listing above, e.g. dd/mm/yyyy
(for /f "tokens=*" %%A in ('dir /b /s /a-d *.*') do echo "%~fA",%~tA) > AllFiles.txt
echo The file AllFiles.txt now contains all those files
find "%MyDate% < AllFiles.txt > DatedFiles.txt
notepad DatedFiles.txt
popd

Open in new window


We could use forfiles also to find files but that can only find files newer or older than a specified date, not exactly ON a date.

Steve
Okay, this should do what you described.  Save as a BAT file, and adjust the paths and dates near the top and give it a test.

@echo off
setlocal EnableDelayedExpansion

set SourceDir=B:\EE\EE28690499\Source
set DestDir=B:\EE\EE28690499\Dest
set CopyDate=06/18/2015

for %%A in ("%DestDir%\*.*") do (
  if exist "%SourceDir%\%%~nxA" (
    for %%B in ("%SourceDir%\%%~nxA") do (
      set FileDate=%%~tB
      if "!FileDate:~0,10!" EQU "%CopyDate%" (
        echo Updating "%%~A" from "%%~B"
        copy /y "%%~B" "%%~A" > NUL
      )
    )
  )
)

Open in new window

~bp
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial