1

This batch script stops after the loop is completed, (successfully), but I want it to continue to run commands after my loop.

for %%F in (*.tif) do (
    echo Processing %%F
    echo Computing binary mask
    call gdalbuildvrt -a_srs EPSG:25832 -b 1 processed/%%~nF_b1.vrt %%F
    gdalbuildvrt -a_srs EPSG:25832 -b 2 processed/%%~nF_b2.vrt %%F
    gdalbuildvrt -a_srs EPSG:25832 -b 3 processed/%%~nF_b3.vrt %%F
    gdal_calc --type=Byte --creation-option=PHOTOMETRIC=MINISBLACK ^
    --creation-option=NBITS=1 --creation-option=TILED=YES ^
    --creation-option=COMPRESS=DEFLATE ^
    -A processed/%%~nF_b1.vrt -B processed/%%~nF_b2.vrt -C processed/%%~nF_b3.vrt ^
    --calc="logical_not(logical_and(logical_and(A==0,B==0),C==0))" ^
    --outfile processed/%%~nF.msk.tif
)

dir /b /s *.tif > index.txt

Why would not the next command after the loop run? If I run them individually in in the command line there are no issues.

jeb
  • 78,592
  • 17
  • 171
  • 225
geogrow
  • 485
  • 2
  • 9
  • 26
  • 1
    Batch is Windows, Bash is UNIX. both are different. Which one are you using? By the way, what do you want to do? Filtering, e.g., can be achieved using `grep` (bash or Cygwin/batch) or `findstr` (batch). – Dominique Jun 14 '23 at 08:52
  • The syntax in your example has nothing to do with Bash. – Andrej Podzimek Jun 14 '23 at 08:57
  • Without knowing what `doing things here!` is, we'd be guessing – Magoo Jun 14 '23 at 09:01
  • Your loop looks correct to me. The only thing I can think of is that one of your tools crashes, causing the batchfile to stop working too. – Dominique Jun 14 '23 at 09:13
  • Individually everything work fine. Everything work fine if I run them "manual" in the command line. That's why i was surprised that it did not work in my bash-file. But then I ever really work much with bash-files. I guess I will be running my commands "manually" in the CMD – geogrow Jun 14 '23 at 09:28
  • 1
    You have made no attempt to handle file basenames with spaces and/or poison characters. Also I can see a definite inconsistency in your submission. `call gdalbuildvrt...` vs `gdalbuildvrt...` vs `gdalbuildvrt...`. The latter two are correct; the `call` command is **only** for other batch files or internal labels. – Compo Jun 14 '23 at 09:59
  • 1
    Run in a command prompt window `where gdalbuildvrt` and `where gdal_calc` and use the fully qualified file names as output by `%SystemRoot%\System32\where.exe`. If the file extension is `.bat` or `.cmd` (batch file) and not `.exe` or `.com` (executable), there must be used the command `call` to __call__ the batch file from within a batch file. See also: [How to call a batch file that is one level up from the current batch file directory?](https://stackoverflow.com/a/24725044/3074564) I suggest further using just one line for one __command line__ and do not use multiple lines with `^` at end. – Mofi Jun 14 '23 at 17:06
  • 1
    Multi-line command lines are often used on web pages and in documentations to avoid readers need to scroll horizontally or because of the web page is not designed at all for one long line in more or less fixed width web page. Such a multi-line command line should not be used in a script although possible because of a programmer can easily scroll left/right on editing the code in a text editor and an erroneous trailing space/tab not visible in text editor after `^` breaks the code and the command line or entire script is not working anymore at all. – Mofi Jun 14 '23 at 17:33

1 Answers1

1

gdalbuildvrt seems to be a batch file itself, therefore it has to be started with CALL.
Else the batch file ends and only the cached code block is executed, but not the remaining batch file.

jeb
  • 78,592
  • 17
  • 171
  • 225
  • I am not sure, gdalbuildvrt is run inside the loop. When the loop is done its a normal cmd command which should run (which does not run) dir /b /s *.tif > index.txt - Its never executed – geogrow Jun 14 '23 at 13:07
  • @geogrow - if you run a batch script from another batch script and you do not use `call`, then control moves to the child script *and then ends*, whereas if you do use `call`, control will be returned to the parent script once the child script ends. – SomethingDark Jun 14 '23 at 15:20