Function - read text file and preserve line read

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Function - read text file and preserve line read

#1 Post by booga73 » 01 Jun 2014 22:09

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

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Function - read text file and preserve line read

#2 Post by Yury » 01 Jun 2014 23:38

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




.
Last edited by Yury on 02 Jun 2014 00:53, edited 1 time in total.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Function - read text file and preserve line read

#3 Post by Yury » 02 Jun 2014 00:00

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




.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Function - read text file and preserve line read

#4 Post by Yury » 02 Jun 2014 00:15

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




.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Function - read text file and preserve line read

#5 Post by dbenham » 02 Jun 2014 05:11

No batch script required.

Code: Select all

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


Dave Benham

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Function - read text file and preserve line read

#6 Post by Yury » 02 Jun 2014 06:09

dbenham wrote:No batch script required.

Code: Select all

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


The fourth way. :)

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Function - read text file and preserve line read

#7 Post by booga73 » 02 Jun 2014 08:32

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


booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Function - read text file and preserve line read

#8 Post by booga73 » 02 Jun 2014 12:00

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

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Function - read text file and preserve line read

#9 Post by Yury » 02 Jun 2014 12:24

Code: Select all

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

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Function - read text file and preserve line read

#10 Post by Yury » 02 Jun 2014 23:04

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




.

Post Reply