0

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.

john c. j.
  • 725
  • 5
  • 28
  • 81
  • is the backtick an AHK escape? If so, the count of `%` chars is different for your AHK wrt your batch. – Magoo Apr 19 '17 at 03:22
  • @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. Apr 19 '17 at 03:35
  • 2
    "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 Apr 19 '17 at 05:26
  • 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 Apr 19 '17 at 08:18
  • @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. Apr 19 '17 at 14:32
  • Why are you using `Join&` at the top of your block? Does the default standard linefeed character between each line not do what you need? – Compo Apr 19 '17 at 14:39
  • now it looks very much like a [delayed expansion](http://stackoverflow.com/a/30284028/2152082) problem. (your `for` loop is inside a code block). Doesn't it work without the parantheses around the "whole script"? – Stephan Apr 19 '17 at 17:38
  • @Stephan Hmm. With delayed expansion, the code still works correctly if launched with separate bat file and still doesn't work properly if launched as part of AutoHotkey script. Maybe it is some AutoHotkey bug. Very thanks for this link, very useful in any case. – john c. j. Apr 19 '17 at 18:10
  • You don't execute a batchfile, but a simple command line. Try `chcp 65001&set "ext=xyz"&for %i in (*) do @call echo %~ni.%ext%` and if that works, try to implement your `for` into it. – Stephan Apr 19 '17 at 19:14

1 Answers1

1

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`%"
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thanks. The second example I've already tried myself many times, see my comments to original post. It doesn't work -- the CMD window is just immediately closing. By the way, AHK works fine if I don't use variable for extension. I will update my post in a few minutes to show working AHK code. (But I *want* to use variable, so it just for demonstation purposes). – john c. j. Apr 19 '17 at 14:21
  • You haven't mentioned trying it at all, your examples did not escape the parentheses only the percents. Please try what I have used after making sure that the rest of your block is correct. – Compo Apr 19 '17 at 15:09
  • Oh, sorry that I don't mentioned it. Yes, I tried both your examples, i.e. with escaping both parentheses and single parenthes. It doesn't work - the window is just immediately closing. It's all very strange. For example, if I ditch the MultiMarkdown from code and use simple "ren" command, then, it will successfully work even with variable. – john c. j. Apr 19 '17 at 15:13
  • Is it necessary to `call` multimarkdown? have you tried removing the word `call`? What about simply adding `ECHO` just before `call` in each test and seeing if the correct command is output to the correct filename and extension? – Compo Apr 19 '17 at 15:43
  • `Echo` (actually `call echo`) works just fine everywhere (before, as well as after the `for`-line). `Call` before `multimarkdown` seems to be necessary (tested also with `ren` command, instead of multimarkdown), but in any case, I've tested it a lot with and without `call`. – john c. j. Apr 19 '17 at 15:46
  • My best guess is that `CALL` is the problem and it is effectively creating a new cmd instance which isn't passed the `%ext%` variable. _(Your bat_script isn't really a batch file but just a series of command lines because your single percents appear to be what works for you.)_ – Compo Apr 19 '17 at 15:48