0
votes

Despite of this question covers Windows CMD, AutoHotkey and MultiMarkdown, I believe, the problem is closely related to CMD (my lack of knowledge of Windows bat files).

So...

I'm trying to create AHK-script for MultiMarkdown, which will allow to convert mmd files to any extension (not particulary html).

This is how I could do it with plain bat file:

chcp 65001
set ext=mmd2html
for %%i in (*.md) do call multimarkdown --escaped-line-breaks --process-html --nosmart "%%i" > %%~ni.%ext%

This works. If you place this bat file together with your mmd-files, it will properly convert and rename them.

However, when I'm trying to place this code into AHK script, it fails. Here is what I have:

#SingleInstance, Force

f1::

bat_script =
(join&
chcp 65001
set ext=mmd2html
for `%i in (*.md) do call multimarkdown --escaped-line-breaks --process-html --nosmart "`%i" > `%~ni.`%ext`%
)

Run, %ComSpec% /c %bat_script%, %A_ScriptDir%
return

How it can be solved?


Post updated

The actual problem is that instead of renaming files like this:

my_test_file.md --> my_test_file.mmd2html (yes, "mmd2html" is an extension)

it renames literally:

my_test_file.md --> my_test_file.%ext%

In other words, the script doesn't understand that %ext% is variable. Here is working AHK code, without using var:

f1::

bat_script =
(join&
chcp 65001
for `%i in (*.md) do call multimarkdown --escaped-line-breaks --process-html --nosmart "`%i" > `%~ni.aaaaaaaa
)

Run, %ComSpec% /c %bat_script%, %A_ScriptDir%
return

However, I want to use variable for file extension, so this working code posted here just for demonstration purposes.

1
is the backtick an AHK escape? If so, the count of % chars is different for your AHK wrt your batch. - Magoo
@Magoo Yes, backticks are AHK escape characters. As I know, we should use %% if bat-script launched from bat-file, and we should use single % if the same code pasted directly into cmd window. This was the reason why I reduced them. However, I've also tried for %%i in (*.md) do call multimarkdown --escaped-line-breaks --process-html --nosmart "%%i" > %%~ni.%ext%. It will immediately close the cmd window, without any effect. - john c. j.
"it fails" is not very concrete. But are you sure, your AHK script knows, which directory to process? Try for %i in ("c:\full path\*.md") do ... or cd /d "c:\full path" & for %I in (*.md) do... (I'll leave proper escaping to you, 'cause I don't speak AHK) - Stephan
Or, echoing Stephan's concern, if %A_ScriptDir% specifies the "current directory" that the command runs from, make sure that points to where the .md files are (I also do not know AHK). - TripeHound
@Stephan Yes, I sure, see my previous comment. "It fails" mean that instead of renaiming file my_test_file.md to my_test_file.mmd2html, it renames it my_test_file.%ext%. Literally. For some reason, it doesn't understand that %ext% is variable. I've updated post. - john c. j.

1 Answers

1
votes

You need to escape the internal parentheses.

for `%i in `(*.md`) do multimarkdown --escaped-line-breaks --process-html --nosmart "`%i">"`%~ni.`%ext`%"

Although untested, escaping the internal opening parentheses may not be required, just the closing ones ensuring your block remains as one. You could try it as an alternative and report back.

for `%i in (*.md`) do multimarkdown --escaped-line-breaks --process-html --nosmart "`%~i">"`%~ni.`%ext`%"