The mailing list Batchworld has a wealth of knowledge, and it's where I learnt most of what I know, the rest is all about being creative.
This guy passed away last year, and feel his loss, even though I never met him.. have a look through his posts, ignore Google, check out
foxidrive
AdamByrd wrote: ↑Sat Feb 17, 2018 9:50 am
The problem here is the 'real time' requirement. Using a for loop in the first batch script waits for the command to finish before processing the output.
TL;DR
Adam, use my code, mess around with it, go check out foxidrive's scripts, join Batchworld and ask them before you do another Google. Please? **HUGz**
Actually, there is a way to do this.
Just a note regarding GOTO.
I limit its use with strict rules, and as such, prefer the CALL statement to implement a function based system to all my Batch scripts (Have a look through the python link in my sig, both for getting back into Python
and I think I have a DOS script somewhere in there, otherwise it was a PM to Silverware).
The other joy is to liberally use the START function when I want to perform multiple simultaneous functions, with no waiting around, at the same time.
NB, you have to call the function from outside a FOR loop, otherwise the information you want to use gets messed up, big time, and you'll start shedding hair!
@echo off
setlocal
:: if this doesn't work, just comment out all
:: three lines of this for loop..
for /L %%I IN (1,1,3) do (
call :CREATEREPORT %%I
)
:: and remove the :: from these 3 lines
:: set REPORT1=%CD%\REPORT-1.TXT
:: set REPORT2=%CD%\REPORT-2.TXT
:: set REPORT3=%CD%\REPORT-3.TXT
set REPORT=%CD%\REPORT.TXT
break > %REPORT%
echo.
echo You should have 4x report files:
dir REPORT*.TXT /b
call :WAIT 5
echo hi > %CD%\LIST-HI.TXT
set LIST=%CD%\LIST-HI.TXT
for /f %%J IN (%LIST%) do (
call :PROCESS %%J
)
copy %REPORT1%+%REPORT2%+%REPORT3% %REPORT%
goto :END
::_______________________________
:: Start of FUNCTIONS
:CREATEREPORT
set NUM=%1
break > REPORT-%NUM%.TXT
set REPORT%NUM% = REPORT-%NUM%.TXT
goto :EOF
:WAIT
set WAITTIME=%1
ping -n %WAITTIME% 127.0.0.1 > NUL
goto :EOF
:PROCESS
set VAR1=%1
start call :SCRIPT1 %VAR1%
start call :SCRIPT2 %VAR1%
start call :SCRIPT3 %VAR1%
goto :EOF
:SCRIPT1
:: NB create a lock file
echo %DATE% - %TIME% >> %REPORT1%
echo %1 - SCRIPT1 >> %REPORT1%
echo. >> %REPORT1%
:: NB delete the lock file
goto :EOF
:SCRIPT2
:: NB create a lock file
echo %DATE% - %TIME% >> %REPORT2%
echo %1 - SCRIPT2 >> %REPORT2%
echo. >> %REPORT2%
:: NB delete the lock file
goto :EOF
:SCRIPT3
:: NB create a lock file
echo %DATE% - %TIME% >> %REPORT3%
echo %1 - SCRIPT3 >> %REPORT3%
echo. >> %REPORT3%
:: NB delete the lock file
goto :EOF
:: End of FUNCTIONS
::_______________________________
:END
:: echo.
:: echo Waiting 2 seconds..
:: call :WAIT 2
start %REPORT%
endlocal
Ok, so I wrote the above script from my head, so completely untested. But do create an empty directory and paste into a new batch file in the new dir.
All it does is create a list file, with hi in it, then creates 3 report files, also with hi in it, then merges them together.
At the end it opens the REPORT.Txt file and it should show
hi
hi
hi
But the exercise here is to show how the start statement should initiate the SCRIPT "function", not wait for a response and continue to the next, and the next.
Now, be careful here. Timing is of the essence, which is why I have the "wait 2 seconds" at the end.. you may find that REPORT only has one entry, but the SCRIPT report files are fully populated.. this may serve your purposes, by breaking down what you need into individual scripts, and have a fire-and-forget function.
Ive added the date time to each of the reports now, which could help understand the process flow.
I think goto :EOF is a way to tell DOS that you want to return to the point where the function was called from.
In olde DOS days, the batch interpreter would jump to the end of the script (end of file), realise it still had a call statement flagged, and return processing to the end of the call statement, and resume whatever came next.
Olde DOS used goto statements everywhere, which is why I include meaningful names for my "functions" and include as much info as I can when testing/debugging.
P. S. I've edited all this on my phone, so please excuse any typos, and i think I have a rogue icode statement somewhere, I'll check back later as driving up North mow to test an emtb in Rostrevor (@Naed
)
P. P. S.
Be wary of the difference between;
> (overwrite, i use to create a new file)
and
>> (appends to an existing file, or adds to a new file)
Also check out
this post on how to create a zero sized file (for reporting), and search for "foxidrive"..
break > newfile.txt
.. is astounding, I only knew of a crazy thing using NUL (I'll post it when I find it)
The reason is that Windows sometimes doesn't know if you're creating a file or a directory. So create a null file, then link that with a report variable, less chance of errors, and then no requirement to use a single > and lose those important reports
P. P. P. S.
Be wary of the indexer you use in FOR loops.
You'll notice that I use %%I for creating reports, and %%J for the main process loop.
Just something I was taught by foxidrive, as DOS holds onto variables until you either exit the program (hence why I use setlocal and endlocal, so they aren't kept in the DOS window afterwards) or you explicitly clear it by stating;
REPORT1=
It also reduces the potential of wtf moments, when a previous function used a different type of info to the one you want to use..
createreport uses /L for integers
process uses /F for strings/ text
oh my word, Ive been editing, reediting and adding new code and functionality for the last hour.. this is mad!
Adam! What hae you started!
It's all good, I like helping people (anyone tbh), as I tend to learn something new in the process