11
Hello, since I started researching about these commands of CMD, this doubt about how to turn the output of a command into a variable has been the one that took me the most time.
I saw a lot of codes out there, many of them quoted that the command FOR could be used in that, but I don’t know how to use that command yet, I don’t understand its logic and even if someone could explain it to me, I would really 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 direct to a variable, indeed direct no, I believe that the data STDOUT were first directed to STDIN (with the >0 ) and then yes were used to define the variable. I’m not sure about this because although I stayed two days to get to this code, some rules of these commands are still unclear to me.
For example, for all I read about the redirectors and transponders, I shouldn’t have to put the 0 (zero) after input redirect ( < ), even so if you do not use it the command does not work.
Also there is a limitation in this code that I wanted to be able to bypass, the limitation is on the line breaks that various command outputs have. It seems not possible to define values of variables with line breaks, someone could help in this limitation?
For example, the output of the command below that would have a line break right at the beginning, could not be set as the value of the variable %abc%
.
reg query HKLM >0 && set /p abc=<0
Only the outputs that did not have any line breaks could be fully set as the value of a variable, I think we can get around this with some command that changes the text formatting.
someone can help improve it?
Updating
For information I’m using a file .BAT.
I made this post because I believe that transforming the output of a command into a variable is a good way to store the information obtained from this command. Once information has become a variable, you can query it, use it in conjunction with another command, change it and more.
Below is a piece what I’m doing here, you can see I’ve rerouted the output data from mountvol
for the variable %guid%
, then adjusted this data to define the parameter of another command. This enables the System Protection on the partition where the online system is installed, do not perform this unless you understand that these data will erase 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 turn the first line of an output into a variable (if any) and modify its characters using offset (~) or assignment (=), as it is possible to see above, but the ideal would be to be able to modify the characters of the entire output and not just the first line.
Correction
I made a mistake, guys, I’m not able to redirect the data from STDOUT for STDIN as I mentioned before, this is impossible for me yet, what I was doing was redirecting STDOUT to a file without named extension 0 (zero) that was created inside the folder System32 (directory of CMD).
It turns out that because of my misinterpretation of what I read here, I really thought I was redirecting to STDIN, that is represented by a zero, but it was worth the information.
You can test and confirm this, type the following command at the prompt and you will see that actually the data STDOUT of the first command are passed to the file 02825 which will be created in System32, if the first command is correctly executed the file 02825 will be opened by Notepad, showing the output STDOUT of the first command.
reg query HKLM >02825 && notepad 02825
You can see the file 02825 by Explorer with command Explorer /select,02825
and to exclude it can use del 02825
.
No news then, unless we now know that we do not need to record the output in a file with exactly extension, as a .txt.
Windows command prompt is quite limited compared to Windows Powershell. Have you tried to find out if you can do what you need using Powershell? http://technet.microsoft.com/pt-br/Library/bb978526.aspx
– Caffé
I’ve heard of that Powershell but I’ve never used it, so far I’m trying to CMD same war craft... as I mentioned not even know basic commands like the FOR still, I’m crawling in it.
– UnanTec
Since you’re just getting started, start with the best ;-)
– Caffé
I extended my comment a little in a reply. Good luck!
– Caffé