0

I want to redirect a file to a text file with a specific filename. The filename should be the serial number of the machine. I currently have the below command but for some reason, the output is different from what I want.

@echo off
set serial=wmic bios get serialnumber
set bit=manage-bde -status C:
%bit% > "C:\Users\Frangon\Desktop\%serial%.txt"
pause

It redirects the %bit% to a text file but the filename becomes wmic bios get serialnumber instead of the SERIAL NUMBER of the machine.

Please help as I'm stuck.

Compo
  • 36,585
  • 5
  • 27
  • 39
Duy Gonzales
  • 43
  • 2
  • 12
  • 1
    You've set a variable to a string value, not a command result. To save the variable as a command result you'll need a `For /F` loop. There are hundreds of examples of this on the site, please search. – Compo Dec 27 '18 at 22:55
  • Would you please direct me Compo? Thank you sir. – Duy Gonzales Dec 27 '18 at 23:00
  • 1
    From the site search, _at the top of the page_, the top result from, `[batch-file]save result command variable`, will be a possible duplicate i.e. [Save the result of a command in variable, Windows batch](https://stackoverflow.com/questions/33215819/save-the-result-of-a-command-in-variable-windows-batch) – Compo Dec 27 '18 at 23:17

1 Answers1

1

As said in a comment before, the way to get a command's output to a variable is a for /f loop. There is one quirk with wmic: it has unusual line endings CR CR LF. The first CR will be part of the variable and will cause bad things. One way to overcome that (and the most reliable way) is parsing the output with another for loop.

There is no need to assign the output of manage-bde -status C: to a variable (would be very difficult in batch anyway because it's more than one line). Just redirect the output directly to the file:

@echo off
for /f "tokens=2 delims==" %%a in ('wmic bios get serialnumber /value') do for %%b in (%%a) do set "serial=%%b"
manage-bde -status C: > "%USERPROFILE%\Desktop\%serial%.txt"
Stephan
  • 53,940
  • 10
  • 58
  • 91