1. 前言
本文介绍一个脚本,实现自动化个人图床。
具体来说,就是将当前目录下包括子目录下的所有图片添加一个img
标签,然后放到一个HTML
中。打开HTML
可以看到所有的图片。
2. 脚本
不多废话,直接上脚本。
@echo off&setlocal enabledelayedexpansion
title 自动化生成图床HTML网页
:: 当前路径
set basePath=%CD%\
rem echo !basePath!
:: blog目录
set subDir=blog\
:: 自增变量
set /a n=1
:: 循环遍历所有图片文件
for /r %%i in (*.jpg, *.png) do (
:: %%~nxi表示当前文件名称加文件后缀
rem echo %%~nxi
:: 是否为博客图片文件
set isBlogFile=false
:: 将循环量赋值给str
set str=%%i
:: 将str中的当前路径替换为空
set str=!str:%basePath%=!
rem echo !str!
rem echo !subDir!
:: 再将str中的blog目录替换掉
set fileName=!str:%subDir%=!
rem echo !fileName!
:: 若替换blog目录后只剩文件名,说明改文件是博客图片文件
if "!fileName!"=="%%~nxi" (
set isBlogFile=true
)
rem echo !isBlogFile!
:: 如果不是博客图片文件,则添加img标签输出到HTML中去
if "!isBlogFile!"=="false" (
:: 将str中的反斜杠替换为斜杠
set str=!str:\=/!
:: 添加img标签
set "str=<img src="!str!" />"
:: 如果是第一次,则直接替换文件内容,否则,在文件后面追加内容
if !n!==1 (
echo !str!>image.html
) else (
echo !str!>>image.html
)
:: 自增加1
set /a n+=1
)
)
:: 合并文件
copy /b head.html+image.html index.html
:: 暂停程序,按任意键继续
pause
3. 实践项目
上面的head.html
所包含的css
和js
,我已放到Gitee
上去了。
项目地址:https://gitee.com/yezhechenyang/imagebed.git
效果演示:https://yezhechenyang.gitee.io/imagebed/