2013-11-26 57 views
3

我有一个脚本,使用YUICompressor压缩所有CSS和JS,然后再制作网站的生产版本。我得到它的工作,但我想通过只压缩那些被修改的文件来加速它。批处理文件检查文件修改日期是否比输出新

换句话说,如果输出文件比输入文件新,它应该跳过。

@echo off 
set batch_path=%~p0 
setlocal enabledelayedexpansion 

call:compress css styles\core\ ..\core\resources\styles\ 
call:compress js scripts\core\ ..\core\resources\scripts\ 

pause 
goto:eof 


:compress 
for /r .\%~2 %%f in (*) do (
    set filename=%%f 

    rem set output filename to correct directory and add .min before extension 
    set output_filename=%%~pf 
    set output_filename=!output_filename:%batch_path%%~2=! 
    set output_filename=%~3!output_filename!%%~nf.min%%~xf 

    echo. !filename! =^> !output_filename! 

    rem --- check file modify times --- 

    rem change \ into \\ for yuicompressor 
    set filename=!filename:\=\\! 
    set output_filename=!output_filename:\=\\! 

    java -jar yuicompressor-2.4.8.jar --type %~1 -o "!output_filename!" "!filename!" 
) 
goto:eof 

我发现只有复杂的解决方案,我将如何比较文件修改时间,或者继续或跳过?

回答

3

Comparing a modified file date with the current date in a batch file

for %%f in (%filename%) do set filedt=%%~tf 
for %%f in (!output_filename!) do set outfiledt=%%~tf 
if %filedt:~0, 10% LSS %outfiledt:~0, 10% DO SOMETHING 

如果文件名比outfilename旧的 - 做一些事情。

+0

Windows以本地格式写日期,对我来说这是''''''''''''''''''''''''时间'。我预计这个脚本在月或年的交替时刻不起作用。 – Melebius

相关问题