[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文件操作] 求教获取文件名,循环打印某个字段!

  小弟想编写这样的一个程序,获取一个目录下的所有文件名(包括子目录里)

得到这样的一个文件名,如:psp\08.jpg
把这些文件名保存到一个数组中,然后替换指定的文件名,再循环打印出这些字段,如:
<tr>
    <td align="center" scope="col"><img src="XXX" width="480" height="272" /></td>
  </tr>

换成
<tr>
    <td align="center" scope="col"><img src="psp/08.jpg" width="480" height="272" /></td>
  </tr>
再循环打印出这些字段,最终得到
<tr>
    <td align="center" scope="col"><img src="psp/02.jpg" width="480" height="272" /></td>
  </tr>
  <tr>
    <td align="center" scope="col"><img src="psp/06.jpg" width="480" height="272" /></td>
  </tr>
  <tr>
    <td align="center" scope="col"><img src="psp/08.jpg" width="480" height="272" /></td>
  </tr>

小弟就是想制作这样的一个网页相册,虽然学过一点编程,但试了多次也没有成功! 求懂编程的大侠,教我一下,用bat批处理或VBS,VB都行,谢谢了!


[ 本帖最后由 ganjie 于 2008-12-2 10:39 编辑 ]

  粉红色部分第二行与蓝色部分相比,少了一些内容,怀疑你是不是写错了?抑或是你有意要这样的效果?请明示。
尺有所短寸有所长,学好批处理没商量;
考虑问题复杂化,解决问题简洁化。

心在天山,身老沧州。

TOP

没有写错,可能是我的文字表达不是很好,我的意思是把这些文件名保存到一个数组中,然后替换指定的文件名,再循环打印出这些字段!

TOP

  实际上,你在我2楼的疑问帖发出来之后,已经修改了顶楼的内容。那就按照目前顶楼的意思给你写一段批处理吧。这段批处理只能帮你生成带html标记的图片列表,不能帮你生成整个html文件,请自行复制list.txt中的内容到html文件中去,请自行测试。
  1. @echo off
  2. set "route=d:\backup\我的文档\snagit\图库\图片"
  3. cd.>list.txt
  4. for /f "delims=" %%i in ('dir /a-d /b /s "%route%\*.jpg"') do (
  5.     set "str=%%i"
  6.     setlocal enabledelayedexpansion
  7.     set "str=!str:%route%=!"
  8.     set "str=!str:\=/!"
  9.     (
  10.         echo ^<tr^>
  11.         echo     ^<td align="center" scope="col"^>^<img src="!str!" width="480" height="272" /^>^</td^>
  12.         echo ^</tr^>
  13.     )>>list.txt
  14.     endlocal
  15. )
  16. pause
复制代码
尺有所短寸有所长,学好批处理没商量;
考虑问题复杂化,解决问题简洁化。

心在天山,身老沧州。

TOP

刚试过,很不错,非常谢谢您!不过我还有个小小的要求,因为我要制作网页相册,每个图片子文件夹都要打印出一个list.txt,可以吗?如:上面贴图 中目录下面的PSP、AB,都打印出个列表,如psp.txt,AB.txt(这个文本就放在指定的目录下,如上图中的D:\Backup\我的文档\SnagIt 图库),文本里内容例如:
<html>
<head>
<meta http-equiv="Content-Type" c />
<title>psp壁纸</title>
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
    <td align="center" scope="col"><img src="psp/04.jpg" width="480" height="272" /></td>
</tr>
  <tr>
    <td align="center" scope="col"><img src="psp/06.jpg" width="480" height="272" /></td>
  </tr>
  <tr>
    <td align="center" scope="col"><img src="psp/08.jpg" width="480" height="272" /></td>
  </tr>
</table>
</body>
</html>
先打印出
<html>
<head>
<meta http-equiv="Content-Type" c />
<title>psp壁纸</title>
</head>

<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">

然后再打印出要循环的部份
<tr>
    <td align="center" scope="col"><img src="psp/04.jpg" width="480" height="272" /></td>
</tr>

  <tr>
    <td align="center" scope="col"><img src="psp/06.jpg" width="480" height="272" /></td>
  </tr>
  <tr>
    <td align="center" scope="col"><img src="psp/08.jpg" width="480" height="272" /></td>
  </tr>
然后再打印出

</table>
</body>
</html>

这样的话,我只要把txt后缀改成html就行了!




[ 本帖最后由 ganjie 于 2008-12-2 11:51 编辑 ]

TOP

  既然要在每个文件夹下生成html文件,其中的路径如psp/08.jpg之类的是否已经不用再写得那么详细,直接写成08.jpg应该更好一点吧?
尺有所短寸有所长,学好批处理没商量;
考虑问题复杂化,解决问题简洁化。

心在天山,身老沧州。

TOP

我是想把图片制作成html的网页相册的,所以才想出这个批处理的,如果不要路径如psp/08.jpg,就不能为每个图片子目录生成一个html文件了,所以…………,请管理员试试看,可以用批处理吗?还要这个<title>XXX</title>,可以把XXX变成子文件夹的文件名吗?谢谢!

TOP

  假设每个文件夹下的图片文件使用的是最直接的相对路径,那么,可以用这个代码来试试(此代码在每个子文件夹下生成一个以当前文件夹文件命名的html文件):
  1. @echo off
  2. set "route=d:\backup\我的文档\snagit\图库\图片"
  3. for /f "delims=" %%i in ('dir /ad /b /s "%route%"') do (
  4.     (
  5.         echo ^<html^>
  6.         echo ^<head^>
  7.         echo ^<meta http-equiv="Content-Type" c /^>
  8.         echo ^<title^>%%~nxi壁纸^</title^>
  9.         echo ^</head^>
  10.         echo ^<body^>
  11.         echo ^<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"^>
  12.     )>"%%i.html"
  13.     for /f "delims=" %%j in ('dir /a-d /b "%%i\*.jpg"') do (
  14.         (
  15.             echo ^<tr^>
  16.             echo     ^<td align="center" scope="col"^>^<img src="%%j" width="480" height="272" /^>^</td^>
  17.             echo ^</tr^>
  18.         )>>"%%i.html"
  19.     )
  20.     (
  21.         echo ^</table^>
  22.         echo ^</body^>
  23.         echo ^</html^>
  24.     )>>"%%i.html"
  25. )
  26. pause
