Run CMD commands from .bat files?

Hi,

I use the following cmd command to scale and crop some images in a folder with ImageMagick:

convert.exe *.* -geometry 512x512^ -gravity center -crop 1:1 -quality 100 -set filename:out %t_%wx%h %[filename:out].jpg

If I open up a cmd window, cd into the folder containing the convert.exe file and the images and then paste this command everything "just werks" and it spits out the cropped images.

But if I paste the command into a text file, rename it to run.cmd and run it, nothing happens. It just opens up a cmd window and close it again.

How can I get it to work?

Extra points if there is a universal method that works for all cmd commands, i.e. something that opens up a cmd window in the same folder as the bat file is stored (what ever the path name is) and then run the command as if I had pasted it in a cmd window my self. Is something like that even possible?

Pic related, my set up.

Attached: cmd.png (1045x475, 112K)

Other urls found in this thread:

robvanderwoude.com/escapechars.php
imagemagick.org/script/download.php
autohotkey.com/docs/commands/LoopFile.htm
autohotkey.com/docs/commands/Run.htm
instagram.com/crypto.otpyrc
dostips.com/
ss64.com/nt/
commandwindows.com/
twitter.com/SFWRedditVideos

in a folding*

Try run.bat just for the hell of it
If that doesn't work, try the full folder path to convert.exe

kek

Just for reference, opening a cmd window inside the folder and pasting the command works. Pic related.

I'll try the full folder path to convert.exe but I don't think it will work as I've tried it before.

Attached: thisworks.png (1085x535, 191K)

But did you try naming it .bat instead of .cmd ?

Just use linux.

>But did you try naming it .bat instead of .cmd ?

Renaming it to bat cause this error

Attached: error.png (639x38, 3K)

I mean this file

Attached: ss (2018-09-04 at 22.54.56).png (107x181, 13K)

Yeah, that's the one. I've renamed it "run.bat" and now it gives me this error

Weird AF

Attached: run.png (137x128, 12K)

>Just use linux.

Attached: linux.png (894x894, 612K)

poor winfags not using bash

Attached: mscall.jpg (535x350, 20K)

Okay I found out about the problem
I'll show you how: Check the screenshot
The faded one is the cmd window of the .cmd file that quickly dissappears
the below one is the code pasted into cmd manually.
You'll notice there are a few differences, highlighted in yellow font by me
The ^ is missing, and so is a lot of the stuff near the end.
The reason is that something enclosed in % signs is treated s a variable name by .cmd and .bat files
On this page you can read how to escape those characters, it works afterwards as I have tested it. robvanderwoude.com/escapechars.php

Attached: ss (2018-09-04 at 23.01.30).png (666x152, 64K)

Look how you much you are struggling with running a single command on windows. Fucking brainlet.

My man! That worked like a charm!

It's always a small little thing like that that turns out to be a major pain. You've made my day and I learned something, you're a real bro.

Attached: 1535057551248.gif (256x192, 1.48M)

Microsoft did a great job making people have nightmares about using the command line with cmd

Attached: 1524476452374.png (271x288, 42K)

>Look how you much you are struggling with running a single command on windows. Fucking brainlet.

I have to use the brain I'm born with, luckily I can always count on the hive mind.

Attached: 1535834965958.jpg (219x187, 7K)

No problem, I've had similar problems while trying to automate convert.exe in a project. There's always something with characters that have to be escaped.

in batch files you need to convert % to %%

While you're here, do you know if it's possible to put the imagemagick install directory inside the folder I use for this project and then have convert.exe use that instead of the installed version in C:\Program Files\ImageMagick-7.0.8-Q16?

If not I have to install ImageMagick on every machine I want to run this on, but a fully self referenced portable tool would be much more practical.

yes, just use relative path to the exe

You only need convert.exe in the same folder as the .cmd file

If I rename the ImageMagick-7.0.8-Q16? folder in C:\Program Files\ I get a "CORE_RL_MagickCore_.dll is missing error-

I don't have it installed
Did you get the portable version?
imagemagick.org/script/download.php

Attached: ss (2018-09-04 at 23.22.46).png (911x259, 22K)

Oh there is a portable version..

I'll try it right away!

That worked like a charm!

I can't thank you enough. Please, take this beautiful image of Costanza as a token of my gratitude.

Attached: Costanza.jpg (473x316, 87K)

Kek, thanks, glad I could help
Good luck with your project

this

if you do anything beyond normie GUI programs on windows, you are running the wrong OS and it will be painful as fuck

>using CLI on windows
found the problem, installing linux will fix this.

Hey! Are you still around?

The command only writes the result to disk after having processed all the images, which cause it to run out of ram on too large conversion jobs. Are there any way to force it to flush the result to disk after x amount of images converted for instance or something along those lines to keep ram and temp space usage low?

% signs need to be escaped in batch so instead of
%blah do %%blah
In command prompt however these are automatically parsed and escaped for you which is why it seems weird

Personally I use an autohotkey script with the run command to make convert.exe do something
I could use the AHK script to index the files and then loop through them all and run the convert.exe on each.
That way it would probably be slower, but not run out of ram.

autohotkey.com/docs/commands/LoopFile.htm

autohotkey.com/docs/commands/Run.htm

Other than that, sorry, can't help you.
You can probably do it in a .bat file though

CLI filepaths are ALL either relative or absolute. If you did not put the .cmd/.exe/.bat into the same folder as your image or whatever files, you need to use ABSOLUTE filepaths.

What a painful thing to watch, install cygwin or change to Linux I dont know, anything but cmd

Hmm, thanks. I've been trying various stuff to get it to work and this:

@ECHO ON

mkdir output

FOR /F "DELIMS=" %%A IN ('DIR /A-D /S /B "jpg2\*.*"') DO Program\convert.exe -limit memory 0 -limit map 0 "%%~A" -geometry 512x512^^ -gravity center -crop 1:1 -quality 100 -set

filename:out %%t_%%wx%%h output\%%[filename:out].jpg

GOTO EOF

Seems to do the trick. Both RAM and disk use is minimal and it doesen't increase.

The only problem is that it's soooo slow. I have a million images to scale and I'll be dead by the time this finishes.

Imagine being a wincuck
>tfw I can not only run any fucking script I want, I can map it to a keybind, having it run at x intervals of time, have it run at startup, have it run when I jerk off etc etc.

install a real OS chris

If you think windows can't do all of these you're delusional.

sure, if your time is worth nothing

A few ideas:
convert 100 images at the same time, then start again and take the next 100.

A hacky way to do that would be a for loop (100 times) and copy the files into a temporary folder, then tell imagemagick to convert everything in the temporary folder, then delete the folder and repeat for the next 100
But I'm not a good programmer so that's probably a shit solution

Another idea is to run multiple instances of imagemagick at the same time, so it does multiple in parallel.

Google also states that imagemagick's "mogrify" file is good for millions of images, might want to look into that.
Gotta sleep now, good luck

When I changed to mogrify things seem much faster but now none of the commands work and it doesen't want to save anything.. Back to the drawing board I guess.

Thanks again, I'll head to bed as well. Gotta try again tomorrow. Sleep well user.

it can, but will take you 10x more time and nerves

i've made so many neat things with imagemagick. it's a great program to automate social media posting for example.

instagram.com/crypto.otpyrc

This was all done using imagemagick. And the posts were really popular, though instagram started to shadow ban my posts and I stopped the project because fuck that.

Tell me more about how you do it

dostips.com/
ss64.com/nt/
commandwindows.com/

You mention cd'ing to the folder but don't do that in your bat file. You still need to do that.

Maybe you are just bad at it