简体   繁体   中英

How to copy multiple files based on date modified using batch file?

I want to make the files copy command based on date modified from other folder.

I have batch file like this, but this only can copy one file

@echo off
set folder1=D:\FTP-NSQM\2G_VOLUME\2017
set folder2=D:\FTP-NSQM\newday\2gvolume
for /f "tokens=*" %%a in ('dir /b /a-d /o-d "%folder1%\*.csv"') do copy "%folder1%\%%~a" "%folder2%" & goto next
:next
echo Carrying on with rest of batch
pause

How to copy multiple files at last modified?

For example, I have files like this. I want copy file a.csv, b.csv, and c.csv from folder 1 to folder 2 And if possible, I want to delete the 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    |      I hope :) be like this
| 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 all .csv files from destination
del "%folder2%\*.csv"
pushd "%folder1%"
:: date lastdate to the latest date/time of a file in folder1
for /f "tokens=*" %%a in ('dir /b /a-d /o-d "*.csv"') do set "lastdate=%%~ta"& goto next
:next
:: grab first 8 characters (may need to be 10, depending on your date/time format)
:: - get the date part only
set "lastdate=%lastdate:~0,8%"
for /f "tokens=*" %%a in ('dir /b /a-d /o-d "*.csv"') do echo "%%~ta" | find "%lastdate%" >nul & if errorlevel 1 (
 goto done
 ) else (copy "%%~a" "%folder2%")
)
:done
popd

So - first clear folder2 , then switch the current directory to folder1

set lastdate to the date/time string from the first .csv found in reverse-date order and remove the time portion.

Run through the directory again and see whether to date found in lastdate matches the file's date. If it does, errorlevel will be set to 0, otherwise to non-zero.

The if errorlevel test interprets the current value of errorlevel , and evaluates to true if the value of errorlevel is the nominated value or greater, otherwise to false, so we need to copy the file if errorlevel is 0 (the date matches lastdate ) and since the files are being listed in date-order, finding the first non-match will mean that the remaining files will also be a non-match on date, so we can exit from the for loop to done and pop back to the original directory.

This should work ->

@echo off
set folder1=D:\FTP-NSQM\2G_VOLUME\2017
set folder2=D:\FTP-NSQM\newday\2gvolume
set dateFlag=""
cd %folder2%
for /f %%a in ('dir /b /od') do @set dateFlag=%%~ta
set dateFlag=%dateFlag:~0,10%
cd ..
del %folder2%
mkdir %folder2%
echo latest date= %dateFlag%
cd %folder1%
FORFILES /D +%dateFlag% /C "cmd /c copy @file %folder2/"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM