[英]Need info on how to create a Batch file to edit multiple html files
我的文件結構和過程如下: http : //i.imgur.com/R7P7HQI.jpg
我有一個名為“ Project_ID”的文件夾。
在該文件夾中,我有html文件,另一個文件夾包含名為01.jpg,02.jpg,03.jpg等的設計模板。
在這些html文件中,我會預覽每張照片,例如01.html,02.html,03.html等。
如果單擊並打開01.html,則會看到第一個圖像01.jpg。 如果單擊該圖像,它將把您發送到打開02.jpg的文件“ 02.html”的相同文件夾,依此類推,直到到達再次打開01.html的最后一個html。
--------------------------------------…
我要使用批處理文件“自動化”的東西是:
每當我有一個新項目時,我都必須使用記事本進入每個.html文件,在Profile中更改“ Profile”的值,並使用我的初始文件夾的確切名稱(Project_ID)。
然后,我必須用images文件夾中01.jpg的確切尺寸替換寬度(在示例1920中)和高度(在示例2394中)。
當然,我想讓我的批處理通過首先查看“初始文件夾的名稱(Project_ID)”,“圖像文件夾中我的.jpg文件的數量”和用記事本從頭開始創建所有這些.html文件。然后創建我在開始時說明的功能所需的確切數量的html文件。 我想僅使用批處理文件將有些困難。
有人可以幫忙嗎?
那是一件很痛苦的事。 考慮其他方法:
display.php?project=MyProject&img=1
如果您要創建相冊,則強烈建議您看一下jAlbum 。 只需單擊幾下即可生成您的網絡相冊,其中包含大量社區開發的模板 ,您甚至可以使用它生成幻燈片。 如果要防止右鍵單擊保存圖像是您的主要目標,那么我認為有些模板可以使用Flash Player顯示圖像。
查看一些樣本專輯 ,看看您的想法。
如果您堅持使用自己的模板,請使用GnuWin32 sed查看內聯流編輯。 您可以使用正則表達式在所有html頁面中更改項目,如下所示:
sed -i -r -e "s/Profile/Project_ID/g" *.html
並對您的圖像寬度進行類似的修改。
但是,當使用-i
開關時,GnuWin32 sed
的內聯編輯傾向於在其喚醒時留下帶有臨時生成名稱的垃圾臨時文件。 最好使用for
循環並像這樣重定向sed
的輸出:
for %%I in (*.html) do (
sed -r -e "s/Profile/Project_ID/g" %%I >%%I.temp
move /y %%I.temp %%I
)
@ECHO OFF
SETLOCAL
:: Get directory from cmdline
SET sourcedir=%~1
IF NOT DEFINED sourcedir ECHO Require source directory&GOTO :EOF
IF NOT exist "%sourcedir%\*.jpg" ECHO No JPGs found IN %sourcedir%&GOTO :EOF
::
:: get name of project (last element in path)
::
FOR %%i IN ("%~1") DO SET project=%%~ni
ECHO project is %project%
:: Create subdir for HTMLs
SET "newhtmls=%sourcedir%\newhtmls"
MD "%newhtmls%" 2>NUL
::
:: %2 is the name of a file containing .jpg names in the order
:: in which they are to appear.
::
:: If %2 is missing, create a list file
SET imgseqlist=%2
IF DEFINED imgseqlist GOTO haveseq
SET imgseqlist=%temp%\htmlfilelist.txt
SET dellist="%imgseqlist%"
DIR /b /a-d /ON "%sourcedir%\*.jpg" >"%imgseqlist%"
:haveseq
::
:: Build file of image details
:: I use IRFANVIEW
::
SET imgdetails=%temp%\imagedetails.txt
SET dellist=%dellist% "%imgdetails%"
"C:\Program Files (x86)\irfanview\i_view32.exe" "%sourcedir%\*.jpg" /info="%imgdetails%"
::
:: Now let's process
::
(SET firstimg=)
FOR /f "usebackqdelims=" %%i IN ("%imgseqlist%") DO (
IF DEFINED firstimg (SET nextimg=%%i&CALL :generate) ELSE (SET firstimg=%%i)
(SET currimg=%%i)
)
:: Do the last one...
SET nextimg=%firstimg%
CALL :generate
:: And finally delete the tempfile
DEL %dellist%
GOTO :EOF
:generate
:: output filename=current imagename -last 4 chars (.jpg)
SET outfile=%currimg:~0,-4%.html
SET nexthtml=%nextimg:~0,-4%.html
::
:: Look up the details of your image.
:: using IRFANVIEW format
::
(SET setdetails=)
FOR /f "usebackq tokens=1,2,4,6" %%d IN ("%imgdetails%") DO (
IF DEFINED setdetails IF "%%e"=="dimensions" (SET width=%%f&SET height=%%g&SET setdetails=)
IF /i %%d==[%nextimg%] SET setdetails=Y
)
:: I'd have filled out these details if you'd posted them
:: rather than just a picture :(
>%newhtmls%\%outfile% (
ECHO ^<!DOCTYPE...transitional.dtd"^>
ECHO ^<html...xthml"^>
ECHO ^<head^>
ECHO ^<meta...-8" /^>
ECHO ^<title^>%project^</title^>
ECHO ^<...and so ON...^>
ECHO background-image:url(images/%currimg%^);
ECHO ^<...and so ON...^>
ECHO ^</head^>
ECHO.
ECHO ^<body^>
ECHO ^<a href="%nexthtml%"^>^<img src="images/blank.gif" width="%width%" height="%height%" /^>^</a^>
ECHO ^</body^>
ECHO ^</html^>
)
GOTO :eof
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.