Page 1 of 1

Function - read text file and preserve line read

Posted: 01 Jun 2014 22:09
by booga73

Code: Select all

@ECHO OFF

for /f "tokens=*" %%a in (%tmp%\ExePath1.txt) do (
   Call :MySplitPath %%a
)

echo.
echo This is the End of code.
pause


:MySplitPath %1
 set val1=%*

for /f "tokens=2,3 delims=\" %%a in ("%val1%") do (

  if NOT "%%b"=="WinSxS" echo %val1% >> %tmp%\SplitPath.txt

)
exit /b


I have a little script that need help with. Problem, every time my MySplitPath function is called, once it reads a line which consists of:

" C:\Program Files (x86)\NVIDIA Corporation\3D Vision\nvSCPAPISvr.exe "

my script errors out. Once the function tries to tokenize / read a line that has the " C:\Program Files (x86)\ " the error message I get is:

" \NVIDIA was unexpected at this time. "

How do you continue reading the %tmp%\SplitPath.txt file and correctly read / preserve the text line that has " C:\Program Files (x86)\ " ?

I am trying to parse out all the lines that have the associated "C:\Windows\WinSxS\" folder path and echo the remaining file content into %tmp%\SplitPath.txt

There's a text file that resides in %tmp%\ExePath1.txt. The contents of the ExePath1.txt is below.

An example of %tmp%\SplitPath.txt contents is also below too for illustration purposes.

very respectfully, Booga73


Contents of ExePath1.txt:

C:\Windows\System32\smss.exe
C:\Windows\WinSxS\amd64_microsoft-windows-smss-minwin_31bf3856ad364e35_6.3.9600.16384_none_6f1f364dbcc273d3\smss.exe
C:\Windows\WinSxS\amd64_microsoft-windows-smss-minwin_31bf3856ad364e35_6.3.9600.17031_none_6f522891bc9cbe45\smss.exe
C:\Windows\System32\csrss.exe
C:\Windows\WinSxS\amd64_microsoft-windows-csrss_31bf3856ad364e35_6.3.9600.16384_none_49a243e2b80cb4c0\csrss.exe
C:\Windows\System32\wininit.exe
C:\Windows\WinSxS\amd64_microsoft-windows-wininit_31bf3856ad364e35_6.3.9600.16384_none_21b118d9d847ad16\wininit.exe
C:\Windows\System32\services.exe
C:\Windows\WinSxS\amd64_microsoft-windows-s..cecontroller-minwin_31bf3856ad364e35_6.3.9600.16384_none_2fd72579d09a45e9\services.exe
C:\Windows\System32\lsass.exe
C:\Windows\WinSxS\amd64_microsoft-windows-lsa-minwin_31bf3856ad364e35_6.3.9600.16408_none_2e8484166600f08e\lsass.exe
C:\Program Files\NVIDIA Corporation\Installer2\Display.ControlPanel.{31C66714-96D3-430F-92A3-999C97F78393}\nvvsvc.exe
C:\Windows\System32\nvvsvc.exe
C:\Program Files (x86)\NVIDIA Corporation\3D Vision\nvSCPAPISvr.exe
C:\Windows\System32\spoolsv.exe
C:\Windows\WinSxS\amd64_microsoft-windows-printing-spooler-core_31bf3856ad364e35_6.3.9600.16384_none_c70a032c957fcb8a\spoolsv.exe
C:\Program Files (x86)\Common Files\Adobe\ARM\1.0\armsvc.exe
C:\Program Files (x86)\Common Files\Java\Java Update\jusched.exe
C:\Program Files (x86)\Common Files\Adobe\ARM\1.0\AdobeARM.exe


Contents of %tmp%\SplitPath.txt:

C:\Windows\System32\smss.exe
C:\Windows\System32\csrss.exe
C:\Windows\System32\wininit.exe
C:\Windows\System32\services.exe
C:\Windows\System32\lsass.exe
C:\Program Files\NVIDIA Corporation\Installer2\Display.ControlPanel.{31C66714-96D3-430F-92A3-999C97F78393}\nvvsvc.exe
C:\Windows\System32\nvvsvc.exe
C:\Windows\System32\smss.exe
C:\Windows\System32\csrss.exe
C:\Windows\System32\wininit.exe
C:\Windows\System32\services.exe
C:\Windows\System32\lsass.exe
C:\Program Files\NVIDIA Corporation\Installer2\Display.ControlPanel.{31C66714-96D3-430F-92A3-999C97F78393}\nvvsvc.exe
C:\Windows\System32\nvvsvc.exe

