批处理文件未给出正确的结果

时间:2014-05-29 09:17:56

标签: batch-file tree cmd

我正在创建一个 .bat 文件,它将在不同的驱动器中生成目录的图形表示。但是当我运行它时,它没有给出正确的结果 这是我的bat文件代码

@echo off
D:
tree /f /a > D:\d_drive.txt
tree /f /a > E:\d_drive.txt
tree /f /a > F:\d_drive.txt
tree /f /a > G:\d_drive.txt
tree /f /a > H:\d_drive.txt
E:
tree /f /a > D:\e_drive.txt
tree /f /a > E:\e_drive.txt
tree /f /a > F:\e_drive.txt
tree /f /a > G:\e_drive.txt
tree /f /a > H:\e_drive.txt
F:
tree /f /a > D:\f_drive.txt
tree /f /a > E:\f_drive.txt
tree /f /a > F:\f_drive.txt
tree /f /a > G:\f_drive.txt
tree /f /a > H:\f_drive.txt
G:
tree /f /a > D:\f_drive.txt
tree /f /a > E:\f_drive.txt
tree /f /a > F:\f_drive.txt
tree /f /a > G:\f_drive.txt
tree /f /a > H:\f_drive.txt
H:
tree /f /a > D:\g_drive.txt
tree /f /a > E:\g_drive.txt
tree /f /a > F:\g_drive.txt
tree /f /a > G:\g_drive.txt
tree /f /a > H:\g_drive.txt

当我运行这个文件时,我得到了所有的文本文件但是下面有类似的东西

文件夹路径列表 卷序列号为00000200 2067:FF00 d :.     新文本Document.bat

没有子文件夹

3 个答案:

答案 0 :(得分:2)

像这样改变

D:
cd/
tree /f /a > D:\d_drive.txt

如果您在E:/test/test然后执行C:并再次执行E:,则您将E:/test/test而不是E:cd/会让你处于根级别

答案 1 :(得分:1)

tree需要很多时间,所以尝试只做一次,然后将结果复制到不同的位置。 您也可以告诉tree从哪里开始:

tree D:\ /f /a >D:\d_drive.txt
copy /y d:\d_drive.txt e:\
copy /y d:\d_drive.txt f:\
copy /y d:\d_drive.txt g:\
copy /y d:\d_drive.txt h:\

更优雅的方式:

for %%i in (d e f g h) do tree %%i:\ /f /a >%%i_drive.txt
for %%i in (d e f g h) do copy /y ?_drive.txt >%%i:\

(第一步:创建所有树;第二步,分发它们)

答案 2 :(得分:0)

试试这个:

@echo off

set drives=d e f g h
for %%a in (%drives%) do (
   %%a:
   cd \
   tree /f /a > %%a_drive.txt
   for %%b in (%drives%) do (
      if %%a neq %%b copy %%a_drive.txt %%b:\
   )
)