This is a slightly different question from Populating Array in DOS Batch Script

I'm trying to compile a JavaScript application using Google Closure Compiler. The list of JavaScript files has grown to 30+ files, and I'm looking for a way to manage it. What I've settled on is to push the files to an array using something like the following:

set projectDrive=E:
set js_folder=\project\trunk\htdocs\script
set deploy_folder=\project\trunk\htdocs\bin
set closure_compiler=java -jar Z:\utils\compiler.jar
set  arrayline[0]=\script\com\appstudio\utils\Shim.js
set  arrayline[1]=\script\com\jquery\chosen.jquery.min.js
set  arrayline[2]=\script\com\jquery\jquery-cookie.js
set  arrayline[3]=\script\com\jquery\jquery.qtip.js
set  arrayline[4]=\script\com\jquery\jquery.zclip.min.js
set  arrayline[5]=\script\com\swfobject\swfobject.js
set  arrayline[31]=\script\com\lastfolder\lastFile.js

According to the post mentioned above, I know I can loop through the array like this:

for /l %%n in (0,1,12) do (
    echo !arrayline[%%n]!

However, this doesn't give me the output I need, as each echo is on a new line. I need a concatenated string for Closure Compiler that will result in something like this:

%closureCompiler% --js "%deployFolder%\arrayline[0].js" --js "%deployFolder%\arrayline[1].js" --js_output_file "%deployFolder%\script.js"

Where I can execute Closure Compiler against a concatenated list of array elements in the format of --js "%deployFolder%\arrayline[x].js" Is this possible?

EDIT: I didn't mention originally that I'm using an array because this is a quick and dirty proof of concept for a continuous integration environment. I'm going to be spitting these files out in different ways in different contexts, but the Closure Compiler output is the first step in that process. I'm stuck with a Windows box for now and my IDE doesn't do what I want it to, so I'm working with batch files, just the choice I made for this stage of the project.


Here is a post that may answer your question.

|improve this answer|||||
  • 1
    Good find, thanks +1. Glad I don't need anything Turing complete! Not going to mark this as an answer in its present form since it's technically a reference to a list of techniques, but very helpful all the same. – Shane Feb 28 '14 at 12:47
  • 2
    Link only answers are not encouraged here. Summarize what you find, and provide a link for attribution – Canadian Luke Feb 28 '14 at 17:51
  • Wasn't really sure of the protocol since it's a link within the same site network. Thanks for the info! – Bradley Forney Mar 1 '14 at 19:10

To clarify the goal, it's to create an array of length n containing a list of files to be compiled via google closure compiler. Here's the quick solution with a bit of abstraction:

@echo off
set projectDrive=E:
set js_folder=\myproject\trunk\htdocs\script
set deploy_folder=\myproject\trunk\deploy\script
::EnableDelayedExpansion is necessary to work with the array elements
::see link from Bradley Forney
setlocal EnableDelayedExpansion
REM start the command string with a reference to closure compiler
set command=java -jar E:\utils\compiler.jar

REM create whatever array elements you wish here
set  __compile_array[0]=%js_folder%\com\mycompany\utils\Shim.js
set  __compile_array[1]=%js_folder%\com\jquery\chosen.jquery.min.js
set  __compile_array[2]=%js_folder%\com\jquery\jquery-cookie.js

::Get the array length
FOR /F "tokens=2* delims=[=]" %%a IN ('SET __compile_array') DO set /a length=length+1
::Adjust length to match zero based array index
set /a length=length-1

REM loop through array elements to create the command    
for /l %%n in (0,1,%length%) do (set command=!command! --js !__compile_array[%%n]!)

write the command to the prompt with the addition of the output file
%command% --js_output_file "%deploy_folder%\main.js"
|improve this answer|||||
  • not yet, only have a 127 rep on this site. Have to wait 2 days :( – Shane Feb 28 '14 at 15:58

Instead of building an array when what you want is a string, build the string:

set deploy_folder=\project\trunk\htdocs\bin
set closure_compiler=java -jar Z:\utils\compiler.jar

set  jsParams=--js %deploy_folder%\script\com\appstudio\utils\Shim.js
set  jsParams=%jsParams% --js %deploy_folder%\script\com\jquery\chosen.jquery.min.js
set  jsParams=%jsParams% --js %deploy_folder%\script\com\jquery\jquery-cookie.js
set  jsParams=%jsParams% --js %deploy_folder%\script\com\jquery\jquery.qtip.js
set  jsParams=%jsParams% --js %deploy_folder%\script\com\jquery\jquery.zclip.min.js
set  jsParams=%jsParams% --js %deploy_folder%\script\com\swfobject\swfobject.js

%closure_compiler% %jsParams%

You might have reasons for wanting to stick with the array; however, just appending a string would achieve your goal of building a command line to execute.

|improve this answer|||||
  • The array is for maintenance and re-purposing I wasn't clear about the motive in the original question. String building technique is definitely simpler though. – Shane Feb 28 '14 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.