Problems passing in files with spaces in name Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
omar
Posts: 462
Joined: 22 Oct 2015, 17:56

Problems passing in files with spaces in name

10 Jan 2021, 23:45

I've 95% written some code... but it's not quite working. Hoping to get some help...

This is my code:

Code: Select all

#NoEnv 
#SingleInstance force

for n, param in A_Args  ; For each parameter:
{
	SplitPath, param, name, dir, ext, name_no_ext, drive
	Run, %COMSPEC% /k convert %param% -resize 300 "%name_no_ext% optimised.jpg"
}
ExitApp ;<- Exit the app

Return
Explaining what I am trying to do...

I want to select many images and resize and optimise them.
imagemagick code needed to do this one file at a time:

convert input.jpg -resize 300 output.jpg
(Works on png files as well.)

My AHK code works OK when run with files that have no spaces.
With space, problem :(

If I run: mycode.ahk "file name1.jpg" "file name2.png"

It throws errors.

ALSO: I get DOS windows for every file I run on.
How do I stop that popping up?

I also wanted to do:

- I'd like to output to a new folder that is created by AHK, called 'Output'.
- If the folder exists already, delete the file contents first (no need for prompting asking for permission needed).

It's 4.35am and I'm just not thinking straight! :shock:

Any help would be great. Thanks.
Rohwedder
Posts: 3978
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Problems passing in files with spaces in name

11 Jan 2021, 02:28

Hallo,
https://imagemagick.org/script/command-line-processing.php
If the image path includes one or more spaces, enclose the path in quotes:
'my title.jpg'
User avatar
Xeo786
Posts: 363
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: Problems passing in files with spaces in name

11 Jan 2021, 05:13

try this

Code: Select all

Run, %COMSPEC% /k ""convert" "%param%" "-resize" "300" "%name_no_ext%" "optimised.jpg""
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
BoBo
Posts: 4362
Joined: 13 May 2014, 17:15

Re: Problems passing in files with spaces in name

11 Jan 2021, 05:33

Code: Select all

Run, % comspec " /k convert """ . param . """ -resize 300 " . name_no_ext . " optimised.jpg" ; using double-quotes 

Code: Select all

Run, % comspec " /k convert '" . param . "' -resize 300 " . name_no_ext . " optimised.jpg"   ; using single-quotes
omar
Posts: 462
Joined: 22 Oct 2015, 17:56

Re: Problems passing in files with spaces in name

11 Jan 2021, 22:42

None of these work guys
I thought it would be such a simple thing to do?

Any other suggestion?
Thanks
BoBo
Posts: 4362
Joined: 13 May 2014, 17:15

Re: Problems passing in files with spaces in name

12 Jan 2021, 08:10

Replace Run, % comspec ... with MsgBox, % comspec ... to check if the shown outcome matches with what you would enter into the cmd-shell manually.
omar
Posts: 462
Joined: 22 Oct 2015, 17:56

Re: Problems passing in files with spaces in name  Topic is solved

12 Jan 2021, 23:36

@BoBo Thanks
I did that and found the bug 🙂

Code: Select all

Run, % comspec " /k convert """ . param . """ -resize 300 " . name_no_ext . " optimised.jpg" ; using double-quotes 
Say I ran for the file 'abc def ghi.jpg'

This was giving me at the end: abc def ghi optimised.jpg
This should be: "abc def ghi optimised.jpg"

I searched and found to get a single quote, you have to put 4 quotes: """"

Now working fine.

Thanks for all the help guys. 👍🏼
BoBo
Posts: 4362
Joined: 13 May 2014, 17:15

Re: Problems passing in files with spaces in name

13 Jan 2021, 04:35

I searched and found to get a single quote, you have to put 4 quotes: """"
Well, sort of ...

MsgBox "Showing quotes"
MsgBox ""Showing doubled quotes""
MsgBox % "Not showing quotes"
MsgBox % " ""Showing quotes"" " ; AKA, for showing a single quote within an (forced) expressions string definition you’ve to double it.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], mikeyww, Nixcalo, roysubs, XMCQCX and 54 guests