.less compilation in batch for-loop ends batch script prematurely - css

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?

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.

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...

Try this:
...
for /r %%i in (*.less) do lessc.exe -x "%%~i" "%%~dpni.css"
...

Related

Running a Sqlite batch file to read all .txt files in the same directory

Problem is i need to run a batch file that will run all file with .txt extension one at a time, but right now i can only run 1 file manually..
sqlite3 db1.db ".read 1.txt"
saved as a .bat file
what i would like,is for it to read all txt files in the directory because it only works for the above named txt file
Updating answer. Try concatenate your text files, then do the command.
copy /b *.txt newfile.txt
sqlite3 db1.db ".read newfile.txt"
---You said that may not work because files are too large then, try this as an alternative?
:start
setlocal EnableDelayedExpansion
for /f "tokens=* delims=" %%a in ('dir/b "%~dp0\*.txt"') do (
set file=%%a
echo "!file!"
call :readfile !file!
)
ENDLOCAL
:readfile
sqlite3 db1.db ".read %file%"
pause
:exit
goto :EOF

How to change the working directory when running R scripts with Rscript.exe in Windows

I want to execute an R script myscript.R by using Rscript.exe on a Windows machine. However, my default home folder is configured to be \\\\<domain>/home/m/myname/Documents. I've made a wrapper DOS batch script (run.bat) on the same folder where myscript.Rresides (actually a folder in a pendrive) in order to make it the current working directory:
#ECHO OFF
REM This batch file is an interface to run R scripts from the command line
SET RSCRIPT="c:\Program Files\R\R-3.4.3\bin\Rscript.exe"
echo "Changing drive and directory to %~dp0"
pushd "%~dp0"
REM we change "\" to "/"
setlocal enabledelayedexpansion
set f=%*
set "f=!f:\=/!"
%RSCRIPT% %f%
popd
I call the script by run.bat "myscript.R" --dir ../AnotherFolder
So far, so good. However, the problem is that the script needs to change the working folder to a folder one level up (../AnotherFolder) but the setwd() R function fails. By debugging, I can see that the problem is that Rscript.exe loads the script myscript.R while still having the default directory setup to the home folder.
The question is: how the heck I can make Rscript.exe to ignore the default home directory and take the script directory as the current working directory.
Ok, I've found my own answer after scratching my head the whole day. I've modified the wrapper batch file and add an extra command line argument --drive to be processed by my R script:
#ECHO OFF
REM This batch file is an interface to run R scripts from the command line
SET RSCRIPT="c:\Program Files\R\R-3.4.3\bin\Rscript.exe"
echo "Changing drive and directory to %~dp0"
pushd "%~dp0"
REM we change "\" to "/"
setlocal enabledelayedexpansion
set f=%*
set "f=!f:\=/!"
set P=%~dpnx1
%RSCRIPT% %f% --drive "%P:~0,2%"
popd
Then, my R script must execute setwd(drive) where drive contains the value passed through the --drive option (e.g. "F:") before executing a setwd() on a relative path to the current folder.

Compile multiple files using Handlebars

I have a templates folder and I have multiple files under it.
I want to compile all these templates into a single file so that I can load it using requireJS.
I know that, I can do this via build tools like Grunt, Gulp, Brunch etc
What I am specifically looking for is how can I do this via command line using the handlebars compiler.
I installed the compiler via node
npm install -g handlebars
But, I am able to compile only 1 file at a time.
handlebars --amd templates/single-template.hbs -f compiled.js
[I am using windows OS]
It's as easy as:
handlebars path/to/template/folder -f path/to/compiled/folder/all-templates-compiled.tpl.js
found here https://github.com/wycats/handlebars.js/issues/131
#echo off
cls
echo -------------------------------
:choice
set /P c=Precompile every .html in this folder?[Y/N]?
if /I "%c%" EQU "Y" goto :compile
if /I "%c%" EQU "N" goto :end
goto :choice
:compile
for %%f in (*.html) do (
echo %%~nf
handlebars "%%~nf.html" -f "%%~nf.tmpl" -m
)
:end
echo End of script
pause
exit

Run uic for all .ui files in directory using windows batch

I can get a list of Qt .ui files like so:
D:\programing\qtproject\ui\designer>dir *.ui /B
main.ui
mainmenu.ui
mainwindow.ui
And what I do right now is that I manually run uic for every one of them, because IDE I'm using (QtCreator, ironically the one IDE that should be perfectly compatible with Qt) is not capable of that.
So can I run uic with a list of files obtained from dir *.ui /B? I would prefer if this worked recursively on all subdirectories.
Ok, I cracked it using some helpful answers:
#echo off
rem Runs uic command over all files in all subdirectories
rem For loop arguments explained here: http://stackoverflow.com/a/3433012/607407
rem start command arguments explained here: http://stackoverflow.com/a/154090/607407
rem /B is why the start creates no additional windows: http://stackoverflow.com/a/324549/607407
for /f %%F in ('dir *.ui /S /B') do start "" /B "uic" %%F -o %%~dpFui_%%~nF.h
To disable the recursion (subdirectories), remove the /S parameter in the dir command.

Makefile run an action before everything

For building my target I have a list of prerequisites contained in a file list.txt and a script for generating this file generate-list.sh.
I need the script to be executed as first thing every time I invoke the make in order to have the list.txt updated and to give ti make the right list of prerequisites.
prebuild:
touch list.txt
.SECONDEXPANSION:
exe: prebuild $$(shell cat list.txt)
touch exe
<files in list.txt>:
<rules for generating these files>
In this way when I run make I first get an error from cat saying that list.txt does not exist, then list.txt is generated but since the cat failed the prerequisites contained in list.txt are not generated.
One method you could use, given that generate_list.sh must be executed at the very start every time, would be to explicitly execute it using the shell function. This would mean altering your makefile to something like
$(shell ./generate_list.sh > /dev/null)
.SECONDEXPANSION:
exe: $(shell cat list.txt)
touch exe
#echo $?
<files in list.txt>:
<rules for generating these files>
Executing this makefile produces
$ make
touch exe
deps.c test.c
where my generate_list.sh file contains
#!/bin/bash
touch test.c deps.c
echo deps.c test.c > list.txt
echo 'Created prerequisites list.'
Notes
/dev/null is included in $(shell ./generate_list.sh > /dev/null) incase your generate_list.sh produces an output as this would cause an error in make of
$ make
GNUmakefile:1: *** missing separator. Stop.
otherwise.
#echo $? shows that all of the prerequisites in list.txt are now included as prerequisites of exe.
Alternate Method Based on Auto Dependency Generation
What you are attempting to do is very similar to automatic dependency generation which can be accomplished using the -include directive in make. For future usage you may want to consider going down this route and altering your generate_list.sh script to create a makefile that can be included in your main makefile.

Resources