Batch substring code not working

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kornkaobat
Posts: 7
Joined: 13 May 2020 05:13

Batch substring code not working

#1 Post by kornkaobat » 04 Jun 2020 03:55

When %VAR:~0,10% is used in the following code, the code doesn't "refresh" the first 10 characters of the %VAR% when new VAR is set using for loop.

Code: Select all

for /f "tokens=1* delims= " %%a in (disktest.txt) do %~dp0\fgrep.exe -i %a:~0,10% %$base% >nul
P.S.
  • The contents in disktest.txt is structured as follow: 036D9462A58BEE6F0F404BC4A569108B md5.png
  • %~dp0\fgrep.exe -i %a:~0,10% %$base% is the same as saying fgrep -i %a:~0,10% %$base%

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Batch substring code not working

#2 Post by Squashman » 04 Jun 2020 20:57

So in your previous thread you knew enough to set the FOR variable to an environmental variable before you manipulated it.

Code: Select all

for %%a in (%~1) do for /f "tokens=1*" %%b in ("!return.vars!") do (
  set "return.normal=!%%a!"
  if defined return.normal (
    set "return.normal=!return.normal:%%=%%3!"
    set "return.normal=!return.normal:"=%%4!"
So that should tell you that string replacement and substrings can only be done with environmental variables.

kornkaobat
Posts: 7
Joined: 13 May 2020 05:13

Re: Batch substring code not working

#3 Post by kornkaobat » 05 Jun 2020 06:30

Code: Select all

setlocal EnableDelayedExpansion
for /f "tokens=1* delims= " %%a in (disktest.txt) do set "comp=!%%a!" && %~dp0\fgrep.exe -i %comp:~0,10% %$base% >nul
setlocal DisableDelayedExpansion
Still didn't work ( comp variable contains ECHO is off ) . I do not quite grasp the concept of ENV variable anyways.

kornkaobat
Posts: 7
Joined: 13 May 2020 05:13

Re: Batch substring code not working

#4 Post by kornkaobat » 05 Jun 2020 10:44

Code: Select all

call :GetUnixTime utimebefore
REM Fix %a:~0,10% syntax usage -- Fixed using grep and batch piping
for /f "tokens=1* delims= " %%a in (disktest.txt) do echo %%a | %~dp0\grep.exe -Eo "^.{10}" | %~dp0\fgrep.exe -i -f - %$base% >nul
call :GetUnixTime utimeafter
Fixed using grep powerful command extensions. I wish cmd could have its own wget, grep, sed, parallel, .etc .

Reino
Posts: 23
Joined: 14 May 2015 06:13
Contact:

Re: Batch substring code not working

#5 Post by Reino » 07 Jun 2020 16:05

I'm having a hard time figuring out what you're actually trying to do.
What do you want to do with/to "036D9462A58BEE6F0F404BC4A569108B md5.png"?
I understand that the first grep returns "036D9462A5", but then what?

T3RRY
Posts: 243
Joined: 06 May 2020 10:14

Re: Batch substring code not working

#6 Post by T3RRY » 07 Jun 2020 18:49

kornkaobat wrote:
05 Jun 2020 06:30

Code: Select all

setlocal EnableDelayedExpansion
for /f "tokens=1* delims= " %%a in (disktest.txt) do set "comp=!%%a!" && %~dp0\fgrep.exe -i %comp:~0,10% %$base% >nul
setlocal DisableDelayedExpansion
Still didn't work ( comp variable contains ECHO is off ) . I do not quite grasp the concept of ENV variable anyways.
It's still not working because your application of delayed expansion is incorrect. !%%a! Attempts to expand the value of the token %%a as thought it is a variable. %comp:~0,10% within a code block will not update. The correct use would be:

Code: Select all

Setlocal enabledelayedexpansion
for /f "tokens=1* delims= " %%a in (disktest.txt) do ( set "comp=%%a" & %~dp0\fgrep.exe -i !comp:~0,10! %$base% > nul )
Assuming your usage of tokens / delims is correct for the content your retrieving.

kornkaobat
Posts: 7
Joined: 13 May 2020 05:13

Re: Batch substring code not working

#7 Post by kornkaobat » 08 Jun 2020 00:28

...I understand that the first grep returns "036D9462A5", but then what?

Code: Select all

for /f "tokens=1* delims= " %%a in (disktest.txt) do echo %%a | %~dp0\grep.exe -Eo "^.{10}" | %~dp0\fgrep.exe -i -f - %$base% >nul
1. Seperate contents in disktest.txt into 2 tokens, namely %%a %%b using space as a delimiter: 036D9462A58BEE6F0F404BC4A569108B=%%a, md5.png=%%b
2. Pipe %%a into grep and repipe the first 10 character of %%a out specifically to fgrep <pattern> using -f - to avoid parsing into standard input.
3. %~dp0\fgrep.exe -i -f - %$base% >nul now reads as fgrep -i %%a %$base% ( %%a is processed ) which compares the truncated %%a MD5 hash with malware hashes' database.
Ref : https://unix.stackexchange.com/question ... lter-terms

Post Reply