Re: Function - read text file and preserve line read

Posted: 01 Jun 2014 23:38
by Yury
There are several ways to fix thеsе errors.



First way:


Code: Select all

@ECHO OFF>%tmp%\SplitPath.txt

for /f "tokens=*" %%a in (%tmp%\ExePath1.txt) do (
   Call :MySplitPath %%a
)

echo.
echo This is the End of code.
pause
rem "exit" or "goto:eof"
exit /b


:MySplitPath %1
 set val1=%*

rem One more "for"
for /f "tokens=2,3 delims=\" %%a in ("%val1%") do (

  if NOT "%%b"=="WinSxS" for /f "tokens=*" %%c in ("%val1%") do echo %%c>>%tmp%\SplitPath.txt

)
exit /b




.

Re: Function - read text file and preserve line read

Posted: 02 Jun 2014 00:00
by Yury
Second way:



Code: Select all

@ECHO OFF>%tmp%\SplitPath.txt

for /f "tokens=*" %%a in (%tmp%\ExePath1.txt) do (
   Call :MySplitPath %%a
)

echo.
echo This is the End of code.
pause
rem "exit" or "goto:eof"
exit /b


:MySplitPath %1
 set val1=%*

rem "SETLOCAL ENABLEDELAYEDEXPANSION" and "!" instead "%"
setlocal enabledelayedexpansion
for /f "tokens=2,3 delims=\" %%a in ("%val1%") do (

  if NOT "%%b"=="WinSxS" echo !val1!>>%tmp%\SplitPath.txt

)
exit /b




.

Re: Function - read text file and preserve line read

Posted: 02 Jun 2014 00:15
by Yury
Third way:



Code: Select all

@ECHO OFF>%tmp%\SplitPath.txt

for /f "tokens=*" %%a in (%tmp%\ExePath1.txt) do (
   Call :MySplitPath %%a
)

echo.
echo This is the End of code.
pause
rem "exit" or "goto:eof"
exit /b


:MySplitPath %1
 set val1=%*

rem "<nul set /p="..."& echo." instead "echo ..."
for /f "tokens=2,3 delims=\" %%a in ("%val1%") do (

  if NOT "%%b"=="WinSxS" (<nul set /p="%val1%"& echo.)>>%tmp%\SplitPath.txt

)
exit /b




.

Re: Function - read text file and preserve line read

Posted: 02 Jun 2014 05:11
by dbenham
No batch script required.

Code: Select all

find /vi "C:\Windows\WinSxS\" "%tmp%\ExePath1.txt" >"%tmp%\SplitPath.txt"


Dave Benham

Re: Function - read text file and preserve line read

Posted: 02 Jun 2014 06:09
by Yury
dbenham wrote:No batch script required.

Code: Select all

find /vi "C:\Windows\WinSxS\" "%tmp%\ExePath1.txt" >"%tmp%\SplitPath.txt"


The fourth way. :)

Re: Function - read text file and preserve line read

Posted: 02 Jun 2014 08:32
by booga73
thank you! I appreciate the input and time put into the help provided.

I do like Yury's option for delayed expansion; that options allows me to maintain my code I wrote. The other script options show a very small modification too. Dave also simplifies to 1 line code! Awesome!


In summary, in using delayed expansion, using delayed expansion helps to maintain a string variable during execution of a script. It's just a matter of when to recognize the need for delayed expansion.

After the posting, I've looked into reading additional info of what "SETLOCAL ENABLEDELAYEDEXPANSION" is. I've read that if you use delayed expansion then the delay expansion doesn't " close " or end untill the script offically completes. So my question then, to end delayed expansion, I should use, endlocal, but where would I place endlocal at?

In either 1 of the 2 locations listed below? I would only use endlocal to stop delay expansion if my script would continue even further.

I simply would be interested in containing the effects of delayed expansion to a specific place in the script when the script is run and not all the way through the script.

Also, why piping @ECHO OFF>%tmp%\SplitPath.txt ?

thank you, Booga73

Code: Select all


@ECHO OFF>%tmp%\SplitPath.txt

