user2820524
user2820524

Reputation: 1

Batch script output to text using a variable to name the output file

I have never written a batch script before but would like to use one to run & write the output of a command line utility I use on a regular basis. I would like it to write to a text file using a string returned by the utility as a file name (serial number in this case). There are a few problems here. The code ideally would:

  1. use the utility to query the serial number of a device
  2. parse the output of that query using the "=" delimiter, as it returns "serial = ##########" and I just want the number.
  3. create a .txt file with the number as the filename, i.e ##########.txt
  4. fill that file with information from subsequent calls of the utility using >> redirector

I have a bit of it in place, but I am struggling with having it parse the output into a variable and then use that variable to name the text file. Currently it looks like:

  1. utility > loo.txt
  2. set /p sn=< loo.txt (here is where I would like it to delimit by = but can't figure out how)
  3. utility >> %sn%.txt (where I would like it to append another call of the utility to the unique file for this iteration of the script)

Is this even possible? Helps!

Upvotes: 0

Views: 1921

Answers (1)

David Ruhmann
David Ruhmann

Reputation: 11367

Something like this should work

@echo off
for /f "tokens=2 delims== " %%A in ('utility') do set "SN=%%~A"
utility>>"%SN%.txt"

See these great Batch resources

Upvotes: 1

Related Questions