简体   繁体   中英

Keeping conditions in filenames in batch file

I have few logs files in C:\\temp\\20120214.003_visual_sciences_web_feed.out.gz file. The filenames created for these logfiles are named based on YYYMMDD format on the day they were created. Now I want to write a batch file, that retrieve log files for files that are created between 1-jan-2013 to 1-sep-2013.

I am new to batch file scripting. Based on filename I am thinking of loop through all the files, get the index of each and every filename from 1-6 and filter it to check date conditions (using BETWEEN date1 and date2).

How can I accomplish this using AND conditions in simple way or do we have any other way?

It is tough to tell exactly what you want. But I believe this should be close:

From the command prompt (no batch):

for /f "delims=" %F in ('dir /b "c:\temp\*.out.gz"') do @if "%F" geq "20130101" if "%F" lss "20130902" @echo %F

From within a batch script:

for /f "delims=" %%F in ('dir /b "c:\temp\*.out.gz"') do if "%%F" geq "20130101" if "%%F" lss "20130902" echo %%F

or formatted nicely:

@echo off
for /f "delims=" %%F in ('dir /b "c:\temp\*.out.gz"') do (
  if "%%F" geq "20130101" if "%%F" lss "20130902" echo %%F
)

The above solutions will include files from 1-sep-2013. If you do not want to include that date, then change the last string to '"20130901"'.

Note that IF is doing a string comparison, not a date or numeric comparison. This only works because your date format is YYYYMMDD.

If you have additional .out.gz files that do not start with the date, and should therefore be ignored, then you can add a FINDSTR regex to be more selective. Unfortunately, FINDSTR regex support is very limited - there is no support for something like [0-9]{8} .

@echo off
for /f "delims=" %%F in (
  'dir /b "c:\temp\*.out.gz"^|findstr "^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.'
) do if "%%F" geq "20130101" if "%%F" lss "20130902" echo %%F

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