How to copy multiple files based on date modified using batch file? -
i want make files copy command based on date modified other folder.
i have batch file this, can copy 1 file
@echo off set folder1=d:\ftp-nsqm\2g_volume\2017 set folder2=d:\ftp-nsqm\newday\2gvolume /f "tokens=*" %%a in ('dir /b /a-d /o-d "%folder1%\*.csv"') copy "%folder1%\%%~a" "%folder2%" & goto next :next echo carrying on rest of batch pause
how copy multiple files @ last modified?
for example, have files this. want copy file a.csv, b.csv, , c.csv folder 1 folder 2 , if possible, want delete previous data on folder 2 (d.csv, e.csv,and f.csv)
+--------------------------+ +--------------------------+ | folder 1 | | folder 2 (before) | +----------+---------------+ +----------+---------------+ | name | date modified | | name | date modified | +----------+---------------+ +----------+---------------+ | a.csv | 2017-07-26 | | d.csv | 2017-07-25 | | b.csv | 2017-07-26 | | e.csv | 2017-07-25 | | c.csv | 2017-07-26 | | f.csv | 2017-07-25 | | d.csv | 2017-07-25 | +----------+---------------+ | e.csv | 2017-07-25 | hope :) | f.csv | 2017-07-25 | +--------------------------+ | g.csv | 2017-07-24 | | folder 2 (after) | | h.csv | 2017-07-24 | +----------+---------------+ | i.csv | 2017-07-24 | | name | date modified | | ..... | ....-..-.. | +----------+---------------+ +----------+---------------+ | a.csv | 2017-07-26 | | b.csv | 2017-07-26 | | c.csv | 2017-07-26 | +----------+---------------+
:: remove .csv files destination del "%folder2%\*.csv" pushd "%folder1%" :: date lastdate latest date/time of file in folder1 /f "tokens=*" %%a in ('dir /b /a-d /o-d "*.csv"') set "lastdate=%%~ta"& goto next :next :: grab first 8 characters (may need 10, depending on date/time format) :: - date part set "lastdate=%lastdate:~0,8%" /f "tokens=*" %%a in ('dir /b /a-d /o-d "*.csv"') echo "%%~ta" | find "%lastdate%" >nul & if errorlevel 1 ( goto done ) else (copy "%%~a" "%folder2%") ) :done popd
so - first clear folder2, switch current directory folder1
set lastdate
date/time string first .csv found in reverse-date order , remove time portion.
run through directory again , see whether date found in lastdate
matches file's date. if does, errorlevel
set 0, otherwise non-zero.
the if errorlevel
test interprets current value of errorlevel
, , evaluates true
if value of errorlevel
nominated value or greater, otherwise false, need copy file if errorlevel
0
(the date matches lastdate
) , since files being listed in date-order, finding first non-match mean remaining files non-match on date, can exit thefor
loop done
, pop
original directory.
Comments
Post a Comment