1

I have run this command each line in cmd and works fine. How to run this command in cmd in one line?

I have try use & but i get different result if i run command line by line and use &.

for /f %i in ('getmac^|find "-"') do set str=%i
set string=%str:-=%
REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318} /s | findstr "\<%string%\>"

Below I attach a printscreen

Command run line by line

Command run with &

Krikil
  • 15
  • 6

2 Answers2

1

Try this here:

for /f %i in ('getmac^|find "-"') do (set str=%i& set string=!str:-=!& REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318} /s | findstr \^<!string!\^>)

Note the parentheses around the compound statement, and the caret to escape the < > redirection. And if you need to enable delayed variable substitution in your cmd, use this:

cmd /v:on /c "for /f %i in ('getmac^|find "-"') do (set str=%i& set string=!str:-=!& REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318} /s | findstr \^<!string!\^>)"
yacc
  • 2,915
  • 4
  • 19
  • 33
  • Thansk for your answer but this is not work. for /f %i in ('getmac^|find "-"') do (set str=%i && set string=%str:-=% && REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318} /s | findstr "\<%string%\>") – Krikil Aug 16 '17 at 04:55
  • just updated my answer – yacc Aug 16 '17 at 06:00
  • I have try run for /f %i in ('getmac^|find "-"') do (set str=%i && set string=!str:-=! && REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318} /s | findstr "\<!string!\>") and cmd /v:on /c "for /f %i in ('getmac^|find "-"') do (set str=%i && set string=!str:-=! && REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318} /s | findstr "\<!string!\>")" in cmd but not work. – Krikil Aug 16 '17 at 06:44
  • Thanks for your help but your answer still not solve my problem – Krikil Aug 16 '17 at 06:45
  • No problem. If you run the last one (with cmd /v:on...) in your cmd what do you get? And what operating system are you on? – yacc Aug 16 '17 at 06:48
  • If i run with cmd /v:on... i get The system cannot find the file specified. I use Windows XP sp 3. – Krikil Aug 16 '17 at 06:59
  • What do you see when you run `cmd /v:on /c "for /f %i in ('getmac^|find "-"') do (set str=%i && echo str=!str!)"` ? Please provide all output. Normally I'd see the for loop echo its substituted command before executing it. I'll switch to XP in a minute. – yacc Aug 16 '17 at 07:14
  • If i run cmd /v:on /c "for /f %i in ('getmac^|find "-"') do (set str=%i && echo str=!str!)" i get result (set str=00-AA-22-BB-33-CC && echo str=!str! ) str=00-AA-22-BB-33-CC – Krikil Aug 16 '17 at 07:22
  • Sorry i cannot attach printscreen. – Krikil Aug 16 '17 at 07:23
  • It's ok, I got it. At least the for loop is working correctly. Next step would be to check this: `cmd /v:on /c "for /f %i in ('getmac^|find "-"') do (set str=%i && set string=!str:-=! && echo string=!string!)"` – yacc Aug 16 '17 at 07:26
  • If i run cmd /v:on /c "for /f %i in ('getmac^|find "-"') do (set str=%i && set string=!str:-=! && echo string=!string!)" i get result (set str=00-AA-22-BB-33-CC && set string=!str:-=! && echo string=!string! ) string=00AA22BB33CC – Krikil Aug 16 '17 at 07:40
  • Then what do you get with `REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318} /s | findstr "\<00AA22BB33CC\>")` ? – yacc Aug 16 '17 at 08:10
  • If i run REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318} /s | findstr "\<00AA22BB33CC\>" without ")" at the end of command i get result NetworkAddress REG_SZ 00AA22BB33CC. This result exactly as I expected. – Krikil Aug 16 '17 at 08:29
  • Ok. But then I don't understand why the composed command failed. And what do you get with `cmd /v:on /c "REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-1‌​1CE-BFC1-08002bE1031‌​8} /s | findstr "\<00AA22BB33CC\>""` ? – yacc Aug 16 '17 at 08:35
  • If i run cmd /v:on /c REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318} /s | findstr "\<00AA22BB33CC\>" without "" at the beginning and end of command i get result NetworkAddress REG_SZ 00AA22BB33CC. This result exactly as I expected. – Krikil Aug 16 '17 at 08:52
  • I just learned that you need to escape the redirection symbols with a caret like so `^<` `^>`. Could you run that command: `cmd /v:on /c "for /f %i in ('getmac^|find "-"') do (set str=%i && set string=!str:-=! && REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318} /s | findstr "\^<!string!\^>")"` ? I admit the whole command string is looking a bit like a hack now. :p – yacc Aug 16 '17 at 09:11
  • If i run cmd /v:on /c "for /f %i in ('getmac^|find "-"') do (set str=%i && set string=!str:-=! && REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318} /s | findstr "\^<!string!\^>")" and export output to text file i get text file contains 1213 lines of sentences. I think this command show all registry in HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318} – Krikil Aug 16 '17 at 09:29
  • And if you run this: `cmd /v:on /c "for /f %i in ('getmac^|find "-"') do (set str=%i && set string=!str:-=! && echo "\^<!string!\^>")"`, what do you get? I'm trying to work towards your problem here on XP. – yacc Aug 16 '17 at 09:50
  • If i run cmd /v:on /c "for /f %i in ('getmac^|find "-"') do (set str=%i && set string=!str:-=! && echo "\^<!string!\^>")" i get result (set str=00-AA-22-BB-33-CC && set string=!str:-=! && echo "\<!string!\>" ) "\<00AA22BB33CC \>" – Krikil Aug 16 '17 at 09:53
  • I just updated my answer. Please test that cmd /v:on ... command and let me know. – yacc Aug 16 '17 at 10:53
  • Work perfect like magic. Thank you very much – Krikil Aug 16 '17 at 12:33
  • I have been looking at this since 7 am here and really appreciate your feedback. Since I'm also preferred user of XP SP3 I just wanted to get this sorted out. Thank you, too. – yacc Aug 16 '17 at 12:34
0

You can use the & character to separate lines, as shown in this question.

Johannes Riecken
  • 2,301
  • 16
  • 17
  • It is not work if i use & – Krikil Aug 16 '17 at 04:41
  • It doesn't work in Kace K1000? It works in cmd for me. What error message do you get? – Johannes Riecken Aug 16 '17 at 04:48
  • If i run command in cmd line by line work fine but if i use & this is not give result like run command line by line. – Krikil Aug 16 '17 at 04:51
  • What result does it give then and how is it different? – Johannes Riecken Aug 16 '17 at 04:52
  • If i run in cmd line by line i get result NetworkAddress REG_SZ 00AA22BB33CC. If i use & i get result set str=00-AA-22-BB-33-CC & set string=%str:-=% & REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318} /s | findstr "\<%string%\>" – Krikil Aug 16 '17 at 05:00
  • You also need to take care of delayed variable expansion. Use `!str:-=!` and `!string!`. I edited my answer, see there for a sample. – yacc Aug 16 '17 at 05:24