3

In a UTF-16LE log file, I have lines like this:

[128 characters]APP_NAME=Samsung Magician&APP_KEY=[additional characters]&APP_FLAG=[single digit]

I want the APP_NAME (e.g., Samsung Magician) from each line where APP_FLAG=0. I can identify those lines thus:

FIND "APP_FLAG=0" "C:\[directory]\logfile.log" > D:\Progs1.txt

The next step is less clear. I'm having trouble extracting the desired text from the middle of those lines in Progs1.txt. It's easy in Excel, but this time I'd like to learn how to automate the process.

I believe the next step is to discard the first 128 + 9 characters from each line in Progs1.txt. Ideally, I wouldn't rely on the assumption that there will always be exactly 128 leading characters. Instead, I would use APP_NAME as the delim in a FOR loop. But it seems multiple characters are not permitted for that purpose.

I'm not yet at the point of trying to figure out the final step, discarding the text beginning with &APP_KEY, so as to leave only the app name as final output. If I could assume a constant number of underscores in each line, maybe I could use something like the Split String code. But I don't know if lines vary in the number of underscores used.

The question here is just, what command is best to produce a Progs2.txt output file in which each line begins with an app name (e.g., Samsung Magician)?

2
  • 1
    Ray, so for each line you will always and only have one value to get and that's what is always and only in between the APP_NAME= and &APP_KEY= characters of each line?? Is it true you only will have one possible match on each line and never more than one match on APP_NAME= THE MATCH HERE &APP_KEY=? Or can there be more than one match on each line? Oct 4, 2019 at 1:43
  • Ray - If you get a chance, please edit and add a small example of your data masking out anything sensitive giving 5-10 lines of "APP_FLAG=0" and "APP_FLAG=notzero" with a various mixture or whatever. I'll test it some more when I get a chance once you update and if you still have not gotten a solution that works. Just tag me back and I'll look into it then. Oct 4, 2019 at 15:14

1 Answer 1

0

(I'm not going to flag this question, because I'm not a jerk flagger like most other people on Stack Exchange, but just for future reference, questions like this should be posted on Stack Overflow.)

I've actually dealt with the same problem as you before, and unfortunately (from what I know), what you want is not possible in Batch. However, I have a work-around.

Batch is geared more towards running in the CMD, whereas Visual Basic Script is geared more towards running in Powershell. Powershell is capable of doing a lot more complex problems, but because the two are nearly the same thing, they can work together, which is what I'm doing here.

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("<insert-txt-file-here>", ForReading)
strSearchString = objFile.ReadAll
objFile.Close
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.Pattern = "<insert-pattern-here>"
Set colMatches = objRegEx.Execute(strSearchString)
Const ForWriting = 2
Set obj = CreateObject("Scripting.FileSystemObject")
Set obj1 = obj.OpenTextFile("<insert-output-file-here>", ForWriting)
If colMatches.Count > 0 Then
   For Each strMatch in colMatches
      obj1.WriteLine(str.Match.Value)
   Next
End If
obj1.Close

The code above will extract any portion of text from a text file, as long as the text matches with it's pattern (insert-pattern-here). For example, if I wanted to search for each time "red jaguar" appeared in the text file, I would insert objRegEx.Pattern = "red jaguar". The patterns can get even more complex than that, more info here.

I know that the above code has worked for me in the past, but Visual Basic Script is notorious for not working universally, so you might have to mess around with the code a bit. I would test it myself, but my Desktop is broken right now, so I am stuck with a laptop that has linux on it, and linux cannot run Visual Basic Script. Also, in case you don't already know, the script must be saved as a '.vbs' file.

Anyways, if you have some kind of 'master program' or something that runs in Batch, then you can tell the batch file to start the Visual Basic Script using the START /WAIT command, let the script run, then import it's output using this code: for /f "Delims=" %%a in (<insert-vbs-output-here>) do (set output=%%a).

Hope this helps.

EDIT: I forgot to mention that the output text file for the Visual Basic Script must be provided in order for it to work correctly. If you want to have a fresh output text file each time you run the script, then before you start the script in the Batch file, create the text file using this: echo. > output.txt, and then after the script runs and the it's output is imported, use this to delete the text file: DEL output.txt. However, if you're debugging, it's probably a good idea to not delete the output, just so you can see what it's outputing.

1
  • @PimpJuiceIT I'm not quite sure what you mean. . . the batch file doesn't write the script itself, you copy and paste the code into a code editor, tweak it a little bit, then save it as a .VBS file. Oct 4, 2019 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.