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.
Related
05-08 23:38:08.484 11404 11764 I Ads : GMA Debug CONTENT {"timestamp":1588961288491,"event":"onNetworkRequest","components":["network_request_36385a69-b99e-4da6-b6a0-7bf2a106a285"],"params":{"firstline":{"uri":"https://pubads.g.doubleclick.net/gampad/ads?submodel=SM-N915G&android_app_volume=1&disable_ml=false&format=interstitial_mb&omid_v=a.1.3.1-google_20200121&is_nonagon=true&android_app_muted=false&am=0&dv=21400&gl=GB&hl=en&js=afma-sdk-a-v201004999.14300000.1&lv=0&ms=CoACClZSVfMXCRQ-WNImS5Q9B8Kr7uHr8m5cVL4sU3c951ZafOWUx64ymtfyXUpdXg0A95gRozZwS4W6-w_RmoEeWxq56UkgzPcVmiNEL1Aa8-v6Cik5s15gGkF5AHJpfjwSOynwJ7ZEgj78CEI_AvDrK8pfF4OhctAnxYwxMmeZDX4zqP8dLptQ8EIZtjfmEtXjFS3lzvdrHUpgsG8E_EscYo0xlLZu7zzQe8GpDRUhnkQr4xGv4ou4ZqbLmstqeZdPLcZX1xROBq_rLfY_3cY7z8Tf8seqVNsTI49EG2HZuMjCLRFOmtinp3mFO8du9pxSg4cNV2JxjxFox1TGQ2QLqQqAAjorBHB9GJsXM85JwwKB2h9tQAvgPoaJZRwBhJQHB_qs2iswjcSsJldAYHKe105wq7VihcvZVM7oLDfQhQqWuQFYb27LCf9UH43etMBZvMq7aAOpKj5ilbFHdg13wiFIdsNlQ1Kv6saMKKSi4WPoScw1bW9WEqPrSuLZDwo6NN4iFo1cJ_2dD5Xu4R52ytmY70NkOeTVa07KGAVwR0GARwuRX6VTKtDAsWap6OybQAF9ZtCwbs5h8Man9xrlaxJRuuzzhH1KEnp3Nld-8JfXglPI4-8yOXRWjnB3fQadPz2BLEKiq-pUtZVrrRqIeQu5jDMjX69Oks3XSHwECN6D32ASENSnE_R_crSMn7XDhwnuK-4&mv=81983300.com.android.vending&rm=2&sp=false&coh=true&riv=11&u_sd=2.4&request_id=4145396e-4ada-4bd7-854a-11556ade0f70&render_in_browser=false&target_api=29&carrier=40407&is_sidewinder=false&seq_num=1&eid=318482080%2C318490406%2C318484496%2C318486317%2C318484676%2C318482960%2C318482459%2C318483611%2C318486978%2C318487060%2C318489974%2C318475417%2C318483742&tag_for_child_directed_treatment=0&_c_csdk_npa_o=false&_c_req_npa=false&npa=0&guci=0.0.0.0.0.0.0.0&rbv=1&u_w=640&u_h=383&msid=com.ea.gp.nbamobile&_package_name=com.ea.gp.nbamobile&an=44310.android.com.ea.gp.nbamobile&net=wi&u_audio=3&u_so=l&preqs_in_session=2&support_transparent_background=true&preqs=0&time_in_session=310380&output=html®ion=mobile_app&u_tz=330&url=44310.android.com.ea.gp.nbamobile.adsenseformobileapps.com&gdfp_req=1&markup=html&m_ast=afmajs&impl=ifr&iu=%2F220%2FNBALive%2FAndroid%2FRewarded_Video%2FDashboard&sz=480x320&oct_ist=1&correlator=1486806085148500&gsb=wi&lite=true&vfmt=18&vst=0&sdkv=o.201004999.14300000.1&sdki=3c4d&caps=inlineVideo_interactiveVideo_mraid1_mraid2_mraid3_sdkVideo_exo3_th_autoplay_mediation_scroll_av_transparentBackground_sdkAdmobApiForAds_di_sfv_dinm_dim_nav_navc_dinmo_ipdof_gls_gcache_xSeconds&bisch=true&blev=0.86&swdr=false&cans=5&canm=false&_mv=23.android&heap_free=9792968&heap_max=268435456&heap_total=38043632&wv_count=0&rdps=15650&blockAutoClicks=true&cust_params=excl_cat%26optout%3Dno%26coppa%3Dno&rewarded_version=1&includeDoritos=true&rdidl=36&idtypel=4&is_latl=-1&blob=ABPQqLGrcFoEjqzU1D-terFskdukGTLEuTUawcm0pBN0aB1d8YNSg6mLc5H5hWfITabn9X_sTUR-xainyvLxhx-QMpVlfZXee0FH8Z8Fjs6uM7kH5i5_Ej4OWZHYmzzc27g9uusoPfNAP_WRsabkoO4MOzGcL_wF_NLF8S72uFyd1TxihwAptei3UGc8midRCER_2m49ZCG92iIMZnSTLY3GHkm3yeGBDjP16FauKMJMYYX2n1MfK52OyblTrPquFX6adhiTApYLs-oIRLLuzbInS3P5cscrCxfBIHIkxOrqtg_fXXJTkgjUzk8Nhp3Tnm-4lN0lNRyzNnic-TU&et=125&tcar=194&jsv=sdk_20190107_RC02-canary-production-sdk_20200428_RC00&urll=2888#caps=inlineVideo_interactiveVideo_mraid1_mraid2_mraid3_sdkVideo_exo3_th_autoplay_mediation_scroll_av_transparentBackground_sdkAdmobApiForAds_di_sfv_dinm_dim_nav_navc_dinmo_ipdof_gls_gcache_xSeconds&eid=318482080%252C318490406%252C318484496%252C318486317%252C318484676%252C318482960%252C318482459%252C318483611%252C318486978%252C318487060%252C318489974%252C318475417%252C318483742&format=interstitial_mb&heap_free=9792968&heap_max=268435456&heap_total=38043632&js=afma-sdk-a-v201004999.14300000.1&msid=com.ea.gp.nbamobile&preqs=0&seq_num=1&target_api=29","verb":"GET"},"headers":[{"name":"User-Agent","value":"Mozilla/5.0 (Linux; Android 6.0.1; SM-N915G Build/MMB29M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/81.0.4044.138 Mobile Safari/537.36 (Mobile; afma-sdk-a-v201004999.14300000.1)"},{"name":"Cookie","value":"IDE=AHWqTUlALo2Mq3Qp52ZNNvAhzeqdzEJ0K5nNOV3OKMFW3vapCmqRNqnN6tIrx26A"}]}}
From this I want the output to be printed as:
tag_for_child_directed_treatment=0, if I enter tag_for_child_directed_treatment in the input or else its fine if it prints complete string tag_for_child_directed_treatment=0
I have tried with the code:
#echo off
set /P search_string= Enter the string you would like to search for:
find "%search_string%" C:\Users\Bhargav\logcat1.txt
pause
It gives the output as a complete line, can anyone help me?
Get the string.
Cut it from the beginning to (including) the search-string.
extract the number.
#echo off
setlocal enabledelayedexpansion
set "search_string=tag_for_child_directed_treatment"
for /f "delims=" %%a in (test.txt) do set "string=%%a"
set "string=!string:*%search_string%=!"
for /f "delims==&" %%a in ("!string!") do set "result=%%a"
echo %search_string%=%result%
EDIT (slightly adapted to take care of multiple entries in the text file):
#echo off
setlocal enabledelayedexpansion
set "search_string=tag_for_child_directed_treatment"
for /f "delims=" %%t in (test.txt) do (
set "string=%%t"
set "string=!string:*%search_string%=!"
for /f "delims==&" %%a in ("!string!") do echo result=%%a
)
Batch is horrible to process such a string. A more reliable solution is possible with dbenham's utility jrepl.bat, a hybrid JScript/batch script with full REGEX support, which makes this task quite easy:
#echo off
set "search_string=tag_for_child_directed_treatment"
for /f "delims=" %%a in ('type test.txt ^| jrepl ".*tag_for_child_directed_treatment=([0-9]*).*" "$1" /XSEQ') do set "result=%%a"
echo %search_string%=%result%
EDIT (slightly adapted to take care of multiple entries in the text file):
#echo off
set "search_string=tag_for_child_directed_treatment"
for /f "delims=" %%a in ('type test.txt ^| jrepl ".*tag_for_child_directed_treatment=([0-9]*).*" "$1" /XSEQ') do echo result=%%a
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)
*/
Extensive searches found no solution in batch to parse netstat -bano (same switch as -nab, -bna, -anb, -nba, nabo, etc.) so all info from a given network connection is on the same line.
Netstat's -ban switches are used to:
-b = display executable
-a = display all connections and listening ports
-n = display addresses and ports in numerical form.
-o = display owning process ID for each connection (i.e., PID)
But netstat adds the file name to the next line, making processing the output very difficult. Extensive searches didn't find any answers in batch.
I created a way to parse it by looking for a ] (right bracket) as the last character on a line. If it is a ], then basically output the "combined" line, which now contains the executable.
My code is posted as an answer, but does an ugly job since it has to use files to handle the parsing instead of variables.
The previous version of this script was missing a backslash. Additionally the script would delete the output file. Creating the file as %computername%--NETSTAT.txt instead of NETSTAT--%computername%.txt fixes the problem.
#ECHO OFF
SetLocal
REM Method of finding last character-of-a-string-from-a-variable
REM http://stackoverflow.com/a/15662607/1569434
REM Get the script's path so all needed files can sit in the same folder
SET SCRIPTPATH=%~p0
CD %SCRIPTPATH%
REM Read and pass each line in file one at a time to sub 'FindEXE'
SET CONCATLINE=
SET HEADERROW=
SET /A LINECOUNT = 0
#echo LINECOUNT = %LINECOUNT%
del %SCRIPTPATH%\netstat*.txt /q 2>nul
netstat -bano>%SCRIPTPATH%\netstat0.txt
REM Copy all lines except those with "TIME_WAIT" into text file
FINDSTR /V /I /C:"TIME_WAIT" %SCRIPTPATH%\netstat0.txt>%SCRIPTPATH%\netstat1.txt
REM Delete first two lines, which are a header and a blank line
for /f "skip=2 delims=*" %%a in (%SCRIPTPATH%\netstat1.txt) do (echo %%a>>%SCRIPTPATH%\netstat2.txt)
REM Search for and process file based on matching text
REM This sub begins putting each netstat connection on one line
for /f "delims=*" %%A in (%SCRIPTPATH%\netstat2.txt) do call :FindTXT1 %%A
REM netstat3 will have all data from given connection on one line
SET /A LINECOUNT = 0
for /f "delims=*" %%A in (%SCRIPTPATH%\netstat3.txt) do call :FindTXT2 %%A
REM Keep only header and unique (i.e., those with "[::]") 'listening' connections
FINDSTR /I /C:"LISTENING" /C:"Local Address" %SCRIPTPATH%\netstat4.txt>%SCRIPTPATH%\netstat5.txt
FINDSTR /I /C:"[::]:" /C:"Local Address" %SCRIPTPATH%\netstat5.txt>%SCRIPTPATH%\netstat6.txt
MOVE /Y %SCRIPTPATH%\netstat6.txt %SCRIPTPATH%\%computername%--NETSTAT.txt
del %SCRIPTPATH%\netstat*.txt /q 2>nul
#echo off
echo done.
EndLocal
goto :EOF
:FindTXT1
REM We've got a line sent to us. Set variable to entire line using * (instead of %1)
SET CURRENTLINE=%*
SET /A LINECOUNT = %LINECOUNT% + 1
REM Add line feed after header row and return to main script
IF "%LINECOUNT%" == "1" (
SET HEADERROW=%CURRENTLINE%
#ECHO %CURRENTLINE%> %SCRIPTPATH%\netstat3.txt
goto :eof
)
REM Append a comma and CURRENTLINE to CONCATLINE. NOTE: Script expecting comma; don't use semi-colon
SET CONCATLINE=%CONCATLINE%,%CURRENTLINE%
REM When echo line, remove first char (comma, inserted above) using:
REM http://ss64.com/nt/syntax-substring.html
REM If last char is "]" then print, otherwise append
IF "%CURRENTLINE:~-1%"=="]" (
REM #echo right bracket=FOUND
#echo %CONCATLINE:~1%>>%SCRIPTPATH%\netstat3.txt
SET CONCATLINE=
) else (
REM #echo right bracket=NOT found
)
REM If line = "Can not obtain ownership information" then print, otherwise append
IF "%CURRENTLINE%"=="Can not obtain ownership information" (
REM #echo No Ownership=TRUE
#echo %CONCATLINE:~1%>>%SCRIPTPATH%\netstat3.txt
SET CONCATLINE=
)
goto :eof
:FindTXT2
REM We've got a line sent to us. Set variable to entire line using * (instead of %1)
SET CURRENTLINE=%*
SET /A LINECOUNT = %LINECOUNT% + 1
REM Add line feed after header row and return to main script
IF "%LINECOUNT%" == "1" (
SET HEADERROW=%CURRENTLINE%
#ECHO %CURRENTLINE%> %SCRIPTPATH%\netstat4.txt
goto :eof
)
REM If last char is "]" then search, otherwise append.
REM Without "DelayedExp...", variable sets to value from previous FOR loop
IF "%CURRENTLINE:~-1%"=="]" (
SetLocal ENABLEDELAYEDEXPANSION
REM IP6 EXEs result in 3 sets of [], so find and set var to last one, which is where EXE lives
FOR /f "tokens=1,2,3,4,5,6 delims=[]" %%a in ("%CURRENTLINE%") do (
SET BINNAME1=%%b
SET BINNAME2=%%f
IF "!BINNAME1!" == "::" (
REM #ECHO BINNAME1=!BINNAME1!>>%SCRIPTPATH%\netstat4.txt
SET BINNAME=!BINNAME2!
REM #echo %CURRENTLINE%;BINNAME=!BINNAME2!>>%SCRIPTPATH%\netstat4.txt
) else (
SET BINNAME=!BINNAME1!
REM #echo %CURRENTLINE%;BINNAME=!BINNAME1!>>%SCRIPTPATH%\netstat4.txt
)
#echo %CURRENTLINE%;BINNAME=!BINNAME!>>%SCRIPTPATH%\netstat4.txt
)
) else (
#echo %CURRENTLINE%>>%SCRIPTPATH%\netstat4.txt
SetLocal DISABLEDELAYEDEXPANSION
)
goto :eof
Created solution in batch to concatenate all output for a given connection so it's on one line. Save script (below) as a batch file and when run it will create a file called "NETSTAT--%computername%.txt" in the same folder as the script, where %computername% will be replaced with the hostname of computer it's run on.
#ECHO OFF
SetLocal
REM Method of finding last character-of-a-string-from-a-variable
REM http://stackoverflow.com/a/15662607/1569434
REM Get the script's path so all needed files can sit in the same folder
SET SCRIPTPATH=%~p0
CD %SCRIPTPATH%
REM Read and pass each line in file one at a time to sub 'FindEXE'
SET CONCATLINE=
SET HEADERROW=
SET /A LINECOUNT = 0
#echo LINECOUNT = %LINECOUNT%
del %SCRIPTPATH%\netstat*.txt /q 2>nul
netstat -bano>%SCRIPTPATH%\netstat0.txt
REM Copy all lines except those with "TIME_WAIT" into text file
FINDSTR /V /I /C:"TIME_WAIT" %SCRIPTPATH%\netstat0.txt>%SCRIPTPATH%\netstat1.txt
REM Delete first two lines, which are a header and a blank line
for /f "skip=2 delims=*" %%a in (%SCRIPTPATH%\netstat1.txt) do (echo %%a>>%SCRIPTPATH%\netstat2.txt)
REM Search for and process file based on matching text
REM This sub begins putting each netstat connection on one line
for /f "delims=*" %%A in (%SCRIPTPATH%\netstat2.txt) do call :FindTXT1 %%A
REM netstat3 will have all data from given connection on one line
SET /A LINECOUNT = 0
for /f "delims=*" %%A in (%SCRIPTPATH%\netstat3.txt) do call :FindTXT2 %%A
REM Keep only header and unique (i.e., those with "[::]") 'listening' connections
FINDSTR /I /C:"LISTENING" /C:"Local Address" %SCRIPTPATH%\netstat4.TXT>%SCRIPTPATH%\netstat5.TXT
FINDSTR /I /C:"[::]:" /C:"Local Address" %SCRIPTPATH%\netstat5.TXT>%SCRIPTPATH%\netstat6.TXT
MOVE /Y %SCRIPTPATH%\netstat6.txt %SCRIPTPATH%\NETSTAT--%computername%.txt
del %SCRIPTPATH%netstat*.txt /q 2>nul
#echo off
echo done.
EndLocal
goto :EOF
:FindTXT1
REM We've got a line sent to us. Set variable to entire line using * (instead of %1)
SET CURRENTLINE=%*
SET /A LINECOUNT = %LINECOUNT% + 1
REM Add line feed after header row and return to main script
IF "%LINECOUNT%" == "1" (
SET HEADERROW=%CURRENTLINE%
#ECHO %CURRENTLINE%> %SCRIPTPATH%\netstat3.txt
goto :eof
)
REM Append a comma and CURRENTLINE to CONCATLINE. NOTE: Script expecting comma; don't use semi-colon
SET CONCATLINE=%CONCATLINE%,%CURRENTLINE%
REM When echo line, remove first char (comma, inserted above) using:
REM http://ss64.com/nt/syntax-substring.html
REM If last char is "]" then print, otherwise append
IF "%CURRENTLINE:~-1%"=="]" (
REM #echo right bracket=FOUND
#echo %CONCATLINE:~1%>>%SCRIPTPATH%\netstat3.txt
SET CONCATLINE=
) else (
REM #echo right bracket=NOT found
)
REM If line = "Can not obtain ownership information" then print, otherwise append
IF "%CURRENTLINE%"=="Can not obtain ownership information" (
REM #echo No Ownership=TRUE
#echo %CONCATLINE:~1%>>%SCRIPTPATH%\netstat3.txt
SET CONCATLINE=
)
goto :eof
:FindTXT2
REM We've got a line sent to us. Set variable to entire line using * (instead of %1)
SET CURRENTLINE=%*
SET /A LINECOUNT = %LINECOUNT% + 1
REM Add line feed after header row and return to main script
IF "%LINECOUNT%" == "1" (
SET HEADERROW=%CURRENTLINE%
#ECHO %CURRENTLINE%> %SCRIPTPATH%\netstat4.txt
goto :eof
)
REM If last char is "]" then search, otherwise append.
REM Without "DelayedExp...", variable sets to value from previous FOR loop
IF "%CURRENTLINE:~-1%"=="]" (
SetLocal ENABLEDELAYEDEXPANSION
REM IP6 EXEs result in 3 sets of [], so find and set var to last one, which is where EXE lives
FOR /f "tokens=1,2,3,4,5,6 delims=[]" %%a in ("%CURRENTLINE%") do (
SET BINNAME1=%%b
SET BINNAME2=%%f
IF "!BINNAME1!" == "::" (
REM #ECHO BINNAME1=!BINNAME1!>>%SCRIPTPATH%\netstat4.txt
SET BINNAME=!BINNAME2!
REM #echo %CURRENTLINE%;BINNAME=!BINNAME2!>>%SCRIPTPATH%\netstat4.txt
) else (
SET BINNAME=!BINNAME1!
REM #echo %CURRENTLINE%;BINNAME=!BINNAME1!>>%SCRIPTPATH%\netstat4.txt
)
#echo %CURRENTLINE%;BINNAME=!BINNAME!>>%SCRIPTPATH%\netstat4.txt
)
) else (
#echo %CURRENTLINE%>>%SCRIPTPATH%\netstat4.txt
SetLocal DISABLEDELAYEDEXPANSION
)
goto :eof
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
Im attempting to get a block of text from a file I have already created and store that information into a new file. The text file id like to parse is set up like this:
HostName: xxxxxx
(8 or so lines of information)
HostName: xxxxxx
The largest problem I am having is that between the blocks of text I am specifically looking for the word "admin". I am able to find and return the line with the word admin in it, now I need to write a loop to grab the information above and below if the word admin is found within that block of text (I was going to use an empty line as the delimiter as thats consistent throughout my file).
Ive written a few for loops but none of them have given me what id like. Any help would be appreciated.
Thanks
The program below do what you want:
#echo off
setlocal EnableDelayedExpansion
rem Create the info removing empty lines
(for /F "delims=" %%a in ('schtasks /query /fo list /v') do echo %%a) > schtasks.txt
rem Add an additional "HostName:" line at end as delimiter
echo HostName: >> schtasks.txt
rem Create a vector of number of lines containing "HostName:"
set i=0
for /F "delims=:" %%a in ('findstr /N "HostName:" schtasks.txt') do (
set /A i+=1
set header[!i!]=%%a
)
rem Seek for the LINES containing "admin"
for /F "delims=:" %%a in ('findstr /N /I "administrator" schtasks.txt') do (
rem Seek for the NEXT section that contains "admin" line
for /L %%i in (1,1,%i%) do if %%a gtr !header[%%i]! set thisSection=%%i
rem Locate that section
set /A start=header[!thisSection!], nextSection=thisSection+1
set /A end=header[!nextSection!]-1
rem ... and show it
set line=0
for /F "delims=" %%a in (schtasks.txt) do (
set /A line+=1
if !line! geq !start! if !line! leq !end! echo %%a
)
echo ----------------------------------------------
)
del schtasks.txt
PS - Of course that MS-DOS/Windows Batch is a programming language!