Batch file in question ... sometimes it works! FileSize key

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
OptimalDesigns
Posts: 6
Joined: 22 Nov 2014 07:56
Location: Medford, Oregon
Contact:

Batch file in question ... sometimes it works! FileSize key

#1 Post by OptimalDesigns » 22 Nov 2014 08:47

Output\Beam.out file does NOT exist after a run. Turned on ECHO and got following response.

Code: Select all

E:\Tools.vb\FC-Compiler\demos\ODEs-ivp>if not exist output\nul mkdir output

E:\Tools.vb\FC-Compiler\demos\ODEs-ivp>if exist scrout (
set mask=scrout
 [b]for /R %a in ((null)) do (if %~za GTR 400 ([/b]
rem Some -good- output
 copy scrout  output\Beam.out  1>nul
 copy scrout+output.txt  E:\Tools.vb\FC-Compiler\~out.txt  1>nul
)  ELSE (
copy scrout+output.txt E:\Tools.vb\FC-Compiler\~out.txt    1>nul
 copy scrout+output.txt output\Beam.out  1>nul
) )
 copy scrout scrout.txt  1>nul
 del scrout  1>nul
)  ELSE (
copy output.txt E:\Tools.vb\FC-Compiler\~out.txt    1>nul
 copy output.txt output\Beam.out  1>nul
)

E:\Tools.vb\FC-Compiler\demos\ODEs-ivp>set FCtmp=Beam.fc
E:\Tools.vb\FC-Compiler\demos\ODEs-ivp>if /I fc == exp set FCtmp=
E:\Tools.vb\FC-Compiler\demos\ODEs-ivp>if /I fc == exe set FCtmp=
E:\Tools.vb\FC-Compiler\demos\ODEs-ivp>if /I fc == obj set FCtmp=
E:\Tools.vb\FC-Compiler\demos\ODEs-ivp>if exist "C:\Program Files (x86)\Notepad+
+\notepad++.exe " (
echo "C:\Program Files (x86)\Notepad++\notepad++.exe " -alwaysOnTop  Beam.fc out
put\Beam.out
 "C:\Program Files (x86)\Notepad++\notepad++.exe " -alwaysOnTop  Beam.fc output\
Beam.out
)  else (C:\Windows\notepad.exe output\Beam.out )
"C:\Program Files (x86)\Notepad++\notepad++.exe " -alwaysOnTop  Beam.fc output\B
eam.out

E:\Tools.vb\FC-Compiler\demos\ODEs-ivp>dir Beam.out, output.txt, scrout.txt
 Volume in drive E is Textbook
 Volume Serial Number is 1E41-B870

 Directory of E:\Tools.vb\FC-Compiler\demos\ODEs-ivp


 Directory of E:\Tools.vb\FC-Compiler\demos\ODEs-ivp
11/21/2014  09:38 AM             4,924 OUTPUT.TXT

 Directory of E:\Tools.vb\FC-Compiler\demos\ODEs-ivp
11/21/2014  09:38 AM             1,090 scrout.txt


Here is the batch code that generated the above echo.

Code: Select all

echo on

rem set f-out=%od-tools%\%FCdir%\...\output\%f-out%
  if not exist output\nul  mkdir output
  if exist scrout  (
   set mask=scrout
   for /r %%a in (%mask%) do (
     [b]if %%~za gtr 400 ([/b]
rem Some -good- output
         copy scrout  [b]output\%f-out%[/b] > nul
         copy scrout+output.txt  %od-tools%\%FCdir%\~out.txt > nul
    [b] ) ELSE ([/b]
         copy scrout+output.txt %od-tools%\%FCdir%\~out.txt > nul 
         copy scrout+output.txt [b]output\%f-out%[/b] > nul
     )
   )
   copy scrout scrout.txt > nul
   del scrout > nul
  ) ELSE (
     copy output.txt %od-tools%\%FCdir%\~out.txt > nul 
     copy output.txt output\%f-out% > nul
    )
   set FCtmp=%FCname%.%FCext%
   if /I %FCext%==exp  set FCtmp=
   if /I %FCext%==exe  set FCtmp=
   if /I %FCext%==obj  set FCtmp=
   if exist "C:\Program Files (x86)\Notepad++\notepad++.exe " (
   echo "C:\Program Files (x86)\Notepad++\notepad++.exe " -alwaysOnTop  %FCtmp% output\%f-out%
   "C:\Program Files (x86)\Notepad++[i]\notepad++.exe[/i] " -alwaysOnTop  %FCtmp% [b]output\%f-out%[/b]
 ) else (
   %WINDIR%\notepad.exe output\%f-out%
 )