复制代码
尺有所短寸有所长,学好批处理没商量;
考虑问题复杂化,解决问题简洁化。

心在天山,身老沧州。

TOP

  8楼的代码会在每个子文件夹下生成以当前文件夹为文件名的html文件,其中的图片路径使用的是最直接的相对路径,也就是说,如果字文件夹名为abc,那么,图片路径就不用abc\02.jpg,而直接写成02.jpg,这个在网页中是可以正确显示的。
尺有所短寸有所长,学好批处理没商量;
考虑问题复杂化,解决问题简洁化。

心在天山,身老沧州。

TOP

你回贴真快,谢谢,我试了一下,如psp这个文件夹,生成html后,内容如下:
<html>
<head>
<meta http-equiv="Content-Type" c />
<title>psp壁纸</title>
</head>
<body>
<table width="100" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
    <td align="center" scope="col"><img src="04.jpg" width="480" height="272" /></td>
</tr>
<tr>
    <td align="center" scope="col"><img src="06.jpg" width="480" height="272" /></td>
</tr>
<tr>
    <td align="center" scope="col"><img src="08.jpg" width="480" height="272" /></td>
</tr>
</table>
</body>
</html>
您没有在这个文件名前加上文件夹名呀!注意上面的蓝色部分应变成"psp\08.jpg",这样才能显示图片呀,因为08.jpg这个文件是在psp这个文件夹里的!而psp.html在D:\Backup\我的文档\SnagIt 图库\图片 这个路径里,请再修改一下,再次打扰您了!

TOP

  哦,原来是想在 D:\Backup\我的文档\SnagIt 图库\图片 这个路径下保存所有的html文件啊,我还以为是要在每个子文件夹下保存html文件呢。既然要在 图片 文件夹下保存所有的html文件,那么,结合4楼的代码,把8楼的代码稍微修改一下就可以了。
  请测试这个代码:
  1. @echo off
  2. set "route=d:\backup\我的文档\snagit\图库\图片"
  3. for /f "delims=" %%i in ('dir /ad /b /s "%route%"') do (
  4.     (
  5.         echo ^<html^>
  6.         echo ^<head^>
  7.         echo ^<meta http-equiv="Content-Type" c /^>
  8.         echo ^<title^>%%~nxi壁纸^</title^>
  9.         echo ^</head^>
  10.         echo ^<body^>
  11.         echo ^<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"^>
  12.     )>"%route%\%%~nxi.html"
  13.     for /f "delims=" %%j in ('dir /a-d /b "%%i\*.jpg"') do (
  14.         set "str=%%i\%%j"
  15.         setlocal enabledelayedexpansion
  16.         set "str=!str:%route%\=!"
  17.         set "str=!str:\=/!"
  18.         (
  19.             echo ^<tr^>
  20.             echo     ^<td align="center" scope="col"^>^<img src="!str!" width="480" height="272" /^>^</td^>
  21.             echo ^</tr^>
  22.         )>>"%route%\%%~nxi.html"
  23.         endlocal
  24.     )
  25.     (
  26.         echo ^</table^>
  27.         echo ^</body^>
  28.         echo ^</html^>
  29.     )>>"%route%\%%~nxi.html"
  30. )
  31. pause
复制代码
尺有所短寸有所长,学好批处理没商量;
考虑问题复杂化,解决问题简洁化。

心在天山,身老沧州。

TOP

小弟试了一下,没有显示图片呀,看了一下代码,发现图片路径错了呀,以下是psp这个文件夹的html,注意蓝色的
<html>
<head>
<meta http-equiv="Content-Type" c />
<title>psp壁纸</title>
</head>
<body>
<table width="100" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
    <td align="center" scope="col"><img src="/psp" width="480" height="272" /></td>
</tr>
<tr>
    <td align="center" scope="col"><img src="/psp" width="480" height="272" /></td>
</tr>
<tr>
    <td align="center" scope="col"><img src="/psp" width="480" height="272" /></td>
</tr>
</table>
</body>
</html>

TOP

  在第二次dir的时候,set语句忘记添加了绝对路径,现在已经更新了11楼的代码,请继续测试。
尺有所短寸有所长,学好批处理没商量;
考虑问题复杂化,解决问题简洁化。

心在天山,身老沧州。

TOP

还是没有图片显示,还是图片的路径问题,
<html>
<head>
<meta http-equiv="Content-Type" c />
<title>psp壁纸</title>
</head>
<body>
<table width="100" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
    <td align="center" scope="col"><img src="/psp/04.jpg" width="480" height="272" /></td>
</tr>
<tr>
    <td align="center" scope="col"><img src="/psp/06.jpg" width="480" height="272" /></td>
</tr>
<tr>
    <td align="center" scope="col"><img src="/psp/08.jpg" width="480" height="272" /></td>
</tr>
</table>
</body>
</html>
蓝色部分,应是psp/08.jpg,你多了一个"/",请再帮我修改一下,大体快要完美了,谢谢!

TOP

  已经修改了11楼代码,不会再多出一个斜杠了。
尺有所短寸有所长,学好批处理没商量;
考虑问题复杂化,解决问题简洁化。

心在天山,身老沧州。

TOP

返回列表