Issue while reading text file from batch script - parsing

While reading text file from batch script am not able to get the values which are already present.
doxygen.txt
Version
8.0.56336(There is no space between version and value)
test.bat
#echo off
setlocal EnableDelayedExpansion
set num=0
if %errorlevel%==0 (
FOR /F "tokens=* delims=" %%a IN ('"wmic product where "Name like 'Microsoft Visual C++ 2005 Redistributable'" get version"') do (
echo %%%a >> doxygen.txt
)
rem FOR /F "tokens=* delims=" %%x in (doxygen.txt) DO echo %%x
for /f "tokens=* delims=" %%i in (doxygen.txt) do (
set /a num+=1
set v[!num!]=%%i
)
del doxygen.txt
set line1=%v[1]%
set line2=%v[2]%
set line3=%v[3]%
set line4=%v[4]%
echo line1: %line1%
echo line2: %line2%
echo line3: %line3%
echo line4: %line4%
endlocal
)
)
Here am not able to get the values to line1,line2 etc.
Can anyone please suggest me where I have done the mistake.
Thanks In Advance.

Batch expands percent expression when a line or parenthesis block is parsed before it is executed.
So set line1=%v[1]% will simply expand to nothing, as it expands before the FOR loop even starts.
But you can use the delayed expansion syntax, as these are expanded at runtime.
#echo off
setlocal EnableDelayedExpansion
set num=0
if %errorlevel%==0 (
FOR /F "tokens=* delims=" %%a IN ('"wmic product where "Name like 'Microsoft Visual C++ 2005 Redistributable'" get version"') do (
echo %%a>> doxygen.txt
)
rem FOR /F "tokens=* delims=" %%x in (doxygen.txt) DO echo %%x
for /f "tokens=* delims=" %%i in (doxygen.txt) do (
set /a num+=1
set "v[!num!]=%%i"
)
del doxygen.txt
set "line1=!v[1]!"
set "line2=!v[2]!"
set "line3=!v[3]!"
set "line4=!v[4]!"
echo line1: !line1!
echo line2: !line2!
echo line3: !line3!
echo line4: !line4!
)
endlocal

if you're just trying to get the version from the output, here's an easier way to do it:
1:
#echo off
setlocal EnableDelayedExpansion
set num=0
if %errorlevel%==0 (
FOR /F "skip=1 tokens=* delims=" %%v IN ('wmic product where "Name like 'Microsoft Visual C++ 2005 Redistributable'" get version ^| findstr "."') do (
echo Version: %%v
set mc_vcpp_05=%%v
)
)
endlocal
2:
Here's if you need to read that file for some reason:
#echo off
if %errorlevel%==0 (
FOR /F "skip=1 tokens=1 delims= " %%a IN ('wmic product where "Name like 'Microsoft Visual C++ 2005 Redistributable'" get version ^| findstr "."') do (
echo %%a >> doxygen.txt
)
for /f "tokens=1* delims=" %%I in ('type doxygen.txt') do (
echo Version: %%I
)
del doxygen.txt
)
Why it's behaving for you in your original script, I don't know yet. Ill edit or comment my answer when I find out

Related

Windows cmd: Parsing output from Ping

I would like to execute a ping and, after evaluating the output, return a single line like "0 - check_ping rta=1 loss=0".
0 and 1 being extracted from the ping, of course.
I came this far:
#setlocal enableextensions enabledelayedexpansion
#echo off
set ip=%1
set num=%2
for /f "tokens=* delims= " %%f in ('ping -n %num% %ip%') do (
set line=%%f
call :processLine
)
echo 0 myTestService loss=!loss! rta=%rta:~1,-2% further information for myTestService
GOTO:EOF
:processLine
for /f "tokens=1,2,3,4,5,6 delims==(%%" %%A in ("%line%") do (
if "%%A" == "Packets: Sent " (
set loss = %%E
echo DD%%E
echo Loss is [!loss!]
)
if "%%A" == "Minimum " (
set rta=%%D
echo RTA is [!rta:~1,-2!]
)
)
It doesn't work and I asume that it is because of some runtime problems. Obviously I am not a cmd expert, but I read that those for-loops can be tricky in regard of setting variables and accessing them later on.
Any help is greatly appreciated.
Warmest regards, freiraum
Here's an example which doesn't require delayed expansion, and, for efficiency, uses find.exe, to process only the lines from which you wish to extract substrings, and without calling a label:
#Echo Off
SetLocal EnableExtensions
If "%~2" == "" Exit /B
Set "loss="
For /F "Tokens=6,8 Delims== " %%G In ('%SystemRoot%\System32\PING.EXE -n %~2 %~1
^| %SystemRoot%\System32\find.exe ","') Do If Not Defined loss (Set "loss=%%H"
) Else Set "rta=%%G"
If Not Defined loss Exit /B
Echo 0 myTestService loss=%loss:~1,-1% rta=%rta:~,-2% further information for myTestService
The following code does what I want it to. There are still mistakes ("tokens=* delims= "), but these seem negligible - at least I hope so.
#setlocal enableextensions enabledelayedexpansion
#echo off
set ip=%1
set num=%2
for /f "tokens=* delims= " %%f in ('ping -n %num% %ip%') do (
call :processLine "%%f"
)
echo 0 myTestService loss=!loss:~1! rta=!rta:~0,-2! further information for myTestService
GOTO:EOF
:processLine
for /f "tokens=1,9,11 delims= " %%A in ("%~1") do (
if "%%A" == "Packets:" (
set loss=%%C
rem echo Loss is !loss:~1!
)
if "%%A" == "Minimum" (
set rta=%%B
rem echo RTA is !rta:~0,-2!
)
)

