简体   繁体   中英

need to process files based on arrival time using windows batch script

I receive 5 to 6 files daily. The file names are in the format XXXXX_DDMM_YYYYMMDDHHMISS.csv .

Examples:

  • A file generated Oct 17th 2016 at 03:12:32 pm is named XXXXX_17OCT_20161017151232.csv .

  • A file generated Oct 16th 2016 at 03:04:23 pm is named XXXXX_16OCT_20161016150423.csv .

  • A file generated Oct 18th 2016 at 02:34:56 pm is named XXXXX_18OCT_20161018143456.csv .

I need to move all the files to a folder named TEST1 and from there I need to find the first file which has the oldest timestamp, process it, and then delete that file from the folder.

Next need to pick second oldest timestamp and repeat the process for all files.

Could you please help me?

The classic method is

for /f "delims=" %%a in ('dir /b /a-d /od "test1\*.csv"') do echo %%a

which will echo the filenames matching the mask test1\\*.csv in /od order-of-date.

This should do what you want (well, select the files in order - the processing is your affair...) but if you really want to process the files according to their names where X* is unknown (but a constant length (of 5)) then

for /f "delims=" %%a in ('dir /b /a-d "test1\*.csv"^|sort /+11 ') do echo %%a

should work - sorting the list of raw filenames on the 11th column onward.

Magoo's 2nd solution works great if and only if the length of XXXXX is constant. But if it can vary, then SORT will not do you any good.

I have written JSORT.BAT to handle complex sorting scenarios that are beyond the capability of the native SORT command. It is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward - no 3rd party exe file required.

Use jsort /? from the command prompt to get help.

In this solution, I sort based on the text that appears after the last _ in the file name. So this solution works properly, even if XXXXX contains _ within it.

pushd "\test1"
for /f "eol=: delims=" %%F in (
  'dir /b /a-d "test1\*.csv"^|jsort /d _ /t -1'
) do (
  REM process the %%F file
  del "%%F"
)
popd

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