0

I have a text file that I am searching through that looks like:

ksdf 0 0 -4

as7d:S:asf 0 0 -4

kc:S:cd3 0 0 -2

asdk:S:s 0 0 6

lasd:S:dd 0 0

At the moment my specific problem is when searching the file for the 1st and 4th tokens. If the 4th token is a number everything sends fine, but when the fourth token is blank (as on the 5th line in my example) the code simply doesn't work. You can see I want to return three asterisks when it finds a blank in the %%B variable.

NOTE: Thanks to the user, LotPings, I'm using the findstr command to grab only lines beginning with strings containing the substring :S: just to clarify why that's there. (Ie. line 1 in my example wouldn't be grabbed.)

I'm using:

setlocal enabledelayedexpansion

For /F "tokens=1,4" %%A in ('Findstr /b /r /c:"[^ ]*:S:" print.log') do (
set space="%%B"
if !space!=="" echo %%A ^*^*^* >> new.txt
)

exit
Community
  • 1
  • 1
Glycoversi
  • 77
  • 8
  • 2
    posted code works happily for me. – Magoo Mar 06 '17 at 23:43
  • 1
    `>>new.txt (if "%%B"=="" (echo %%A ^*^*^*) else (echo %%A %%B))` – JosefZ Mar 07 '17 at 00:00
  • Why are you assigning the FOR metavariable to an environmental variable? – Squashman Mar 07 '17 at 02:03
  • I had changed the ":S:" to something else accidentally, THAT was my issue! Thanks for looking anyways friends! – Glycoversi Mar 07 '17 at 03:15
  • @Squashman I am ultimately going to compare the values stored in `%%b` by using `set space=!space:-=!` to remove the negative, then use `if !errorlevel!==0` on that as a switch to compare the absolute values (one case to add the negative back on and one to echo the `space` value as is if it's a positive) to a static number like `if !space! GTR 3 echo %%a -!space! >>new.txt` if its negative. It hasn't worked for me yet, but maybe I'm missing something obvious, idk. – Glycoversi Mar 07 '17 at 03:23

1 Answers1

0
@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion

>new.txt (
  For /F "tokens=1,4" %%A in ('Findstr /b /r /c:"[^ ]*:S:" print.log') do (
    set "space=%%B"
    if "!space!"=="" (echo %%A ^*^*^*) else (echo %%A %%B)
  )
)

type new.txt
pause
JosefZ
  • 28,460
  • 5
  • 44
  • 83