Page 1 of 1
Batch substring code not working
Posted: 04 Jun 2020 03:55
by kornkaobat
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%
Re: Batch substring code not working
Posted: 04 Jun 2020 20:57
by Squashman
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.
Re: Batch substring code not working
Posted: 05 Jun 2020 06:30
by kornkaobat
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.
Re: Batch substring code not working
Posted: 05 Jun 2020 10:44
by kornkaobat
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 .
Re: Batch substring code not working
Posted: 07 Jun 2020 16:05
by Reino
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?
Re: Batch substring code not working
Posted: 07 Jun 2020 18:49
by T3RRY
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.
Re: Batch substring code not working
Posted: 08 Jun 2020 00:28
by kornkaobat
...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