Hello, since I started searching on these CMD commands, this question about how to turn the output of a command into a variable has been the one that took the most time.
I've seen many codes out there, many quoted that the FOR command could be used in this, but I do not know how to use this command yet, I did not understand its logic and even if someone could explain it to me I would appreciate it.
Well, the code with which I was able to redirect the output of a command to a variable.
mountvol %SystemDrive% /l >0 && set /p abc=<0
With this command I finally managed to redirect the output of Mountvol directly to a variable, not directly direct, I believe the STDOUT data was first directed to STDIN (with > 0 ) and then they were used to define the variable. I'm not sure why even though I've stayed two days to get into this code, some rules of those commands are still obscure for me.
For example, for all I read about redirectors and identifiers, I should not have to put 0 (zero) after the input redirector ), even so if you do not use it the command does not work.
In addition there is a limitation in this code that I wanted to be able to work around, the limitation is on the line breaks that multiple command outputs have. It seems that it is not possible to define variable values with line breaks, could anyone help with this limitation?
For example, the output of the command below that would have a line break early in the beginning could not be set to the value of the variable %abc%
.
reg query HKLM >0 && set /p abc=<0
Only outputs that do not have any line breaks could be fully set to the value of a variable, I think you can get around this with some command that changes the text formatting.
Can anyone help improve this?
Update
For information I'm using a .BAT file.
I made this post because I believe that turning the output of a command into a variable is a good way to save the information obtained from that command. Once the information has become a variable, you can query it, use it in conjunction with another command, change it, and more.
Below is a piece of what I'm doing here, you can see that I redirected the output data from mountvol
to the %guid%
variable, then I set that data to set the other command. This enables System Protection on the partition where the online system is installed, do not run this unless you understand that this data will delete those that already define your current settings. In this case the codes are for a .BAT .
@echo off
::
set chv=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SPP\Clients
set vlr={09F7EDC5-294E-4180-AF6A-FB0E6A0E9513}
set tip=REG_MULTI_SZ
::
mountvol %SystemDrive% /l >0 && set /p guid=<0
::
reg add "%chv%" /v "%vlr%" /t %tip% /d "%guid: =%:(%SystemDrive:~,1%%%3A)" /f && cls && echo Comando executado com sucesso! && goto fim
::
cls
echo Houve algum erro, execute novamente como Administrador.
::
:fim
pause>nul
exit
I can then transform the first line of an output into a variable (if it exists) and modify its characters using the ~ ) or assignment ( = ), as you can see above, but the ideal would be to be able to modify the characters of the whole output and not only those of the first line.
Correction
I was wrong, I am not able to redirect the STDOUT data to STDIN as I mentioned before, this is impossible for me yet, what I was doing was redirect STDOUT directory for a file without extension named 0 (zero) that was created inside the System32 ). It turns out that for my misinterpretation of what I read > here , I even thought I was redirecting to STDIN , which is represented by a zero, but worth the info. You can test and confirm this by typing the following command at the prompt and you will see that the STDOUT data of the first command is actually passed to the 02825 file that will be created in < If the first command is executed correctly, the 02825 file will open through Notepad, showing the STDOUT output of the first command.
reg query HKLM >02825 && notepad 02825
You can see the file 02825 by Explorer with the command Explorer /select,02825
and to delete it you can use del 02825
.
No news then, unless we now know we do not need to write the output to a file with an extension exactly like a .txt .