BAT文件将文件夹结构索引到HTML [英] BAT file to index folder structure to HTML

查看:73
本文介绍了BAT文件将文件夹结构索引到HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从未写过BAT文件,但我想执行以下操作.

I've never written a BAT file before, but I want to do the following.

在共享的网络驱动器上,我们具有文件/文件夹结构.我希望我的同事能够双击BAT文件.然后,BAT文件应生成这些文件的HTML索引以及链接,每个文件夹和子文件夹的头(不超过三个级别).

On a shared network drive we've got a file / folder-structure. I want my colleagues to be able to double click a BAT file. The BAT file should then produce an HTML index of these files with links, and a header per folder and subfolder (no more than three levels).

我认为我已经非常接近谷歌搜索和复制代码了,但是我被卡在了标题部分.

I think I've gotten pretty close Googling and copying code, but I'm stuck at the header part.

setlocal EnableDelayedExpansion
pushd %~dp0
for /L %%n in (1 1 500) do (
if "!__cd__:~%%n,1!" neq "" set /a "len=%%n+1"
)

for /r . %%g in (*) do (
setlocal EnableDelayedExpansion
  set "absPath=%%g"
  set "relPath=!absPath:~%len%!"
  for %%F in ("!absPath!") do set "var=%%~dpF"

  [MISSING CODE HERE]

  echo "<a href='http://linkhere.com/!relPath!'>%%~ng</a>" >>output.txt
  endlocal
)
echo %~dp0
popd
pause

这可能是一团糟,因为我不确定自己在做什么. 该文件将装入驱动器,然后循环浏览所有文件夹并将它们列出为链接.但是,每次循环到达新文件夹或子文件夹时,我都需要添加一个标头.该标头应为HTML,因此我可以添加诸如展开,折叠和格式化之类的功能.

It might be messy, since I'm not exactly sure what it's doing. This file mounts the drive, then loops through all the folders and lists them as a link. However, I need to add a header everytime the loop reaches a new folder or subfolder. This header should be HTML, so I can add functionality like expand and collapse and formatting.

我已经尝试过朝这个大致方向进行尝试

I've tried it with something in this general direction

for /f "tokens=1,2,3,4 delims=\" %%a in ("!relPath!") do set a=%%a&set b=%%b&set c=%%c&set d=%%d
set "fname=!d!" & set "fone=!a!" & set "ftwo=!b!" & set "fthree=!c!" & set "mainFolder=!a!"
IF [!fname!] == [] (set "fname=!c!" & set "fone=!a!" & set "ftwo=!b!" & set "fthree=")
IF [!fname!] == [] (set "fname=!b!" & set "fone=!a!" & set "ftwo=" & set "fthree=")
IF [!fname!] == [] (set "fname=!a!" & set "fone=" & set "ftwo=" & set "fthree=" & set "mainFolder=")

这会将每个文件夹和文件名设置为一些变量.每当这些变量之一发生变化时,我都希望使用它来回显标头,但是我似乎无法解决这个问题.

This sets every folder and filename into some variables. I was hoping to use this to echo a header every time one of these variables change, but I can't seem to work it out.

也许我完全走错了路,但是我很高兴听到应该怎么做.

Maybe I'm completely on the wrong track, but I'll gladly hear how it should be done.

谢谢.

编辑> Foxidrive建议我添加文件夹结构和输出示例.

Edit > Foxidrive suggested I'd add in examples of the folder structure and the output.

上面的屏幕截图演示了文件夹结构. BAT文件将位于顶部文件夹(称为 Standaard formulieren ).

The screenshot above demonstrates the folder structure. The BAT file would be located in the top folder (Called Standaard formulieren).

输出应该是这种伪HTML形式的

Output should be something in the form of this pseudo-HTML

<h2 class='header' onClick='showHide(1)'>ABU</h2>
<div id='itemlist1' class='itemlist'>
<ul>
 <li><a href='http://link-to-file.com/ABU/filename.doc'>Filename</a></li>
 <li><a href='http://link-to-file.com/ABU/filename%202.doc'>Filename 2</a></li>
</ul> 
</div>
[etc...]
<h2 class='header' onClick='showHide(9)'>Personeelszaken</h2>
<div id='itemlist9' class='itemlist'>
<h3 class='subheader' onClick='showHide(10)'>Algemeen</h2>
<div id='itemlist10' class='subitemlist'>
<ul>
 <li><a href='http://link-to-file.com/Personeelszaken/Algemeen/filename.doc'>Filename</a></li>
 <li><a href='http://link-to-file.com/Personeelszaken/Algemeen/filename%202.doc'>Filename 2</a></li>
</ul>
</div> 
</div>
[etc...]

推荐答案

复制HTML不是我的专长,但这应该会创建一种HTML,其中包含的信息足以帮助您-希望如此.

Reproducing your HTML is not my specialty but this should create a HTML of sorts which contains enough info to help you on your way - well I hope so.

已以递归方式包含子目录的完整列表,并纠正了相对路径

@echo off
setlocal enabledelayedexpansion

set "basefolder=d:\Standaard formulieren"
pushd "%basefolder%"

(
for /d /r %%a in (*) do (
   pushd "%%a"
          echo going into "%%a"
    for /r %%b in (*) do (
       set "relfolder=%%a"
       set "relfolder=!relfolder:%basefolder%\=!"
       set "relfolder=!relfolder:\=/!"
       set "filename=%%~nb"
       set "filename=!filename: =%%20!"
          echo ^<li^>^<a href="http://link-to-file.com/!relfolder!/!filename!%%~xb"^>%%~nb^</a^>^</li^>
    )
   popd
   echo leaving "%%a"
)
)>"file.html"
echo done
pause

这篇关于BAT文件将文件夹结构索引到HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