1.

Solve : my for command is not working?

Answer»

could someone please tell why this does not work

echo off
setlocal enabledelayedexpansion
for /F "tokens=1-3 delims=" %%a in (REG QUERY "hklm\software\microsoft\windows nt\currentversion" /v CurrentVersion) do (
if !%%c==! goto:eof
echo %%c >>ver.txt
)

single quotes needed around command STRING
What error message do you get?
Can you explain what this does?
Orr what you expect it to do?
Code: [Select]if !%%c==! goto:eof
Have you already tried it with echo on to see what is happening?Geek, did you see my post? Did you bother to read it?
Quote from: Salmon Trout on April 24, 2010, 05:20:01 PM

Geek, did you see my post? Did you bother to read it?
Sorry I was typing when you posted .
Why does it need the single quote?
What error message do you get?
Code: [Select]the system cannot find the file regCan you explain what this does?
Code: [Select]writes the version number to ver.txtOrr what you expect it to do?
Code: [Select]create a txt file called ver.txt with the windows version numberCode:
Have you already tried it with echo on to see what is happening?
this
Code: [Select]C:\Documents and Settings\admin-matthew\Desktop>setlocal enabledelayedexpansion


C:\Documents and Settings\admin-matthew\Desktop>for /F "tokens=1-3 delims=" %a i
n (REG QUERY "hklm\software\microsoft\windows nt\currentversion" /v CurrentVersi
on) do (
if !%c == ! goto A
 echo %c  1>ver.txt
)
The system cannot find the file REG.

C:\Documents and Settings\admin-matthew\Desktop>echo teste
teste

C:\Documents and Settings\admin-matthew\Desktop>pause
Press any key to continue . . .aahhhh!
THIS IS WAY OVER MY HEAD!
Code: [Select]Examples:

  REG QUERY HKLM\Software\Microsoft\ResKit /v Version
    Displays the value of the registry value Version

  REG QUERY HKLM\Software\Microsoft\ResKit\Nt\Setup /s
    Displays all subkeys and values under the registry key Setup

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>REG QUERY HKLM\Software\Microsoft\ResKit\Nt\Setup /s

Error:  The system was unable to find the specified registry key or val

C:\>
Code: [Select]echo off
setlocal enabledelayedexpansion
for /f "skip=4 tokens=1-3" %%a in ('REG QUERY "hklm\software\microsoft\windows nt\currentversion" /v currentversion') do (
  if !%%c==! goto:eof
  echo %%c >> ver.txt
)

A few notes:

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.

You need single quotes around a command in the for statement, and double quotes around parameters with embedded spaces (ie: windows nt).

Reg Query outputs lines which are not needed, easiest to skip over.

Good luck.  Quote from: Geek-9pm on April 24, 2010, 06:21:36 PM
aahhhh!
THIS IS WAY OVER MY HEAD!
Code: [Select]Examples:

  REG QUERY HKLM\Software\Microsoft\ResKit /v Version
    Displays the value of the registry value Version

  REG QUERY HKLM\Software\Microsoft\ResKit\Nt\Setup /s
    Displays all subkeys and values under the registry key Setup

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>REG QUERY HKLM\Software\Microsoft\ResKit\Nt\Setup /s

Error:  The system was unable to find the specified registry key or val

C:\>

That's not the issue. The issue is that in a FOR LOOP, to get the output of a command, you WRAP it in ', to use a string you use " and to get the output from a file you don't use any quotes.

An example of each:
Code: [Select]rem Command
for /f "tokens=3" %%c in ('findstr  /c:price^: item.txt') do (
     commands
     commands
)
Code: [Select]rem String
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
Code: [Select]rem File
rem The for loop will preform each task for as many lines are in the file, just like any
rem other type of output.
for /f "delims=," %%b in (filename.ext) do (
     echo %%b
)thank you SIDEWINDER Quote from: Sidewinder on April 24, 2010, 06:40:51 PM
Code: [Select]echo off
setlocal enabledelayedexpansion
for /f "skip=4 tokens=1-3" %%a in ('REG QUERY "hklm\software\microsoft\windows nt\currentversion" /v currentversion') do (
  if !%%c==! goto:eof
  echo %%c >> ver.txt
)


I cannot get the above code to run correctly. I'm using windows 7.


C:\test>type  matt2.bat
echo off
setlocal enabledelayedexpansion
for /f "skip=4 tokens=1-3" %%a in ('REG QUERY "hklm\software\microsoft\windows n
t\currentversion" /v currentversion') do (
  if !%%c==! goto:eof
  echo %%c >> ver.txt
)

C:\test>matt2.bat

C:\test>type  ver.txt
The system cannot find the file specified.

C:\test>

What file cannot be found?    I tested the code as given. 

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?


echo off
setlocal enabledelayedexpansion
for /f "tokens=1-3 delims=" %%a in (REG QUERY "hklm\software\microsoft\windows nt\currentversion" /v CurrentVersion) do (

                  ^                                                                                 ^
              nonsense; 3 tokens when                                            commands here need single quotes
              the delims are the start and
              end of the line?

if !%%c==! goto:eof
       ^
    Mad IF test and strange mishmash of
    loop metavariable and delayed ordinary
    variable that does not even make sense
    and a crazy goto


echo %%c >>ver.txt
         ^
and APPEND the (never to appear) result to poor little ver.txt?

)


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)



Salmon Trout I just can't make for loops.


marvinengland the batch file takes a second to make the file
also it is going to say 6.1 not 7 Code: [Select]echo off
set commandstring='REG QUERY "hklm\software\microsoft\windows nt\currentversion" /v currentversion'
for /f "skip=2 tokens=1-3" %%a in (%commandstring%) do set versionstring=%%c
echo %versionstring%
if "%versionstring%"=="6.1" echo Windows 7
    Quote from: mat123 on April 24, 2010, 04:44:20 PM
Would someone please tell why this does not work.


C:\test>type ver617.bat
Code: [Select]echo  off
for /f "skip=1 tokens=1-4" %%i in ('ver') do (
echo %%k %%l
set ver61=%%i %%j %%k %%l
)
echo %ver61%
if "%ver61%"=="Microsoft Windows [Version 6.1.7600]" echo Windows 7
OUTPUT:

C:\test>ver617.bat
[Version 6.1.7600]
Microsoft Windows [Version 6.1.7600]
Windows 7
C:\test>


Discussion

No Comment Found