1

I just created the following batch file which saves all my lyx documents as tex files:

cd /d "D:\"
:: if the "for"-command is executed from the command line, just use "%" rather than "%%"
for /R %%g in (*.lyx) do "C:\Program Files (x86)\LyX 2.1\bin\lyx.exe" --force-overwrite --export pdflatex "%%g"

The problem is now that instead of the *.lyx files the batch uses the *.lyx~ files which are as far as I know some kind of backup files and don't contain the latest content.

How can I modify the batch file such that it takes the *lyx files rather than the *.lyx~ files?

Any suggestion would be great help.

Best :-)

PS: If I type this code in the command line (without using the batch), everything is fine.

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
Fabian
  • 103
  • 6
  • Are you saying that it processes the _.lyx~_ files __only__ (leaving the _.lyx_ files out)? On my machine(W7 x64) it processes all of them (from batch __and__ cmdline). – CristiFati Jul 24 '15 at 16:42
  • Right, the resulting tex file contains not what is in the *.lyx files but what is stored in the *.lyx~ files .... – Fabian Jul 24 '15 at 17:11
  • For replication: I create a lyx file with, say the conten "old", then create the tex file with the batch, then change the lyx content to "new" and run the batch again. The tex file is not updated and still contains "old", just as is contained by the lyx~ file ... – Fabian Jul 24 '15 at 17:18
  • Does it work when you just double-quote the set parameter of `for`, like `for /R %%g in ("*.lyx") do `? (maybe then it might be required to use `"%%~g"` instead of `"%%g"` in the body of `for`) – aschipfl Jul 25 '15 at 17:47

1 Answers1

0

@Edit1: added tokens option. Now it works in subdirs with spaces.

Modify your for loop to:

for /F "tokens=*" %%g in ('dir /b /s *.lyx') do if "%%~xg" equ ".lyx" (
    "C:\Program Files (x86)\LyX 2.1\bin\lyx.exe" --force-overwrite --export pdflatex "%%g"
)

Another solution would be to use forfiles.

As for the 3rd comment, I think it does both but it does the .lyx~ at the end.

To see the output of a batch file simply launch it from a cmd window.

CristiFati
  • 38,250
  • 9
  • 50
  • 87
  • I changed the for loop according to your post. This solves the problem! Thank you very much. There is just one thing, now, subfolders are not included anymore. Is there an easy solution to also include subfolders? – Fabian Jul 24 '15 at 17:53
  • Shall I do it even though the part with the subfolders is still open? – Fabian Jul 24 '15 at 18:15
  • Sorry, i didn pay attention to the whole answer :). How can that be? That's what `/s` flag passed to `dir`does (should do). What's the _Windows_ version you use? There are some weird things going on. What would `dir /b /s *.lyx` output? – CristiFati Jul 24 '15 at 18:29
  • So do I. I've even tried it on a XP and it works fine. Hmm, what happens if instead of converting the files you would simply echo their names: `echo "%%g"`? Where does `lyx.exe` generate the output file? Right next to the source? – CristiFati Jul 24 '15 at 19:01
  • Next to the source, correct! Can't see what happens using echo, the window closes again immediately ... – Fabian Jul 24 '15 at 19:04
  • I just found out what causes the problem! If there is a space in the folder name, then it does not work! Unfortunately there are many spaces in my folder names ... – Fabian Jul 24 '15 at 19:07