Why is for /f pulling up the file name?

I have a text file named paths.txt It contains the following data,
LocalName RemotePath
N: \DANIEL-HP\Users\Public
Z: \DANIEL-HP\Users\Public\Favorites
LocalName RemotePath
N: \DANIEL-HP\Users\Public
Z: \DANIEL-HP\Users\Public\Favorites
I am running the following batch file to extract the desired info path letter, path.
#echo off
for /f "tokens=1 delims=" %%a in ('find ":" paths.txt') do (
echo %%a
)
pause
The result
---------- PATHS.TXT
N: \\DANIEL-HP\Users\Public
Z: \\DANIEL-HP\Users\Public\Favorites
N: \\DANIEL-HP\Users\Public
Z: \\DANIEL-HP\Users\Public\Favorites
Press any key to continue . . .
The way that I fix this is I modify the FOR as below,
for /f "skip=2 tokens=1 delims=" %%a in ('find ":" paths.txt') do (echo %%a)
And I get a clean desired output,
N: \\DANIEL-HP\Users\Public
Z: \\DANIEL-HP\Users\Public\Favorites
N: \\DANIEL-HP\Users\Public
Z: \\DANIEL-HP\Users\Public\Favorites
Press any key to continue . . .
Question is, what is wrong with my first FOR function that it is allowing the filename to passthrough.
The problem is your FIND command, not your FOR /F.
FIND always prints out the name of the file like that if it opens the file itself (if the file name is passed in as an argument).
You can avoid the file name by using redirection:
find ":" <paths.txt
or a pipe:
type paths.txt | find ":"
To use these in a FOR loop, the pipe or redirection has to be escaped.
for /f "tokens=1 delims=" %%a in ('find ":" ^< paths.txt') do echo %%a
for /f "tokens=1 delims=" %%a in ('type paths.txt ^| find ":"') do echo %%a
Another alternative is to use FINDSTR instead (but only if the file is ASCII):
findstr ":" paths.txt
If the file is unicode (UTF-16), then you can convert the content to ASCII by piping the output of the TYPE command (again, escape the pipe if used within FOR /F):
type paths.txt | findstr ":"
need to be deleted ;))
/*
i advice:
find ":" ^<paths.txt
type paths.txt ^| find ":"
findstr working great.. i not understand what the blank you have
#echo off
set "tmp=PATHS.TXT"----your value
echo,---findstr
for /f "tokens=1 delims=" %%a in ('findstr ":" %tmp%') do (echo %%a)
echo,---type
for /f "tokens=1 delims=" %%a in ('type %tmp% ^| find ":"') do (echo %%a)
echo,---redirect
for /f "tokens=1 delims=" %%a in ('find ":" ^<%tmp%') do (echo %%a)
*/

Parsing contents of file in CMD/Batch not working?

I have a simple text file with some numbers in it:
1122
2244
But when I run the CMD
FOR /F %i IN (CIFLIST.TXT) DO ECHO %i
Nothing is output to the screen?
I've also tried from a batch file as
FOR /F %%i IN (CIFLIST.TXT) DO ECHO %%i
But again, no output? Any ideas?
[I've been through How do you loop through each line in a text file using a windows batch file? but can't find anything obvious)
try this:
FOR /F "delims=" %%i IN ('type CIFLIST.TXT') DO ECHO %%i
FOR /F "delims=" %%i IN ('cmd /A /C type CIFLIST.TXT') DO ECHO %%i

parse batch line by line

