0

I have two folders.

folder1: C:\one\firstfolder
files:
111-YYYYMMDD-HHMMSSms
222-YYYYMMDD-HHMMSSms
or any value on first 3 digits and date and time.

folder2: C:\two\secondfolder
files:
111-YYYYMMDD-HHMMSSms
222-YYYYMMDD-HHMMSSms
or any value on first 3 digits and date and time.

The requirement I have is to compare the second folder files with first folder files.

(If first three digit code is same in both files and folder1 file time stamp is before the folder2 file timestamp, No need to process the files)
else
((If no files in folder1) or (if first three digit code is same and folder1 file time stamp is after the folder2 file timestamp, we need to move those files to other folder called C:\two\secondfolder\work)).

1
  • 1
    Please regard that SO is not a code writing service; so share what you have tried so far and describe where you're stuck by editing your post; thanks!
    – aschipfl
    Sep 16, 2015 at 14:19

1 Answer 1

0

This should also cater for a situation where there is more than 1 file matching the first part of the filename.

But it's untested:

@echo off
set "target=C:\two\secondfolder\work"
set "source1=C:\one\firstfolder"
set "source2=C:\two\secondfolder"
if exist "%source2%\*-*" dir "%source1%" /b /a-d >nul 2>&1 || move "%source2%\*-*" "%target%\" >nul
for /f "tokens=1-2,* delims=-" %%a in ('dir "%source1%\*-*" /b /a-d ') do (
  for /f "tokens=1-2,* delims=-" %%x in ('dir "%source2%\%%a-*" /b /a-d ') do (
      if %%b%%c GTR %%y%%z (
         if exist "%source1%\%%a-%%b-%%c" move "%source1%\%%a-%%b-%%c" "%target%\" >nul
         move ""%source2%\%%x-%%y-%%z" "%target%\" >nul
      )
  )
)
pause
13
  • Thank you. I have tested this... files are not moving from source2 to target if no files in source1. Please help me where do I need to change the logic.
    – Sruthi
    Sep 17, 2015 at 14:28
  • Do the filenames in source2 have a hyphen in them?
    – foxidrive
    Sep 17, 2015 at 14:43
  • Yes. file name format is 222-YYYYMMDD-HHMMSSms in both source folders.
    – Sruthi
    Sep 17, 2015 at 14:47
  • If the file is only in source2 then it will move. Or it does in my test. What version of Windows are you using?
    – foxidrive
    Sep 17, 2015 at 15:05
  • Windows 7, Let me send logic again, Requirement is to compare files on source1, source2 and move all files from source2 to target only If (no files on source1) or ((if first three digit code of both files is same) and (source2 file time stamp is greater than the source1 file time stamp)) .
    – Sruthi
    Sep 17, 2015 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.