Processing
 

Windows Batch File for loop and line continuation

09/09/2016 17:11#1

Joined at: 5 months ago

Post: 1

Thank: 0

Thanked: 0

I need a script for filling in all possible combinations of 5 blocks in a 3 by 3 grid. So I figured I could use ImageMagick for the coloring and a txt file for input. The text file consists of one number per line, the number shows which block should be colored. 5 numbers form one possible combination.

I use a for loop to read my txt file line for line. A variable (called a) is incremented and when it reaches 5 it is reset to 0 and another variable (called n) gets incremented, which changes the name of the output file. Since I need to reload the original image each time a new combination starts I use another variable (name) to switch between the original image and the picture for every combination.

This is my first time writing a batch file. So I might have not fully understood how line continuation and variables work since my script produces no output.

setlocal ENABLEDELAYEDEXPANSION set a=0 set n=0 for /F %%G in (test.txt) do ^ set /A a=a+1 & ^ if !a!==5 set a=0  & set /A n=n+1 & ^ if !a!==0 set name=original.png else set name=output_!n!.png ^ if %%G == 1 magick convert -fill blue -draw "color 50, 50 floodfill" %name% output_!n!.png ^ if %%G == 2 magick convert -fill blue -draw "color 150, 50 floodfill" %name% output_!n!.png ^ if %%G == 3 magick convert -fill blue -draw "color 250, 50 floodfill" %name% output_!n!.png ^ if %%G == 4 magick convert -fill blue -draw "color 50, 150 floodfill" %name% output_!n!.png ^ if %%G == 5 magick convert -fill blue -draw "color 150, 150 floodfill" %name% output_!n!.png ^ if %%G == 6 magick convert -fill blue -draw "color 250, 150 floodfill" %name% output_!n!.png ^ if %%G == 7 magick convert -fill blue -draw "color 50, 250 floodfill" %name% output_!n!.png ^ if %%G == 8 magick convert -fill blue -draw "color 150, 250 floodfill" %name% output_!n!.png ^ if %%G == 9 magick convert -fill blue -draw "color 250, 250 floodfill" %name% output_!n!.png Pause 
09/09/2016 17:18Top#2

Compo

Member

Joined at: 5 months ago

Post: 1

Thank: 0

Thanked: 0

Here's your code reformatted only, (apart from the new first line):

@echo off setlocal ENABLEDELAYEDEXPANSION set a=0 set n=0 for /F %%G in (test.txt) do (     set /A a=a+1     if !a!==5 (set a=0)     set /A n=n+1     if !a!==0 (set name=original.png) else (set name=output_!n!.png)     if %%G==1 (magick convert -fill blue -draw "color 50, 50 floodfill" !name! output_!n!.png)     if %%G==2 (magick convert -fill blue -draw "color 150, 50 floodfill" !name! output_!n!.png)     if %%G==3 (magick convert -fill blue -draw "color 250, 50 floodfill" !name! output_!n!.png)     if %%G==4 (magick convert -fill blue -draw "color 50, 150 floodfill" !name! output_!n!.png)     if %%G==5 (magick convert -fill blue -draw "color 150, 150 floodfill" !name! output_!n!.png)     if %%G==6 (magick convert -fill blue -draw "color 250, 150 floodfill" !name! output_!n!.png)     if %%G==7 (magick convert -fill blue -draw "color 50, 250 floodfill" !name! output_!n!.png)     if %%G==8 (magick convert -fill blue -draw "color 150, 250 floodfill" !name! output_!n!.png)     if %%G==9 (magick convert -fill blue -draw "color 250, 250 floodfill" !name! output_!n!.png) ) Pause 

I have updated the code to fix a major issue with your name variables.

Similar articles

How to use Logstash with log4j in Windows 7 32-bit VM

5 months ago - Reply: 1 - Views: 136

Running webrick server in background for Windows

5 months ago - Reply: 0 - Views: 60

Task Scheduler doesn't start my exe for the next day

5 months ago - Reply: 0 - Views: 47

Unable to get Windows Updates in Windows 7 [on hold]

5 months ago - Reply: 0 - Views: 27

Connect Spark-Shell using Eclipse in Windows 7

5 months ago - Reply: 0 - Views: 28

Can't use custom windows themes

5 months ago - Reply: 1 - Views: 117

“npm install <module>” doesn't work

5 months ago - Reply: 0 - Views: 41

OpenGL 4.5 functions segfaulting

5 months ago - Reply: 0 - Views: 6

Executing a batch file during installation

6 months ago - Reply: 0 - Views: 3

vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re