2

I do web development and I'm using a new module system. Whenever I create a new module, I have to recreate a folder and file structure. I have written a batch file to do this that works pretty well, however I would also like to modify the file contents based on the name of the module. So, for example, I would like to be able to execute a command like:

createTemplate my-template new-module

and have it create the following file/folder structure:

 /new-module
     /new-module.html
     /new-module.css

It would copy new-module.html from /my-template/my-template.html, which may also include variables like ${NAME} which would be replaced with "new-module" during creation.

For instance, my-template.html may look something like this:

<html>
  <body>
    <div>This is the <b>${NAME}</b> template</div>
  </body>
</html>

Which when applied as a template and viewed the browser would simply look like the following:

This is the new-module template

1

From the "forward slashes" used in your examples, I assume you're looking for a Linux/Unix solution. The software ships with the OS there, and is called "shell" :) Following one-liner does this job in Bash:

alias createTemplate="mkdir /${2}; cp /${1}/my-module.html /${2}/${2}.html; cp /${1}/my-module.css /${2}/${2}.css"

Of course, you could also put it into a shell script you name createTemplate, and make it a little easier to read:

#!/bin/bash
# Initializing new module
template="$1"  # first parameter defines the template
module="$2"    # second parameter is the name of the new module

mkdir /${module}
cp /${template}/my-module.html /${module}/${module}.html
cp /${template/my-module.css /${module}/${module}.css

echo "Directory for module ${module} initialized."

Enhance with more variables as needed :) For help on details with bash scripting, a good place to check out is our sister site Super User.

  • Crap I forgot to say OS, sorry. It's windows. I have actually written a batch file that does basically what you suggested. I'll post when I get home. – Devil's Advocate Jul 26 '14 at 21:10
  • On Windows, PowerShell should be capable of doing similar things. But I can't tell for sure. That batch stuff on windows always drove me crazy ;) And yeah, guessing was hard: forward-slashes indicated Linux, as did ${NAME} (looks like Bash syntax). Just the term "batch file" was a bit "out of sync" there ;) – Izzy Jul 26 '14 at 21:14
0

Here is what I came up with: https://github.com/ScottBeeson/polyclone

It was originally Polymer specific, but it's not anymore. I should rename the repo. Here is the full code:

@echo off &setlocal
set /p seed= Enter Template Folder:
set /p target= Enter New Name
@echo Creating new element named "%target%"
echo Creating Folder...
md %target%

for %%i in (%seed%\*.*) do (
    set "filename=%%~ni"
    set ex=%%~xi

    if %seed%==%%~ni (
        call:WriteFile %seed%\%%~ni%%~xi %target%\%target%%%~xi
    ) else (
        call:WriteFile %seed%\%%~ni%%~xi %target%\%%~ni%%~xi
    )
)


GOTO:EOF

:WriteFile
echo Writing %~1 to %~2
set "search=${NAME}"
set "replace=%target%"
set "textfile=%~1"
set "newfile=%~2"


(for /f "delims=" %%i in ('findstr /n "^" "%textfile%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(!line!
    endlocal
))>"%newfile%"

GOTO:EOF

:EOF
echo Done!

Currently it only supports one variable to replace, I would like to expand this to read from some sort of input file where you could map multiple variables to strings for replacement.

Right now, all you do is run "polyclone.bat" from a command prompt and it will prompt you for the template folder and the target folder. It will then process the template and write every file replacing any instance of ${NAME} with the target name provided.

Also no subfolder support yet. But it does exactly what I need for my current situation.

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.