Hmm, ... in above code didn't work. I was trying to make bold the important parts of code.

Here is the code that is key to this problem.

for /r %%a in (%mask%) do (
if %%~za gtr 400 (
copy scrout output\%f-out% > nul
) ELSE (
copy scrout+output.txt output\%f-out% > nul
)
Either route should create an output\%f-out% file. :(
Both 'scrout' and 'output.txt' files exist at this time.

When the output file, %f-out% = Beam.out here, filesize is small ... 1,090 bytes. It is also a very fast run. I can't read the echo response to figure out what code got executed on the filesize test, see BOLD lines of code and Echo response. On slower runs where the %f-out% is of larger filesize, this batch code works fine. \notepad++.exe complains about opening a non-existent file, output\%f-out%.

Ideas how to solve this batch code?

Thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file in question ... sometimes it works! FileSize

#2 Post by foxidrive » 22 Nov 2014 09:46

Your parentheses after the first nested loop are mismatched by the look of it.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file in question ... sometimes it works! FileSize

#3 Post by foxidrive » 22 Nov 2014 09:50

Use these modifications and see if the right routines are being executed:

Code: Select all

  if exist scrout  (
   set mask=scrout
   for /r %%a in (%mask%) do (
     if %%~za gtr 400
    echo pause 1 "%%a" & pause
rem Some -good- output
         copy scrout  [b]output\%f-out%[/b] > nul
         copy scrout+output.txt  %od-tools%\%FCdir%\~out.txt > nul
     ) ELSE (
    echo pause 2 "%%a" & pause
         copy scrout+output.txt %od-tools%\%FCdir%\~out.txt > nul
         copy scrout+output.txt [b]output\%f-out%[/b] > nul
     )
   )
    echo pause 3 & pause

OptimalDesigns
Posts: 6
Joined: 22 Nov 2014 07:56
Location: Medford, Oregon
Contact:

Re: Batch file in question ... sometimes it works! FileSize

#4 Post by OptimalDesigns » 22 Nov 2014 12:10

This may show part of problem, but why does it not get to the if statement?

Code: Select all

E:\Tools.vb\FC-Compiler\demos\ODEs-ivp>rem set f-out=E:\Tools.vb\FC-Compiler\...
\output\Beam.out

E:\Tools.vb\FC-Compiler\demos\ODEs-ivp>if not exist output\nul mkdir output

E:\Tools.vb\FC-Compiler\demos\ODEs-ivp>if exist scrout (
set mask=scrout
 for /R %a in ((null)) do (if %~za GTR 400 (echo pause 1 "%a"   & pause
 rem Some -good- output
 copy scrout  output\Beam.out  1>nul
 copy scrout+output.txt  E:\Tools.vb\FC-Compiler\~out.txt  1>nul )  ELSE (echo p
ause 2 "%a"   & pause
 copy scrout+output.txt E:\Tools.vb\FC-Compiler\~out.txt    1>nul
 copy scrout+output.txt output\Beam.out  1>nul ) )
 echo pause 3 "%a"   & pause
 copy scrout scrout.txt  1>nul
 del scrout  1>nul
)  ELSE (
copy output.txt E:\Tools.vb\FC-Compiler\~out.txt    1>nul
 copy output.txt output\Beam.out  1>nul
)
pause 3 "%a"
Press any key to continue . . .


So the 3rd pause was executed but why did the if statement get skipped? And, why was %a not replaced with a file name?

Phil

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file in question ... sometimes it works! FileSize

#5 Post by foxidrive » 22 Nov 2014 12:18

You've set mask=scrout within a loop and delayed expansion is required to set-and-use a variable within parenthesis.

http://ss64.com/nt/setlocal.html for info.

Your chosen for /r loop also needs a wildcard in there to give you the correct files, once you deal with the variable.

OptimalDesigns
Posts: 6
Joined: 22 Nov 2014 07:56
Location: Medford, Oregon
Contact:

Re: Batch file in question ... sometimes it works! FileSize

#6 Post by OptimalDesigns » 22 Nov 2014 12:39

Moved set mask=... outside ( ... It seems to work ... %a still their, why? are they okay?

Code: Select all

E:\Tools.vb\FC-Compiler\demos\ODEs-ivp>set mask=scrout

E:\Tools.vb\FC-Compiler\demos\ODEs-ivp>if not exist output\nul mkdir output

E:\Tools.vb\FC-Compiler\demos\ODEs-ivp>if exist scrout (
for /R %a in (scrout) do (if %~za GTR 400 (echo pause 1 "%a"   & pause
 rem Some -good- output
 copy scrout  output\Beam.out  1>nul
 copy scrout+output.txt  E:\Tools.vb\FC-Compiler\~out.txt  1>nul )  ELSE (echo p
ause 2 "%a"   & pause
 copy scrout+output.txt E:\Tools.vb\FC-Compiler\~out.txt    1>nul
 copy scrout+output.txt output\Beam.out  1>nul ) )
 echo pause 3 "%a"   & pause
 copy scrout scrout.txt  1>nul
 del scrout  1>nul
)  ELSE (
copy output.txt E:\Tools.vb\FC-Compiler\~out.txt    1>nul
 copy output.txt output\Beam.out  1>nul
)

E:\Tools.vb\FC-Compiler\demos\ODEs-ivp>(if 1090 GTR 400 (echo pause 1 "E:\Tools.
vb\FC-Compiler\demos\ODEs-ivp\scrout"   & pause
 rem Some -good- output
 copy scrout  output\Beam.out  1>nul
 copy scrout+output.txt  E:\Tools.vb\FC-Compiler\~out.txt  1>nul )  ELSE (echo p
ause 2 "E:\Tools.vb\FC-Compiler\demos\ODEs-ivp\scrout"   & pause
 copy scrout+output.txt E:\Tools.vb\FC-Compiler\~out.txt    1>nul
 copy scrout+output.txt output\Beam.out  1>nul ) )
pause 1 "E:\Tools.vb\FC-Compiler\demos\ODEs-ivp\scrout"
Press any key to continue . . .

E:\Tools.vb\FC-Compiler\demos\ODEs-ivp>(if  GTR 400 (echo pause 1 "E:\Tools.vb\F
C-Compiler\demos\ODEs-ivp\output\scrout"   & pause
 rem Some -good- output
 copy scrout  output\Beam.out  1>nul
 copy scrout+output.txt  E:\Tools.vb\FC-Compiler\~out.txt  1>nul )  ELSE (echo p
ause 2 "E:\Tools.vb\FC-Compiler\demos\ODEs-ivp\output\scrout"   & pause
 copy scrout+output.txt E:\Tools.vb\FC-Compiler\~out.txt    1>nul
 copy scrout+output.txt output\Beam.out  1>nul ) )
pause 2 "E:\Tools.vb\FC-Compiler\demos\ODEs-ivp\output\scrout"
Press any key to continue . . .
pause 3 "%a"
Press any key to continue . . .


Got my output\beam.out file.

Phil

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch file in question ... sometimes it works! FileSize

#7 Post by Ed Dyreen » 22 Nov 2014 21:02

i don't know the conditions, so i marked them with question marks, changed it a bit, no errors, i assume this does the same as yours, hope it helps..

Code: Select all

@echo on

set "od-tools=?"
set "FCdir=?"
set "f-out=?"
set "FCname=?"
set "FCext=?"

rem set "f-out=%od-tools%\%FCdir%\...\output\%f-out%"

pushd output &&popd ||mkdir output

for %%? in ( "scrout" ) do if exist %%? (
        ::
        for /r %%? in ( "%%~?" ) do if %%~z? gtr 400 (
                ::
                rem Some -good- output
                >nul copy "scrout" "output\%f-out%"
                >nul copy "scrout" + "output.txt" "%od-tools%\%FCdir%\~out.txt"
        ) ELSE (
                >nul copy "scrout" + "output.txt" "%od-tools%\%FCdir%\~out.txt"
                >nul copy "scrout" + "output.txt" "output\%f-out%"
        )
        >nul copy "scrout" "scrout.txt"
        del "scrout"
) ELSE (
        >nul copy "output.txt" "%od-tools%\%FCdir%\~out.txt"
        >nul copy "output.txt" "output\%f-out%"
)

set "FCtmp=%FCname%.%FCext%"
if /I "%FCext%" == "exp" "set FCtmp="
if /I "%FCext%" == "exe" "set FCtmp="
if /I" %FCext%" == "obj" "set FCtmp="
if exist "C:\Program Files (x86)\Notepad++\notepad++.exe" (
        ::
        echo("C:\Program Files (x86)\Notepad++\notepad++.exe" -alwaysOnTop "%FCtmp%" "output\%f-out%"
        "C:\Program Files (x86)\Notepad++\notepad++.exe" -alwaysOnTop "%FCtmp%" "output\%f-out%"

) else  "%WINDIR%\notepad.exe output\%f-out%"

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file in question ... sometimes it works! FileSize

#8 Post by foxidrive » 22 Nov 2014 23:11

OptimalDesigns wrote:Moved set mask=... outside ( ... It seems to work

foxidrive wrote:Your chosen for /r loop also needs a wildcard in there to give you the correct files, once you deal with the variable.

Open a cmd prompt in c:\users\phil for example, and test this

Code: Select all

for /R %a in (scrout) do @echo %a&pause

OptimalDesigns
Posts: 6
Joined: 22 Nov 2014 07:56
Location: Medford, Oregon
Contact:

Re: Batch file in question ... sometimes it works! FileSize

#9 Post by OptimalDesigns » 26 Nov 2014 16:07

Ed Dyreen wrote:i don't know the conditions, so i marked them with question marks, changed it a bit, no errors, i assume this does the same as yours, hope it helps..

Code: Select all

for %%? in ( "scrout" ) do if exist %%? (
        ::
        for /r %%? in ( "%%~?" ) do if %%~z? gtr 400 (
                ::


With this change that you recommended, all seems to run fine. 6 or so app.s use this common batch file so it may take a while to test all app.s.

Thanks for your help.
Phil
PS: any way one can donate for such great help?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch file in question ... sometimes it works! FileSize

#10 Post by Ed Dyreen » 26 Nov 2014 23:20

OptimalDesigns wrote:PS: any way one can donate for such great help?
want to donate a cup of cafe? . click ., dosTips seems currently unable to accept donations, i don't know :(

OptimalDesigns
Posts: 6
Joined: 22 Nov 2014 07:56
Location: Medford, Oregon
Contact:

Re: Batch file in question ... sometimes it works! FileSize

#11 Post by OptimalDesigns » 02 Dec 2014 18:20

Ed Dyreen wrote:i don't know the conditions, so i marked them with question marks, changed it a bit, no errors, i assume this does the same as yours, hope it helps..

Code: Select all

for %%? in ( "scrout" ) do if exist %%? (
        ::
        for /r %%? in ( "%%~?" ) do if %%~z? gtr 400 (
                ::


Still giving me problems ... changed '%~z?' to '0%~z?' in the above test since it kept showing as a blank ie. 'do if gtr 400'. Adding the '0' gives a 'do if 0 gtr 400' which looks and reads better if nothing else. The output below shows problems with file 'scrout'; filesize=2144.

Code: Select all

E:\Tools.vb\FC-Compiler\demos\algebra>if exist scrout dir scrout
 Volume in drive E is Textbook
 Volume Serial Number is 1E41-B870

 Directory of E:\Tools.vb\FC-Compiler\demos\algebra

12/03/2014  05:14 AM             2,144 Scrout
               1 File(s)          2,144 bytes
               0 Dir(s)  72,817,561,600 bytes free

ooo


E:\Tools.vb\FC-Compiler\demos\algebra>for %? in ("scrout") do if exist %? (for /
R %? in ("%~?") do if 0%~z? GTR 0400 (
rem Some -good- output
 echo   Executing path 1a
 copy "scrout" "output\circuit.out"
 copy "scrout" + "output.txt" "E:\Tools.vb\FC-Compiler\~out.txt"
)  ELSE (
echo    Executing path 1b
 copy "scrout" + "output.txt" "E:\Tools.vb\FC-Compiler\~out.txt"
 copy "scrout" + "output.txt" "output\circuit.out"
) )  ELSE (
echo    Executing path 2a
 copy "output.txt" "E:\Tools.vb\FC-Compiler\~out.txt"
 copy "output.txt" output\circuit.out
)

E:\Tools.vb\FC-Compiler\demos\algebra>if exist "scrout" (for /R %? in ("scrout")
 do if 02144 GTR 0400 (
rem Some -good- output
 echo   Executing path 1a
 copy "scrout" "output\circuit.out"
 copy "scrout" + "output.txt" "E:\Tools.vb\FC-Compiler\~out.txt"
)  ELSE (
echo    Executing path 1b
 copy "scrout" + "output.txt" "E:\Tools.vb\FC-Compiler\~out.txt"
 copy "scrout" + "output.txt" "output\circuit.out"
) )  ELSE (
echo    Executing path 2a
 copy "output.txt" "E:\Tools.vb\FC-Compiler\~out.txt"
 copy "output.txt" output\circuit.out
)

E:\Tools.vb\FC-Compiler\demos\algebra>if 0 GTR 0400 (
rem Some -good- output
 echo   Executing path 1a
 copy "scrout" "output\circuit.out"
 copy "scrout" + "output.txt" "E:\Tools.vb\FC-Compiler\~out.txt"
)  ELSE (
echo    Executing path 1b
 copy "scrout" + "output.txt" "E:\Tools.vb\FC-Compiler\~out.txt"
 copy "scrout" + "output.txt" "output\circuit.out"
)
Executing path 1b
Scrout
OUTPUT.TXT
        1 file(s) copied.
Scrout
OUTPUT.TXT
        1 file(s) copied.

E:\Tools.vb\FC-Compiler\demos\algebra>if 0 GTR 0400 (
rem Some -good- output
 echo   Executing path 1a
 copy "scrout" "output\circuit.out"
 copy "scrout" + "output.txt" "E:\Tools.vb\FC-Compiler\~out.txt"
)  ELSE (
echo    Executing path 1b
 copy "scrout" + "output.txt" "E:\Tools.vb\FC-Compiler\~out.txt"
 copy "scrout" + "output.txt" "output\circuit.out"
)
Executing path 1b
Scrout
OUTPUT.TXT
        1 file(s) copied.
Scrout
OUTPUT.TXT
        1 file(s) copied.

E:\Tools.vb\FC-Compiler\demos\algebra>copy "scrout" "scrout.txt"
        1 file(s) copied.

E:\Tools.vb\FC-Compiler\demos\algebra>del "scrout"


Why double execution of my "path 1b" after the test "if 0 GTR 0400 (" ... it should have executed "path 1a" after the "if 02144 GTR 0400 (" test, right? What gives?

Thanks, Phil

Post Reply