i am trying to parse the output of another function which is output line by line. to understand the function, it returns several lines of parameter and numbers like "top=123456789" or "low=123456789" (without the quotations) -
i try to parse the lines now with
for /F "delims=" %%a in ('%%I ^| findstr top') do set updir=%%1
set "updir=%1:~4%"
echo. %updir%
i am trying to get the pure numbers by trimming the known keywords like top, which would need then to be set to a var to return (%~1% ???) to a calling function back (other batch file).
could anyone help me with this please? shure it would be better to trim right from "=".
UPDATE:
this is the code returning the lines from the script i linked. i tried several ways to parse the return but i seem to be blind or too stupid to see, all is going weird.
for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0" "%URL%"') do (
rem process the HTML line-by-line
org echo(%%I
try1 (echo %%I|findstr top
try2 for /F "delims=" %%a in ('%%I ^| findstr top') do set updir=%%a
try2 echo. %updir%
try3 for /F "delims=" %%a in ('%%I') do findstr top
try3 echo. %2%
)
didn't work either
for /F "tokens=1,2delims==" %%a in ('%%I') do if %1 == top set updir=%%b
echo %updir%
i tried both delim version beneath (too the tokens/delims version) but i don't get it right.
UPDATE SOLUTION:
for the ones reading the question here some additional comment:
rem trim whitespace from beginning and end of line
for /f "tokens=*" %%x in ("%%~I") do set "line=%%x"
rem test that trimmed line matches "variable=number"
to find a single item like e.g. "top" you have to add "to" or adjust whole first token
echo !line! | findstr /i "^to[a-z]=[0-9]" >NUL && (
rem test was successful. Scrape number.
for /f "tokens=2 delims==" %%x in ("%%I") do set "value=%%x"
echo !value!
)
If all you wish to do is to is to skip all lines until you find one that matches "text=numerals", then scrape the numeric portion of that line, all you need to do is this:
for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0" "%URL%"') do (
rem trim whitespace from beginning and end of line
for /f "tokens=*" %%x in ("%%~I") do set "line=%%x"
rem test that trimmed line matches "variable=number"
echo !line! | findstr /i "^[a-z]*=[0-9]*$" >NUL && (
rem test was successful. Scrape number.
for /f "tokens=2 delims==" %%x in ("%%I") do set "value=%%x"
)
)
I think that's right, anyway. I didn't test it.
But I suspect that this is not going to work as you intend, since what you are scraping will probably include HTML tags. We will probably not be able to help you scrape the HTML unless you pastebin the HTML source of an example page, and explain what you wish to scrape from that source example.
does this fit your needs?
for /F "delims=" %%a in ('type file.txt ^| findstr "top low"') do set /a %%a
set top
set low
echo %top%, %low%
Try this:
for /F "tokens=2delims==" %%a in ('findstr top file.txt') do set "updir=%%a"
echo.%updir%
According to your comment my new code:
#echo off &setlocal enabledelayedexpansion
set "string=%%I"
set "string=!string:*top=!"
for /f "delims== " %%z in ("!string!") do set "string=%%z"
echo !string!
.. output:
123456789
Edit2: "added.

FINDSTR cannot open path with spaces

Drag and drop .bat, it takes the files,
puts it in 2 lists and process both list simultanously.
I passed a path : X:\folder\folder number\begin.txt into %1
it's working fine if i have foldernumber
PUSHD %~dp0
:loop
IF ["%~f1"] EQU [""] goto :out
echo %~f1>>list
set /a count+=1
SHIFT
goto :loop
POPD
:out
if %count% LEQ 1 (copy list list1 && goto :START)
set /a count2=%count%/2
more /e +%count2% list > list2
set count=0
setlocal enabledelayedexpansion
for /F "eol=; tokens=* delims=," %%i in (list) do (
set /a count+=1
if !count! leq !count2! echo %%i >>list1
)
endlocal
:: Create the 2nd .bat for multiprocessing and start multiprocessing
more /e +88 mybat.bat > temp.bat
START "2nd Process mybat2" temp.bat
:start
for /f "eol=; tokens=* delims==," %%i in (list1) do call :SEARCH %%i
goto :END
:: 88th line here
for /f "eol=; tokens=* delims==," %%k in (list2) do call :SEARCH %%k
goto :END
:search
setlocal enabledelayedexpansion
for %%A in (jan feb mar apr mai etc...) DO (
findstr /m "%%A" "%~f1" > NUL
if !ERRORLEVEL! == 0 (
call :theend %~1 %%A
goto :EOF
)
)
endlocal
I get this error message "FINDSTR: Cannot open X:\folder\folder number\begin.txt"
Is there a way to fix this findstr problem?
thanks
To let us see if your file dropping is OK:
Can you create a batch file with these contents
echo %*
set /p dummy=press return
drop a file, with spaces in the name, onto it and post the results?

Resources