Scripting on a Unix terminal is so darn easy – the tools are documented in the man pages, online or from a buddy on the phone. Windows batch scripting was another beast entirely. It took me nearly an hour to craft this simple script, which honestly would have been as easy as a more/grep/awk pipe on Unix.
Anyway, here is my script to search for a string in a file, and print out the key to said file. Before I reveal the script, this is what my data looks like. I’m trying to pull out and echo just aedfc45.
IP:port : 0.0.0.0
Target : aedfc45
Application ID : {00000000-0000-0000-0000-000000000000}
Certificate Store Name : MYI’m sure my solution is not the best (this is my first batch script, ever). Note, I broke this single line in two in case you try running it with your own inputs.
more test.txt | for /F "tokens=4 delims= " %X in ('findstr /c:"Target"') do echo %X
What was difficult to understand was that the for command was handling much more than I felt it should be handling. It was not only looping over input, applying the command in single quotes to each line, it was also delimiting and tokenizing the output with the “tokens” and “delims” commands contained in double quotes. What a strange implementation of for.
