Skip to content

Windows Batch script to trim unprocessed images based on processed ones? (auto-trim) #7233

Answered by snibgo
YutMarma asked this question in Help
Discussion options

You must be logged in to vote

Yeah, my fault, I got it a bit wrong. I had a nagging feeling something was wrong, but couldn't see what.

setlocal enabledelayedexpansion

for %F in (*.png) do (
  for /F "usebackq" %A in (`magick "%~nF" *YourDisgustingFilters* -format %@ info:`) do set MYTRIM=%A

  magick "%~nF" -crop !MYTRIM! +repage "someNewDirectory\%~nF.png"
)

Note the new first line and the change to !MYTRIM!.

The problem is that the Windows interpreter only translates %MYTRIM% once, so the value isn't updated in the loop. !MYTRIM! is re-translated every time in the loop, aka "delayed expansion".

Sorry. (Still no guarantees I've now got it right.)

Replies: 3 comments 7 replies

Comment options

You must be logged in to vote
4 replies
@YutMarma
Comment options

@fmw42
Comment options

@YutMarma
Comment options

@fmw42
Comment options

Comment options

You must be logged in to vote
1 reply
@YutMarma
Comment options

Comment options

You must be logged in to vote
2 replies
@YutMarma
Comment options

@YutMarma
Comment options

Answer selected by YutMarma
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
3 participants