3

I'm new to batch, but I figured out it would be a good way to compile the .less files for my website into .css before deploying it.

Unfortunately, after I use a for loop in a batch file to find all of my .less files and compile them into their .css equivalents, my script stops execution even though I have more commands after the for loop.

My batch script contains the following:

@echo off
rmdir c:\code\releaseDirectory /s /q
echo d | xcopy /d c:\code\devDirectory c:\code\releaseDirectory /s
cd c:\code\releaseDirectory
for /r %%i in (*.less) do echo %%i
for /r %%i in (*.less) do lessc -x %%i > %%~pi%%~ni.css
echo reached the end of the batch script!

When I run this, the output shows all of the .less compiling happening, but the "reached the end of the batch script!" string never get displayed and thus, and any actions I want to take after the .less compilation never happen.

Why isn't any script after the less compilation in the for loop being executed?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
James J
  • 53
  • 1
  • 3
  • Related: *[Why does only the first line of this Windows batch file execute but all three lines execute in a command shell?](https://stackoverflow.com/questions/4036754)* – Peter Mortensen Nov 01 '18 at 16:23

3 Answers3

4

I had this same issue and had to change lessc to call lessc and then my script stopped ending prematurely. Here is a SO link that lead me to try it: Why does calling a nested batch file without prepending "call" to the line exit the parent batch file? Basically it says that you need to use call when executing nested batch files so that control will be returned to the parent file when the sub-file is finished executing.

Community
  • 1
  • 1
bygrace
  • 5,868
  • 1
  • 31
  • 58
3

Is there a lessc.bat in your path?

If so, you need to CALL lessc...


Is there an "odd" filename? being processed?

Perhaps ...do ECHO %%i& lessc -x %%i > %%~pi%%~ni.css

may reveal more...

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Sorry, no, lessc is the .Less compiler used to compile the .less files into .css files. http://lesscss.org/#usage – James J Mar 25 '13 at 00:22
  • After adding in the extra echo and redirecting the batch script to a .txt file, the output for each line of the for-loop looked like `c:\Code\releaseDirectory>echo c:\Code\releaseDirectory\styles\common.less & lessc -x c:\Code\releaseDirectory\styles\common.less 1>\Code\releaseDirectory\styles\common.css c:\Code\releaseDirectory\styles\common.less` except for the very first iteration of the for loop, which only output `c:\Code\releaseDirectory\styles\about.less`. Would the first iteration only showing that indicate an error of some sort? – James J Mar 25 '13 at 00:55
  • More than likely. I'd suggest you try changing to cd c:\code\releaseDirectory and try `lessc -x c:\Code\releaseDirectory\styles\about.less > \Code\releaseDirectory\styles\about.css` from the prompt and see what happens. Maybe actually remove the `>...` part and just let the output appear on the screen. **BTW** `%%~pni.css` will work just as well as `%%~pi%%~ni` – Magoo Mar 25 '13 at 01:18
  • The output appears as expected, so it doesn't appear as though there's anything wrong with the about.less file. Any other ideas what it could be? – James J Mar 25 '13 at 01:28
1

Try this:

...
for /r %%i in (*.less) do lessc.exe -x "%%~i" "%%~dpni.css"
...
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • Think it is normally "lessc.cmd" not "lessc.exe" in windows. However, for /r %%i in (*.less) do lessc -x "%%~i" "%%~dpni.css" will work. – Stephen Simpson Feb 21 '14 at 12:03