0

I have a text file including filename specifications formatted as following, filename-yyyymmdd

source.txt

IMG-20190601
IMG-20190602
IMG-20190603
...

I want to read this file in order to compare the dates with a reference date and do some action depending the result. IMG is always the same, only the date is changing.

For this purpose I am trying to find the filename date into each line I am reading to compare it with today.

I did not succeed to find the right syntax, I found that extracting a substring can be done with

set SUBSTRING=%VAR:~POSITION,SIZE% 

but it is not working with %%variable type.

Any help is welcome.

My code:

set comparedate=20190702
set /A i=0
for /F "usebackq delims=" %%a in (source.txt) do (
    set /A i+=1
    rem call echo %%i%%
    rem call echo %%a
    set datefile=%%a:~4,8 # the line that is not working
    if %datefile% geq %comparedate%  (goto here) else (goto there)
    :here
    echo do something
    :there
    echo do something else
)
Compo
  • 36,585
  • 5
  • 27
  • 39
dantlv
  • 1
  • 1
  • 1
    `but it is not working with %% variable type.`. Yes, that's correct (by design). You need to convert the `%%x` variable to a "normal" `%var%` variable before you can do substring processing. And of course, you need [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) (`!var:~4,8!`) to make it work in a code block. – Stephan Jul 29 '19 at 09:09
  • Can you see the difference between, `%VAR:~POSITION,SIZE%` and `%%a:~4,8`? They're both different kinds of variables, but only one of them can be expanded and modified. You need to make the latter into the former first, i.e. `Set "VAR=%%a"`. I would also strongly suggest that you enable delayed expansion before trying to modify and use the variables content within a code block too. – Compo Jul 29 '19 at 09:10
  • Please show us the actual format of `source.txt`. As it now stands, `:~4,8` would extract the string `filename` – Stephan Jul 29 '19 at 09:15
  • Any `goto` within a loop breaks the loop (e.g it will only process the first line). Avoid that with `call` instead of `goto`. And place your lables (subroutines) *outside* the loop. – Stephan Jul 29 '19 at 09:17
  • Thanks both for answering. It works by using set "VAR=%%a", with double quotes. Without it doesn't work. Best :) – dantlv Jul 29 '19 at 11:14
  • I believe you want to extract the last 8 characters from the file names, right? if so, use `!VAR:~-8!`... – aschipfl Jul 30 '19 at 08:33

1 Answers1

0

Implemented my suggestions from the comments to your code. Also avoided delayed expansion by using the tokens directly (together with another method to get the date string from the filenames (by splitting at the hyphen)):

@echo off
set comparedate=20190702
set /A i=0
for /F "tokens=1,* delims=-" %%a in (source.txt) do (
    ECHO FileDate=%%b
    ECHO FileName=%%a-%%b [in case you need it]
    if "%%b" geq "%comparedate%"  (
      echo do something
    ) else (
      echo do something else
    )
)
Stephan
  • 53,940
  • 10
  • 58
  • 91