Search code examples
regexbatch-filefindstr

How can I use FINDSTR to place a serial number from a txt file in a variable?


I have a script that outputs a serials.txt file like this:

Serial Number
88PRDQ1


Serial Number
CZSQZV1

I would like to capture the service tag in a variable for future manipulation in the script.

My code seems to fail entirely and I can't find much in the way of what I am looking for with FINDSTR.

for /f "delims=" %%S in (serials.txt) do (findstr /r "^......[^ ]")

Since the output can be 6-7 long and any letter or number, I am struggling to get it to behave properly. It runs forever at the moment and I am unsure how to output the result to a variable and act on it if I somehow managed to get this line correct.

The [^ ] is meant to exclude spaces but accept anything else so as to not print the line that says "Serial Number" in the event of a 6 char serial.

What am I missing?


Solution

  • findstr /v /c:"Serial Number" serials.txt|findstr "."
    

    First finds all line not containing the string "Serial Number", then filters any line containing at least one character to suppress empty lines.