for /f "tokens=*" %%a in (%tmp%\ExePath1.txt) do (
   Call :MySplitPath %%a
)

:: 2nd Position
endlocal

echo.
echo This is the End of code.
pause
rem "exit" or "goto:eof"
exit /b


:MySplitPath %1
 set val1=%*

rem "SETLOCAL ENABLEDELAYEDEXPANSION" and "!" instead "%"
setlocal enabledelayedexpansion
for /f "tokens=2,3 delims=\" %%a in ("%val1%") do (

  if NOT "%%b"=="WinSxS" echo !val1!>>%tmp%\SplitPath.txt

:: 1st Position
endlocal
)
exit /b


Re: Function - read text file and preserve line read

Posted: 02 Jun 2014 12:00
by booga73
need help again . .. . please

I've tried the options below as illustrated, but problem parsing out the strings that contain:

C:\Windows\winsxs\

is not working. All the lines whether consisting C:\Windows\winsxs\ are being included, echo'ed, into my final text file

I tested on a Windows 7 x32 bit O/S (Not working) and haven't returned to verify on my Win8 x64 box, but the parsing isn't working.

Last night on my Win8 x64 box the parsing was working till it hit the initial snag of " C:\Program Files (x86)\NVIDIA Corporation\3D Vision\nvSCPAPISvr.exe ".

my final output file last night did show that C:\Windows\winsxs\ was removed in my destination text file, %tmp%\SplitFile.txt.


Code: Select all


@ECHO OFF>%tmp%\SplitPath.txt

for /f "tokens=*" %%a in (%tmp%\ExePath1.txt) do (
   Call :MySplitPath %%a
)

echo.
echo This is the End of code.
pause
rem "exit" or "goto:eof"
exit /b


:MySplitPath %1
 set val1=%*

setlocal enabledelayedexpansion
for /f "tokens=2,3 delims=\" %%a in ("%val1%") do (

cls
echo.
echo token1 is: %%a
echo token2 is: %%b
echo token3 is: %%c

  if NOT "%%b"=="WinSxS" echo !val1!>>%tmp%\SplitPath.txt
)
endlocal
exit /b





%tmp%\ExePath1.txt file:

