neahan
neahan

Reputation: 87

how can I fix this batch script?

I am trying to write a batch script that combines several css files into one file. So far I have come up with this...

# Set start folders & files
set fn1=filename.css
set fn2=another-filename.css
set fn3=yet-another-filename.css

# get filename add to temp file inside comment syntax
echo /* %fn1% >>  tmp.css
echo. --------------------------------------------------------------- */ >>  tmp.css
echo. >>  tmp.css

# copy file contents
copy/b %fn1% + tmp.css

# repeat with other files...

echo /* %fn2% >>  tmp.css
echo. --------------------------------------------------------------- */ >>  tmp.css
echo. >>  tmp.css
copy/b %fn2% + tmp.css

...

rename tmp.css  combined-files.css
move combined-files.css \new-folder\combined-files.css

Problem is it produces the following

/* filename.css
--------------------------------- */
/* another-filename.css
--------------------------------- */
/* ... */

[styles from filename.css]
[styles from another-filename.css]
....

Where am I going wrong with this?

Thanks

p.s. my attempts to simplify the above using ms-dos FOR command are pretty naff as well.

set commentpt1=\*
set commentpt2=----------------------------------------- *\

FOR /F %%I IN ('DIR /s C:\[folder location]') DO echo %commentpt1% %%~nI 0x0A %commentpt2% 0x0A 0x0A >> temp.css copy/b %%I + tmp.css >> temp.css

Upvotes: 2

Views: 229

Answers (2)

dbenham
dbenham

Reputation: 130929

Your primary problem is you have reversed the order of the file names in your COPY /B command. A side effect of this problem is you are modifying your original files!

You probably don't want to see the output of your COPY /B command, so you could redirect to nul.

There is no need to rename your temp file before moving it.

In fact, why use a temp file at all? Why not write directly to your desired destination file?

I would put the blank line after the file contents, not before. I think it looks better.
It is safer to use echo( instead of echo..

But... there is a much simpler and cleaner method to do what you want.

@echo off
(
  for %%F in (
    "filename.css"
    "another-filename.css"
    "yet-another-filename.css"
  ) do (
    echo /* %%~F
    echo --------------------------------------------------------------- */
    type %%F
    echo(
  )
)>"\new-folder\combined-files.css"

If you want to combine all .css files in the folder, then it is even simpler:

@echo off
(
  for %%F in ( *.css ) do (
    echo /* %%~fF
    echo --------------------------------------------------------------- */
    type "%%~fF"
    echo(
  )
)>"\new-folder\combined-files.css"

The above processes the current directory, but you could include path info in the IN() clause.

Upvotes: 3

Romil Kumar Jain
Romil Kumar Jain

Reputation: 20775

For example, you can join file1.txt and file2.txt to a new file named file3.txt:

copy/b file1.css+file2.css file3.css

OR:

copy/b *.css newfilename.css

OR for all files in a folder:

copy/b * "newfilename_with_path"

Upvotes: 0

Related Questions