subroutine

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

subroutine

#1 Post by booga73 » 08 Mar 2013 13:03

I have this code that reads from text file tmp1.txt, which I believe the text is read one line at a time. The goal is to read from the text then take that initial line from text pass that line of text to a subroutine. The subroutine then should seperate the text that was imported to the following format:

path fileName fileInformation

I have ran the 2nd for loop itself without the need for the 1st for loop and the subroutine and my 2nd loop displays all the text from tmp1.txt in the dos windows without issue. So, is the 2nd for loop passing each text line to the subroutine correctly? I can't get the subroutine to read each text line to seprate out the path, fileName, and file information.

So, basically, subroutine isn't recognizing the 2nd for loop's passing the text line into it. I can't see where the issue is at. Please assist.

Code: Select all

@ECHO OFF

:: 1st for loop
for /f %%N in ('find /v /c "" ^<"c:\temp1\tmp1.txt"') do set "cnt3=%%N"

:: 2nd for loop
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ c:\temp1\tmp1.txt"`) do (
    set "var=%%a"
    SETLOCAL EnableDelayedExpansion
    set "var=!var:*:=!"
    CALL :01Cmd2Line !var!
    :: echo(!var!
    ENDLOCAL
)

:: subroutine
:01Cmd2Line
set var1=%1
for %%I in (%var1%) do set "Path=%%~dpI"& set "File=%%~nxI"& set "FileInfo=%%~atzI"

echo %Path%     %File%     %FileInfo% KB >> c:\temp1\FileOutput.txt


exit /b


pause

exit


text file tmp1.txt contains the following info:

    C:\Program Files\IDT\WDM\sttray.exe
    C:\Program Files\Common Files\Java\Java Update\jusched.exe
    C:\Program Files\AirTight\SpectraGuard SAFE\WSAUI.exe
    C:\Program Files\McAfee\VirusScan Enterprise\SHSTAT.EXE
    C:\Program Files\NVIDIA Corporation\nView\nwiz.exe
    C:\Program Files\Renesas Electronics\USB 3.0 Host Controller Driver\Application\nusb3mon.exe
    C:\Program Files\McAfee\Common Framework\udaterui.exe
    C:\Program Files\McAfee\Policy Auditor Agent\PASysTray.exe
    C:\Program Files\McAfee\Host Intrusion Prevention\FireTray.exe
    C:\Program Files\Intel\Intel(R) Rapid Storage Technology\IAStorIcon.exe
    C:\Program Files\STMicroelectronics\AccelerometerP11\FF_Protection.exe
    C:\Program Files\Symantec\Symantec Endpoint Encryption Clients\Client Console\EAFRCliStart.exe
    C:\Program Files\DellTPad\Apoint.exe
    C:\Program Files\Common Files\Adobe\ARM\1.0\AdobeARM.exe
    C:\Program Files\Adobe\Acrobat 10.0\Acrobat\Acrobat_sl.exe
    C:\Program Files\Adobe\Acrobat 10.0\Acrobat\Acrotray.exe
    C:\Program Files\ActivIdentity\ActivClient\acevents.exe
    C:\Program Files\ActivIdentity\ActivClient\accrdsub.exe

subroutine output:

    C:\ Program KB
    C:\ Program KB
    C:\ Program KB
    C:\ Program KB
    C:\ Program KB
    C:\ Program KB
    C:\ Program KB
    C:\ Program KB
    C:\ Program KB
    C:\ Program KB
    C:\ Program KB
    C:\ Program KB
    C:\ Program KB
    C:\ Program KB
    C:\ Program KB
    C:\ Program KB
    C:\ Program KB
    C:\ Program KB
    C:\Program Files\Gradkell Systems, Inc\DBsign Data Security Suite\Common\Lib\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\ActivIdentity\ActivClient\;C:\Program Files\Common Files\Roxio Shared\DLLShared\;C:\Windows\System32\Windows System Resource Manager\bin;;C:\Windows\idmu\common;C:\Windows\System32\WindowsPowerShell\v1.0\;c:\Program Files\UE;c:\Program Files\UE\bin KB


mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: subroutine

#2 Post by mfm4aa » 08 Mar 2013 13:25

You need delimiters:

Code: Select all

@echo off &setlocal

for /f "delims=" %%i in (tmp1.txt) do call :subroutine "%%~i"
goto :eof

:subroutine
for %%i in ("%~1") do set "fpath=%%~dpi"&set "fname=%%~nxi"&set "finfo=%%~atzi"
echo %fpath% %fname% %finfo% KB
goto :eof

endlocal


Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: subroutine

#3 Post by Squashman » 08 Mar 2013 13:26

Spaces in your path..!!!!!!!
%1 is assigned everything up to the first space in your path name.
Regardless of that i am totally baffled at why you are trying to accomplish this task this way. You have a lot of extra code that does not really do anything. I also avoid using environmental variables as variable names.

You could accomplish parsing out the path and filename in 1 loop. you dont need the call or the loop in the subroutine.

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: subroutine

#4 Post by mfm4aa » 08 Mar 2013 13:38

Yes, better without "call":

Code: Select all

@echo off &setlocal
for /f "delims=" %%i in (tmp1.txt) do echo %%~dpi %%~nxi %%~atzi KB
endlocal



.. or on the command line:

Code: Select all

for /f "delims=" %i in (tmp1.txt) do @echo %~dpi %~nxi %~atzi KB


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

Re: subroutine

#5 Post by booga73 » 08 Mar 2013 16:04

mfm4aa,

that's pretty amazing results you provided, thank you. . .

got a question about subroutines. . . .

Code: Select all

@echo off &setlocal

for /f "delims=" %%i in (tmp1.txt) do call :subroutine "%%~i"
goto :eof

:subroutine
for %%i in ("%~1") do set "fpath=%%~dpi"&set "fname=%%~nxi"&set "finfo=%%~atzi"
echo %fpath% %fname% %finfo% KB
goto :eof

endlocal


I am not familiar with goto :eof. If you subsitute goto :eof command with each an " exit /b", does the command exit /b till the the command interpreter to return back to the main body of the For Loop then once the for loop completes, the script closes out or proceeds to next command sequence?

to illustrate, I'm thinking of this pseudo code:

for %%i in (123tmp.txt) do (
call :1st_SubRoutine
call :2nd_SubRoutine
)

goto 01NextCmd

:1st_SubRoutine
pseudo code blaw blaw
exit /b

:2nd_SubRoutine
pseudo code blaw blaw
exit /b

:01NextCmd
echo goodNite!
exit

but if modified your code as the following:

Code: Select all


@echo off &setlocal

for /f "delims=" %%i in (tmp1.txt) do call :subroutine "%%~i"

goto 01NextCmd

:subroutine
for %%i in ("%~1") do set "fpath=%%~dpi"&set "fname=%%~nxi"&set "finfo=%%~atzi"
echo %fpath% %fname% %finfo% KB
exit /b

endlocal

:01NextCmd
echo goodNite!
exit



is that valid so that the code would proceed to further process additional commands?

v/r Booga73
Thank you for the illustrations .. . . . :D

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: subroutine

#6 Post by mfm4aa » 08 Mar 2013 16:32

":eof" is a built in label. It means "end of file" and "goto :eof" works similar to "exit /b". The subroutine gives control back to the main programme.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: subroutine

#7 Post by Squashman » 08 Mar 2013 16:47

booga, why are you still doing your code that way. it is extremely inefficient.

Post Reply