C:\Windows\System32\smss.exe
C:\Windows\winsxs\x86_microsoft-windows-smss_31bf3856ad364e35_6.1.7600.16385_none_ac10fe207a85352b\smss.exe
C:\Windows\winsxs\x86_microsoft-windows-smss_31bf3856ad364e35_6.1.7601.18113_none_ae40f33e7774c473\smss.exe
C:\Windows\winsxs\x86_microsoft-windows-smss_31bf3856ad364e35_6.1.7601.22379_none_ae8fb42390bda114\smss.exe
C:\Windows\winsxs\x86_microsoft-windows-smss_31bf3856ad364e35_6.1.7601.22436_none_aeb7f4db909fe272\smss.exe
C:\Windows\winsxs\x86_microsoft-windows-smss_31bf3856ad364e35_6.1.7601.22653_none_ae9f57f190b2c89d\smss.exe
C:\Windows\System32\csrss.exe
C:\Windows\winsxs\x86_microsoft-windows-csrss_31bf3856ad364e35_6.1.7600.16385_none_58ba39fb456943bd\csrss.exe
C:\Windows\System32\wininit.exe
C:\Windows\winsxs\x86_microsoft-windows-wininit_31bf3856ad364e35_6.1.7600.16385_none_30c90ef265a43c13\wininit.exe
C:\Windows\System32\csrss.exe
C:\Windows\winsxs\x86_microsoft-windows-csrss_31bf3856ad364e35_6.1.7600.16385_none_58ba39fb456943bd\csrss.exe
C:\Windows\System32\services.exe
C:\Windows\winsxs\x86_microsoft-windows-s..s-servicecontroller_31bf3856ad364e35_6.1.7600.16385_none_cf36168b2e9c967b\services.exe
C:\Windows\System32\lsass.exe
C:\Windows\winsxs\x86_microsoft-windows-lsa_31bf3856ad364e35_6.1.7600.16385_none_a620e0e5be1ecda7\lsass.exe
C:\Windows\winsxs\x86_microsoft-windows-lsa_31bf3856ad364e35_6.1.7600.16484_none_a61fe281be1fb177\lsass.exe
C:\Windows\winsxs\x86_microsoft-windows-lsa_31bf3856ad364e35_6.1.7600.16915_none_a66c9bbdbde5f8fa\lsass.exe
C:\Windows\winsxs\x86_microsoft-windows-lsa_31bf3856ad364e35_6.1.7600.17035_none_a656d407bdf6641e\lsass.exe
C:\Windows\winsxs\x86_microsoft-windows-lsa_31bf3856ad364e35_6.1.7600.20594_none_a69eaf60d7456d32\lsass.exe
C:\Windows\winsxs\x86_microsoft-windows-lsa_31bf3856ad364e35_6.1.7600.21092_none_a69c8e86d7476262\lsass.exe
C:\Windows\winsxs\x86_microsoft-windows-lsa_31bf3856ad364e35_6.1.7600.21225_none_a6eb42a4d70be51e\lsass.exe
C:\Windows\winsxs\x86_microsoft-windows-lsa_31bf3856ad364e35_6.1.7601.17514_none_a851f4adbb0d5141\lsass.exe
C:\Windows\winsxs\x86_microsoft-windows-lsa_31bf3856ad364e35_6.1.7601.17725_none_a84828d7bb1480d7\lsass.exe
C:\Windows\winsxs\x86_microsoft-windows-lsa_31bf3856ad364e35_6.1.7601.17856_none_a828bb43bb2beb28\lsass.exe
C:\Windows\winsxs\x86_microsoft-windows-lsa_31bf3856ad364e35_6.1.7601.17940_none_a82d8b59bb293454\lsass.exe
C:\Windows\winsxs\x86_microsoft-windows-lsa_31bf3856ad364e35_6.1.7601.18270_none_a80cf783bb41b5b7\lsass.exe
C:\Windows\winsxs\x86_microsoft-windows-lsa_31bf3856ad364e35_6.1.7601.18443_none_a8306bf1bb26a837\lsass.exe
C:\Windows\winsxs\x86_microsoft-windows-lsa_31bf3856ad364e35_6.1.7601.21861_none_a8a284cad4562b09\lsass.exe
C:\Windows\winsxs\x86_microsoft-windows-lsa_31bf3856ad364e35_6.1.7601.22010_none_a8d76e24d42eb666\lsass.exe
C:\Windows\winsxs\x86_microsoft-windows-lsa_31bf3856ad364e35_6.1.7601.22099_none_a889f15ed46779fd\lsass.exe
C:\Windows\winsxs\x86_microsoft-windows-lsa_31bf3856ad364e35_6.1.7601.22465_none_a8a66792d452b56a\lsass.exe
C:\Windows\winsxs\x86_microsoft-windows-lsa_31bf3856ad364e35_6.1.7601.22653_none_a8af3ab6d44c6119\lsass.exe
C:\Windows\System32\lsm.exe
C:\Windows\winsxs\x86_microsoft-windows-t..localsessionmanager_31bf3856ad364e35_6.1.7600.16385_none_a51b22e46bde44fe\lsm.exe
C:\Windows\winsxs\x86_microsoft-windows-t..localsessionmanager_31bf3856ad364e35_6.1.7601.17514_none_a74c36ac68ccc898\lsm.exe
C:\Windows\System32\winlogon.exe

Re: Function - read text file and preserve line read

Posted: 02 Jun 2014 12:24
by Yury

Code: Select all

  if /i NOT "%%b"=="WinSxS" echo !val1!>>%tmp%\SplitPath.txt

Re: Function - read text file and preserve line read

Posted: 02 Jun 2014 23:04
by Yury
Interestingly, even in the case of the presence of parentheses in a line this code works (the fourth way):



Code: Select all

@ECHO OFF>%tmp%\SplitPath.txt

for /f "tokens=*" %%a in (%tmp%\ExePath1.txt) do (
   Call :MySplitPath %%a
)

echo.
echo This is the End of code.
pause
rem "exit" or "goto:eof"
exit /b


:MySplitPath %1
 set val1=%*

rem Using of "call <command> [...]%%variable%%[...]"
for /f "tokens=2,3 delims=\" %%a in ("%val1%") do (

  if /i NOT "%%b"=="WinSxS" call echo %%val1%%>>%tmp%\SplitPath.txt

)
exit /b




.