1.

Solve : my for command is not working?

Answer» <html><body><p>could someone please tell why this does not work<br/><br/>echo off<br/>setlocal enabledelayedexpansion<br/>for /<a href="https://interviewquestions.tuteehub.com/tag/f-236701" style="font-weight:bold;" target="_blank" title="Click to know more about F">F</a> "tokens=1-3 delims=" %%a in (REG QUERY "hklm\software\microsoft\windows nt\currentversion" /v CurrentVersion) do (<br/>if !%%c==! goto:eof<br/>echo %%c &gt;&gt;ver.txt<br/>)<br/><br/>single quotes needed around command <a href="https://interviewquestions.tuteehub.com/tag/string-11290" style="font-weight:bold;" target="_blank" title="Click to know more about STRING">STRING</a><br/>What error message do you get?<br/>Can you explain what this does?<br/>Orr what you expect it to do?<br/> Code: <a>[Select]</a>if !%%c==! goto:eof<br/>Have you already tried it with <strong>echo on</strong> to see what is happening?Geek, did you see my post? Did you bother to read it?<br/> Quote from: Salmon Trout on April 24, 2010, 05:20:01 PM</p><blockquote>Geek, did you see my post? Did you bother to read it?<br/></blockquote> Sorry I was typing when you posted .<br/>Why does it need the single quote?<br/>What error message do you get?<br/> Code: <a>[Select]</a>the system cannot find the file regCan you explain what this does?<br/> Code: <a>[Select]</a>writes the version number to ver.txtOrr what you expect it to do?<br/> Code: <a>[Select]</a>create a txt file called ver.txt with the windows version numberCode:<br/>Have you already tried it with echo on to see what is happening?<br/>this<br/> Code: <a>[Select]</a>C:\Documents and Settings\admin-matthew\Desktop&gt;setlocal enabledelayedexpansion<br/><br/><br/>C:\Documents and Settings\admin-matthew\Desktop&gt;for /F "tokens=1-3 delims=" %a i<br/>n (REG QUERY "hklm\software\microsoft\windows nt\currentversion" /v CurrentVersi<br/>on) do (<br/>if !%c == ! goto A<br/> echo %c  1&gt;ver.txt<br/>)<br/>The system cannot find the file REG.<br/><br/>C:\Documents and Settings\admin-matthew\Desktop&gt;echo teste<br/>teste<br/><br/>C:\Documents and Settings\admin-matthew\Desktop&gt;pause<br/>Press any key to continue . . .aahhhh!<br/>THIS IS WAY OVER MY HEAD!<br/> Code: <a>[Select]</a>Examples:<br/><br/>  REG QUERY HKLM\Software\Microsoft\ResKit /v Version<br/>    Displays the value of the registry value Version<br/><br/>  REG QUERY HKLM\Software\Microsoft\ResKit\Nt\Setup /s<br/>    Displays all subkeys and values under the registry key Setup<br/><br/>Microsoft Windows XP [Version 5.1.2600]<br/>(C) Copyright 1985-2001 Microsoft Corp.<br/><br/>C:\&gt;REG QUERY HKLM\Software\Microsoft\ResKit\Nt\Setup /s<br/><br/>Error:  The system was unable to find the specified registry key or val<br/><br/>C:\&gt;<br/> Code: <a>[Select]</a>echo off<br/>setlocal enabledelayedexpansion<br/>for /f "skip=4 tokens=1-3" %%a in ('REG QUERY "hklm\software\microsoft\windows nt\currentversion" /v currentversion') do (<br/>  if !%%c==! goto:eof<br/>  echo %%c &gt;&gt; ver.txt<br/>)<br/><br/><strong>A few notes:</strong> <br/><br/>The default delimiter is a space which is how the output is delimited. Overriding it with no delimiters defeats the purpose of the delims= parameter.<br/><br/>You need single quotes around a command in the <strong>for</strong> statement, and double quotes around parameters with embedded spaces (ie: windows nt).<br/><br/>Reg Query outputs lines which are not needed, easiest to skip over.<br/><br/>Good luck.  Quote from: Geek-9pm on April 24, 2010, 06:21:36 PM<blockquote>aahhhh!<br/>THIS IS WAY OVER MY HEAD!<br/> Code: <a>[Select]</a>Examples:<br/><br/>  REG QUERY HKLM\Software\Microsoft\ResKit /v Version<br/>    Displays the value of the registry value Version<br/><br/>  REG QUERY HKLM\Software\Microsoft\ResKit\Nt\Setup /s<br/>    Displays all subkeys and values under the registry key Setup<br/><br/>Microsoft Windows XP [Version 5.1.2600]<br/>(C) Copyright 1985-2001 Microsoft Corp.<br/><br/>C:\&gt;REG QUERY HKLM\Software\Microsoft\ResKit\Nt\Setup /s<br/><br/>Error:  The system was unable to find the specified registry key or val<br/><br/>C:\&gt;<br/><br/></blockquote> That's not the issue. The issue is that in a FOR <a href="https://interviewquestions.tuteehub.com/tag/loop-345124" style="font-weight:bold;" target="_blank" title="Click to know more about LOOP">LOOP</a>, to get the output of a command, you <a href="https://interviewquestions.tuteehub.com/tag/wrap-1461765" style="font-weight:bold;" target="_blank" title="Click to know more about WRAP">WRAP</a> it in ', to use a string you use " and to get the output from a file you don't use any quotes.<br/><br/>An example of each:<br/> Code: <a>[Select]</a>rem Command<br/>for /f "tokens=3" %%c in ('findstr  /c:price^: item.txt') do (<br/>     commands<br/>     commands<br/>)<br/> Code: <a>[Select]</a>rem String<br/>for /f "tokens=1,3-5" %%A in (this sentence is very long and will not have many parts displayed after) do echo %%A %%B %%C %%D<br/> Code: <a>[Select]</a>rem File<br/>rem The for loop will preform each task for as many lines are in the file, just like any<br/>rem other type of output.<br/>for /f "delims=," %%b in (filename.ext) do (<br/>     echo %%b<br/>)thank you <a href="https://interviewquestions.tuteehub.com/tag/sidewinder-7737530" style="font-weight:bold;" target="_blank" title="Click to know more about SIDEWINDER">SIDEWINDER</a> Quote from: Sidewinder on April 24, 2010, 06:40:51 PM<blockquote> Code: <a>[Select]</a>echo off<br/>setlocal enabledelayedexpansion<br/>for /f "skip=4 tokens=1-3" %%a in ('REG QUERY "hklm\software\microsoft\windows nt\currentversion" /v currentversion') do (<br/>  if !%%c==! goto:eof<br/>  echo %%c &gt;&gt; ver.txt<br/>)<br/><br/></blockquote> <br/>I cannot get the above code to run correctly. I'm using windows 7.<br/><br/><br/>C:\test&gt;type  matt2.bat<br/>echo off<br/>setlocal enabledelayedexpansion<br/>for /f "skip=4 tokens=1-3" %%a in ('REG QUERY "hklm\software\microsoft\windows n<br/>t\currentversion" /v currentversion') do (<br/>  if !%%c==! goto:eof<br/>  echo %%c &gt;&gt; ver.txt<br/>)<br/><br/>C:\test&gt;matt2.bat<br/><br/>C:\test&gt;type  ver.txt<br/><strong>The system cannot find the file specified.</strong><br/><br/>C:\test&gt;<br/><br/>What file cannot be found?    I tested the code as given.  <br/><br/>Please help.The original code is so amazingly, awfully, ridiculously, mind-boggingly wrong that I grow suspicious. It reads like code that was designed by somebody who thinks that keywords and commands are like Lego bricks that can be snapped together to produce something that works, without the snapper having to do any thinking or learning. Alternatively it reads like a piece of working batch code that somebody has deliberately edited to introduce as many glaring errors as possible. Who would do that? Somebody who starts threads as one person as then jumps in as another with an "answer". A pathetic individual like Greg/Marvin/Bill? <br/><br/><br/>echo off<br/>setlocal enabledelayedexpansion<br/>for /f "tokens=1-3 delims=" %%a in (REG QUERY "hklm\software\microsoft\windows nt\currentversion" /v CurrentVersion) do (<br/><br/>                  ^                                                                                 ^<br/>              nonsense; 3 tokens when                                            commands here need single quotes<br/>              the delims are the start and<br/>              end of the line?<br/><br/>if !%%c==! goto:eof<br/>       ^<br/>    Mad IF test and strange mishmash of<br/>    loop metavariable and delayed ordinary<br/>    variable that does not even make sense<br/>    and a crazy goto<br/><br/><br/>echo %%c &gt;&gt;ver.txt<br/>         ^<br/>and APPEND the (never to appear) result to poor little ver.txt? <br/><br/>)<br/><br/><br/>I presume the idea is to extract the NT major and minor version numbers. Maybe running the REG command at the prompt would have been a good starting point? Anyhow, it's all nonsense anyhow, I reckon. (See above)<br/><br/><br/><br/>Salmon Trout I just can't make for loops.<br/><br/><br/>marvinengland the batch file takes a second to make the file<br/>also it is going to say 6.1 not 7 Code: <a>[Select]</a>echo off<br/>set commandstring='REG QUERY "hklm\software\microsoft\windows nt\currentversion" /v currentversion'<br/>for /f "skip=2 tokens=1-3" %%a in (%commandstring%) do set versionstring=%%c<br/>echo %versionstring%<br/>if "%versionstring%"=="6.1" echo Windows 7<br/>    Quote from: mat123 on April 24, 2010, 04:44:20 PM<blockquote>Would someone please tell why this does not work.<br/></blockquote> <br/><br/>C:\test&gt;type ver617.bat<br/> Code: <a>[Select]</a>echo  off<br/>for /f "skip=1 tokens=1-4" %%i in ('ver') do (<br/>echo %%k %%l<br/>set ver61=%%i %%j %%k %%l<br/>)<br/>echo %ver61%<br/>if "%ver61%"=="Microsoft Windows [Version 6.1.7600]" echo Windows 7<br/><strong>OUTPUT:</strong><br/><br/>C:\test&gt;ver617.bat<br/>[Version 6.1.7600]<br/>Microsoft Windows [Version 6.1.7600]<br/>Windows 7<br/>C:\test&gt;</body></html>


Discussion

No Comment Found