1.

Solve : Dos:Unable to call multiple bat files from single bat file?

Answer» <html><body><p>I am trying to <a href="https://interviewquestions.tuteehub.com/tag/call-412416" style="font-weight:bold;" target="_blank" title="Click to know more about CALL">CALL</a> the bat file(main.bat) which contains 2 bat files(first, second and first bat files internally calls multiple bat files and outputs that to a file(also echos) and second bat file reads that file and calculates the total and echos back to the dos<br/>so when I execute the main bat file, it is executing the first bat file and giving the results, but the second bat file I dont see it is executing as it is not echoing any results.<br/><br/>Following is the code of the main bat file<br/><br/>D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MAbatch\jobs\old_Campaign_DB_Person al.bat --- first bat<br/>call D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MAbatch\jobs\Count_db_pers.bat----second bat<br/><br/>Can some body please help me on this? <br/>In the code you posted, control is passed to the first batch script, but you did not use CALL, so therefore control will never return.<br/><br/>Thankyou..I have tried with the Call, but still it is not working.. following is the code for the bat files..<br/><br/>Following is the code of the first bat file...<br/><br/>setlocal<br/>REM *********************************************************<br/>REM * Rundate 'D'YYMMDD must be <a href="https://interviewquestions.tuteehub.com/tag/specified-1221578" style="font-weight:bold;" target="_blank" title="Click to know more about SPECIFIED">SPECIFIED</a> as first argument *<br/>REM *********************************************************<br/><br/>REM *********************************************************<br/>REM * Command-file must exist *<br/>REM *********************************************************<br/>FOR /R D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MASched\jobs\DB_Personal\ %%G IN (*.bat) DO start /b "call D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MASched\jobs\DB_Personal\" %%G<br/><br/><br/>set rc=%errorlevel%<br/>REM *******************************************************<br/>REM * Exit script and report return code *<br/>REM *******************************************************<br/>:done<br/>echo rc=%rc%<br/>exit /b %rc%<br/><br/>REM *******************************************************<br/>REM * Handle argument errors *<br/>REM *******************************************************<br/>:noargument<br/>echo Error: No argument specified<br/>set rc=2<br/>goto done<br/>:nopgm<br/>echo Error: Command file "%sch_command%" does not exist<br/>set rc=2<br/>goto done<br/><br/>Following is the code of the second bat file.<br/><br/>echo off &amp; setLocal enableDELAYedexpansion<br/>set toterrors=<br/>set totjobs=<br/>for /f "tokens=1 delims= " %%a in (D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MASched\jobs\count\count.txt) do (<br/>set /a toterrors+=%%a<br/>)<br/>for /f "tokens=2 delims= " %%b in (D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MASched\jobs\count\count.txt) do (<br/>set /a totjobs+=%%b<br/>)<br/>echo total errors is !toterrors!<br/>echo total jobs is !totjobs!<br/>goto :eof <br/>I am sorry but you have a needlessly complicated problem.<br/><br/>You are using D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MASched\jobs\DB_Personal\<br/>Better to use<br/> Code: <a>[Select]</a>D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MASched\jobs\DB_Personal\<br/>INFINITELY better to alter your problem and the code for which you need help by using<br/> Code: <a>[Select]</a>D:\DB_Personal\<br/>You seem to have a problem involving CALL and START.<br/>Why do you confuse the whole situation with the complication of<br/>FOR /R ...... %%G IN (*.bat) DO .....<br/><br/>Finally, this code fragment is ludicrously wrong<br/> Code: <a>[Select]</a>DO start /b "call D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MASched\jobs\DB_Personal\" %%G<br/><br/>As Sidewinder has already pointed out in your simultaneous topic on this same horrible code,<br/>you should use start or call, but not both.<br/>REGARDLESS of all the previous horrors, buried at the end of all the needless verbiage, is the SPACE character, i.e.<br/> Code: <a>[Select]</a>D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MASched\jobs\DB_Personal\" %%G<br/>I think you might have better luck with<br/> Code: <a>[Select]</a>"D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MASched\jobs\DB_Personal\%%G"<br/><br/>Alan<br/> Quote from: ALAN_BR on November 21, 2010, 10:02:35 AM</p><blockquote>You are using D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MASched\jobs\DB_Personal\<br/>Better to use<br/> Code: <a>[Select]</a>D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MASched\jobs\DB_Personal\<br/>INFINITELY better to alter your problem and the code for which you need help by using<br/> Code: <a>[Select]</a>D:\DB_Personal\</blockquote> True, but chances are the file system arrangement is beyond their control. You can't just say "Because it is easier for me, everybody must store their files in this particular arrangement" in a company.<br/><br/> Quote<blockquote>Why do you confuse the whole situation with the complication of<br/>FOR /R ...... %%G IN (*.bat) DO .....<br/></blockquote> Probably because that was in his batch file.<br/><br/> Quote<blockquote>Finally, this code fragment is ludicrously wrong<br/> Code: <a>[Select]</a>DO start /b "call D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MASched\jobs\DB_Personal\" %%G<br/><br/>As Sidewinder has already pointed out in your simultaneous topic on this same horrible code,<br/>you should use start or call, but not both.<br/></blockquote> It works fine. It looks like they were trying to run the various batch files asynchronously, but because /b was specified they all end up waiting on each other for the console anyway.<br/><br/> Quote<blockquote>REGARDLESS of all the previous horrors, buried at the end of all the needless verbiage, is the SPACE character, i.e.<br/> Code: <a>[Select]</a>D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MASched\jobs\DB_Personal\" %%G<br/>I think you might have better luck with<br/> Code: <a>[Select]</a>"D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MASched\jobs\DB_Personal\%%G"<br/></blockquote> it works either way.<br/> Quote from: BC_Programmer on November 22, 2010, 02:09:34 PM<blockquote>True, but chances are the file system arrangement is beyond their control. You can't just say "Because it is easier for me, everybody must store their files in this particular arrangement" in a company.<br/>Probably because that was in his batch file.<br/></blockquote> I accept that he cannot stipulate the paths that are used company wide,<br/>but he SHOULD have the ability to compose a very concise file path with which to explore how to call / start bat scripts.<br/><br/>I find extremely verbose scripts to be a pain and tend to obscure any tiny (but significant) code error.<br/><br/>I always prefer to have a small script with the one feature that does not work for me,<br/>and only after learning how to use it will I go back to the grand picture.<br/><br/> Quote<blockquote>it works either way.<br/></blockquote> Are you sure, or did you fail to spot the obscure error to which I drew attention ?<br/><br/>Please explain any error in my test and explanation which follows.<br/><br/>I have created TEST.BAT at location D:\DAT<br/>TEST.BAT code<br/> Code: <a>[Select]</a>echo This script is %0<br/><br/>I have used RUN and launched CMD.EXE and invoked TEST.BAT twice, with and without the extraneous space character, and selected and pasted the window contents below.<br/> Code: <a>[Select]</a>Microsoft Windows XP [Version 5.1.2600]<br/>(C) Copyright 1985-2001 Microsoft Corp.<br/><br/>C:\Documents and Settings\Dad&gt;CALL D:\DAT\ TEST<br/>'D:\DAT\' is not recognized as an internal or external command,<br/>operable program or batch file.<br/><br/>C:\Documents and Settings\Dad&gt;CALL D:\DAT\TEST<br/><br/>C:\Documents and Settings\Dad&gt;echo This script is D:\DAT\TEST<br/>This script is D:\DAT\TEST<br/><br/>C:\Documents and Settings\Dad&gt;<br/>Regards<br/>Alan<br/> Quote from: ALAN_BR on November 22, 2010, 03:22:49 PM<blockquote>Are you sure, or did you fail to spot the obscure error to which I drew attention ?<br/><br/>Please explain any error in my test and explanation which follows.<br/><br/>I have created TEST.BAT at location D:\DAT<br/>TEST.BAT code<br/> Code: <a>[Select]</a>echo This script is %0<br/><br/>I have used RUN and launched CMD.EXE and invoked TEST.BAT twice, with and without the extraneous space character, and selected and pasted the window contents below.<br/> Code: <a>[Select]</a>Microsoft Windows XP [Version 5.1.2600]<br/>(C) Copyright 1985-2001 Microsoft Corp.<br/><br/>C:\Documents and Settings\Dad&gt;CALL D:\DAT\ TEST<br/>'D:\DAT\' is not recognized as an internal or external command,<br/>operable program or batch file.<br/><br/>C:\Documents and Settings\Dad&gt;CALL D:\DAT\TEST<br/><br/>C:\Documents and Settings\Dad&gt;echo This script is D:\DAT\TEST<br/>This script is D:\DAT\TEST<br/><br/>C:\Documents and Settings\Dad&gt;<br/>Regards<br/>Alan<br/></blockquote> Works for me. Probably because I tested what he had and was executing, rather then a more bare case that essentially avoids any issue.<br/><br/>First, I created a bunch of batch files in D:\testbat, test1.bat through test9.bat, they simple contained:<br/><br/> Code: <a>[Select]</a>echo this is %0<br/><br/><br/><br/><br/>and then this one (in a separate folder)<br/><br/> Code: <a>[Select]</a>FOR /R D:\testbat\ %%G IN (*.bat) DO start /b "call D:\testbat\" %%G<br/><br/>and the output:<br/> Code: <a>[Select]</a><br/>D:\testbat&gt;start /b "call D:\testbat\" D:\testbat\test1.bat<br/><br/>D:\testbat&gt;start /b "call D:\testbat\" D:\testbat\test10.bat<br/><br/>D:\testbat&gt;start /b "call D:\testbat\" D:\testbat\test2.bat<br/><br/>D:\testbat&gt;start /b "call D:\testbat\" D:\testbat\test3.bat<br/><br/>D:\testbat&gt;start /b "call D:\testbat\" D:\testbat\test4.bat ♪◙<br/>D:\testbat&gt;start /b "call D:\testbat\" D:\testbat\test5.bat<br/>this is D:\testbat\test1.bat<br/><br/>D:\testbat&gt;start /b "call D:\testbat\" D:\testbat\test6.bat ♪◙<br/><br/>D:\testbat&gt;start /b "call D:\testbat\" D:\testbat\test7.bat<br/>D:\testbat&gt;this is D:\testbat\test2.bat<br/><br/>this is D:\testbat\test10.bat<br/>D:\testbat&gt;this is D:\testbat\test3.bat<br/>start /b "call D:\testbat\" D:\testbat\test8.bat<br/><br/>D:\testbat&gt;<br/><br/>D:\testbat&gt;start /b "call D:\testbat\" D:\testbat\test9.bat<br/>D:\testbat&gt;<br/>D:\testbat&gt;<br/>D:\testbat&gt;this is D:\testbat\test4.bat<br/>this is D:\testbat\test7.bat<br/><br/>D:\testbat&gt;this is D:\testbat\test5.bat<br/>this is D:\testbat\test6.bat<br/><br/>D:\testbat&gt;<br/>D:\testbat&gt;this is D:\testbat\test8.bat<br/><br/>D:\testbat&gt;<br/>D:\testbat&gt;this is D:\testbat\test9.bat<br/><br/>which was certainly weird (what with the starts and the fact that the output was usually delayed and they had to wait for each other and all) but there were no syntax errors.<br/><br/><br/>On the other hand, when I said "it works either way"  I was being disingenuous; truly, it doesn't work with the %%G in the quotes. In the first case (outside the quotes) start is using the quoted portion as the window title, and then executes the second part (the batch filename). Whether this was intended, I haven't a clue. Including the %%G within the quotes would cause an error, as it would then expand to start /b "call D:\testbat\D:\testbat\test9.bat"  which clearly wouldn't work.<br/><br/>Also, I rather like the musical symbols that the batch throws in the output. adds a nice touch.<br/><br/>Thanks for the explanation.<br/><br/>I like your concise code, i.e.<br/> Code: <a>[Select]</a>FOR /R D:\testbat\ %%G IN (*.bat) DO start /b "call D:\testbat\" %%G<br/>I now see that "call D:\testbat\" is the title  and not part of the command.<br/><br/>I was rather confused that the title was preceded by the argument /b.<br/>I thought the title was always first after the start.<br/><br/>It has been a long time since I used "FOR /R" and I wrongly guessed that %%G would be appending the name of each *.bat in the folder, so assumed the intention was <br/> Code: <a>[Select]</a>call "D:\very long path which MAY have spaces that need quotes\"%%Gin which case a space before the %%G would have broken the path<br/><br/>Question, what are the benefits and side effects of giving a title to a program with no window so no title <a href="https://interviewquestions.tuteehub.com/tag/bar-237951" style="font-weight:bold;" target="_blank" title="Click to know more about BAR">BAR</a> ?<br/>I have just created t.bat and started it with a title,<br/>it ran in the "DOS window" that invoked it and had no effect on the existing title bar,<br/>no effect whilst it was paused,<br/>no effect after it was ended.<br/> Code: <a>[Select]</a>C:\Documents and Settings\Dad&gt;echo pause &gt; t.bat<br/><br/>C:\Documents and Settings\Dad&gt;start /b "a silly misleading title" t<br/><br/>C:\Documents and Settings\Dad&gt;<br/>C:\Documents and Settings\Dad&gt;pause<br/>Press any key to continue . . .<br/>C:\Documents and Settings\Dad&gt;<br/><br/>Regards<br/>Alan<br/> Quote from: ALAN_BR on November 23, 2010, 05:23:44 AM<blockquote>I like your concise code, i.e.<br/> Code: <a>[Select]</a>FOR /R D:\testbat\ %%G IN (*.bat) DO start /b "call D:\testbat\" %%G</blockquote> Not sure if it's sarcasm... but in any case, I basically wanted to test what they had; so that I could see the same sort of behaviour. Upon seeing that it worked, it was possible to  figure out why.<br/><br/> Quote<blockquote>I was rather confused that the title was preceded by the argument /b.<br/>I thought the title was always first after the start.<br/></blockquote> I don't think it really matters wether switches appear before or after; although they have to appear before the command (which in this case is %%G) (otherwise those switches will be sent to the command)<br/><br/><br/> Quote<blockquote>Question, what are the benefits and side effects of giving a title to a program with no window so no title bar ?<br/></blockquote> none... since the /b switch suppresses creating a new window, the title has no effect. I suspect the batch was only working entirely accidentally.<br/> Quote from: BC_Programmer on November 23, 2010, 05:28:24 AM<blockquote>Not sure if it's sarcasm... <br/></blockquote> Definitely not sarcasm.<br/>Short and all on one line, not long and wrapped to continue on following line.  Simple to read.<br/>I admire your patience at testing every aspect of the original code.<br/><br/>I developed undying hatred of software verbosity when I inherited and had to correct code from vandals that <a href="https://interviewquestions.tuteehub.com/tag/moved-2846161" style="font-weight:bold;" target="_blank" title="Click to know more about MOVED">MOVED</a> away.<br/>They left me with 'C' code in which every line of code was up to 400 characters wide.<br/>When I scrolled to the last bug on a line, I could no longer see the previous 75% of the line.<br/>Partly the problem was that they chose to indent the code by increments of 8 characters and they had many levels of indentation,<br/>but mostly they chose NOT to document Global Variables with a comment upon the real purpose,<br/>and instead the variable was given a <a href="https://interviewquestions.tuteehub.com/tag/lengthy-2787497" style="font-weight:bold;" target="_blank" title="Click to know more about LENGTHY">LENGTHY</a> self documenting name.  e.g. code such as<br/>Value_to_put_in_DAC_register_1 ^= Value_for_DAC_register_1 *= Scaling_Factor_for PAL_Colour - PAL_SECAM_OFFSET etc etc<br/><br/>Some of their code depended upon the "rules of precedence" for which there are ANSI standard rules,<br/>but the compilers in use were not quite compliant ! ! !<br/><br/>Regards<br/>Alan<br/></body></html>


Discussion

No Comment Found