我该怎么做呢 ? - 按级别生成Dir列表 - 而不是遵循结构 [英] How do I do this ? - Producing a Dir listing by level - not by following structure

查看:221
本文介绍了我该怎么做呢 ? - 按级别生成Dir列表 - 而不是遵循结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望你能在这里保存我的理智。我被要求产生一个报告,显示在Windows服务器上的所有文件和文件夹。不是你会认为一个问题 - 目录/ s> report.txt和你做对了吗?

错误。

我被告知,报告必须按级别,而不是遵循文件夹结构。解释。想象一下,我们有一个像这样布置的服务器 -

  E:
├───动物
│ ├───Birds
│├───Mammals
│└───Reptiles
├───Cities
│└────────────────────────────────────────────────────────────────────────────────── ───非洲
├───欧洲
│└───北美洲
└───植物
├───花卉
├── □□□□
└───Trees





  E:
目录23/11/2017 11:57< DIR> 。
23/11/2017 11:57< DIR> ..
23/11/2017 11:46< DIR>动物
23/11/2017 11:51< DIR>城市
23/11/2017 11:52< DIR>植物

0文件0字节

E:\动物目录

23/11/2017 11:46< DIR> ; 。
23/11/2017 11:46< DIR> ..
23/11/2017 11:47< DIR>鸟
23/11/2017 11:49< DIR>哺乳动物
23/11/2017 11:50< DIR>爬行动物

0文件0字节

E:\Animals\Birds

23/11/2017 11:47 < DIR> 。
23/11/2017 11:47< DIR> ..
23/11/2017 11:47 0 Duck.txt
23/11/2017 11:47 0 Ostrich.txt
23/11/2017 11:47 0 Pigeon.txt

3文件0字节

E:\\动物\Mammals

23/11/2017 11:49< ; DIR> 。
23/11/2017 11:49< DIR> ..
23/11/2017 11:48 0 Aardvark.txt
23/11/2017 11:48 0 cat.txt
23/11/2017 11:48 0 dog.txt

3文件0字节

E:\Animals\Reptiles目录

23/11/2017 11:50< ; DIR> 。
23/11/2017 11:50< DIR> ..
23/11/2017 11:49 0 Iguana.txt
23/11/2017 11:50 0 Sand Lizard.txt

2文件0字节

等等



-

  E的目录:

23/11/2017 11:57< DIR> 。
23/11/2017 11:57< DIR> ..
23/11/2017 11:46< DIR>动物
23/11/2017 11:51< DIR>城市
23/11/2017 11:52< DIR>植物

0文件0字节

E:\动物目录

23/11/2017 11:46< DIR> ; 。
23/11/2017 11:46< DIR> ..
23/11/2017 11:47< DIR>鸟
23/11/2017 11:49< DIR>哺乳动物
23/11/2017 11:50< DIR>爬行动物

0文件0字节

E:\Cities

23/11/2017 11:51< DIR> ; 。
23/11/2017 11:51< DIR> ..
23/11/2017 11:51< DIR>大洲

0文件0字节

E:\Plants目录

23/11/2017 11:52< DIR> ; 。
23/11/2017 11:52< DIR> ..
23/11/2017 11:51< DIR>鲜花
23/11/2017 11:51< DIR>草
23/11/2017 11:51< DIR>树

0文件0字节

etc ...

我已经被告知在Windows资源管理器中手动列出每个文件夹,然后将每个文件夹屏幕打印成一个word文档。

<我是什么!有超过14,000个文件夹!我做了什么应得的?!我是否在晚上死亡,没有注意到?是不是地狱应该是充满了火焰和花花公子周围携带干草?



我不是那熟悉的Windows命令行,但相信有循环?可以使用其中之一,以某种方式循环通过水平是一个解决方案?有解决方案吗? (哦,我希望有一个解决方案!)。
$ b

请别人救我从永恒的屏幕上打印地狱!





-Edit 1 -



我一直在阅读命令行上的各种网页,而我仍然不明白For / f命令,我遇到了弹出和pushd,这使您可以将当前目录存储在虚拟堆栈。因为我的问题是,我需要以某种方式修改dir命令的行为,以便按级别列出文件夹,而不是按照明显的文件夹结构,我想知道是否有任何方法可以使用pushd和popd来做到这一点?

使用dir命令使我能够将文件从目录列表(dir *。/ s / b)中除去,但仍然尝试遵循这个结构,在我的例子dir *中。 / s / b给出 -

E:\动物

E:\Cities

E:\Plants

E:\Animals\Birds

E:\动物\Mammals

E:\动物\Reptiles

E:\Cities\Continents

E:\Cities\Continents\Acrica

E:\Cities\Continents\Europe

E:\Cities\Continents\ North America

E:\Plants\Flowers

E:\Plants\Grasses

E:\Plants\Trees

我需要以某种方式获取所有与相同数量级别的目录 - 所以移动E:\plants\\ \\ xxxxx到E:\Cities\Continents。我不知道如果我有更接近的解决方案,排序输出的DIR / S / B不起作用,所以它看起来像我将不得不以某种方式解析dir / s / b的输出。 b $ b






- 编辑2 -



是用高级语言做的 - 而不是批处理文件 - 我会把dir / s / b的输出结果设置成一个变量x - x。然后,我将运行dir输出并复制用x \在他们的行中添加一个新的文件。在eof我会增加x并再次运行文件(与x这个时候2,它会挑出所有的行2 \在其中)。我会继续这样做,直到没有行内有x \在那里,这将给我一个所需的顺序文件。然后,我会运行新文件,为每个条目执行一个简单的目录。

事情是我不知道如何在批处理文件中完成上述操作 - 或者即使有可能,我也无法访问此项目的高级语言。我甚至不认为他们会让我使用Word vb,因为我在任何情况下都做了Visual Basic编程。 pre $ @echo off
setlocal enabledelayedexpansion
$ b $(for / fdelims =%% a in('dir / b / s / ad' )(
setline = %% a
setline =!line:\ =!
set depth = 100
for %% b in(! line)!set / adepth + = 1
echo!depth!#%% a
))> temp
for / ftokens = 1 * delims =# %% a in('sort temp')do echo %% b


$ b


  • 您的 find / c 方法失败,因为它没有找到出现的地方,但与行搜索字符串(这是没有关系,如果有一个或几个调查结果在一行)
  • 我用 dir ' s开关 / ad 只显示目录。

  • 用于计数,我为循环使用了一个(对于每一个字都增加一个计数器)
  • 因此,我首先从字符串中删除空格,然后用空格替换每个 \

  • 用100而不是0开始计数可能听起来很奇怪,但它使代码能够处理深度大于10( sort sort>字符串,所以 4 会大于 12

  • 必须使用临时文件(管道重新启用 echo


I hope that you can save my sanity here. I have been asked to produce a report showing all the files and folders on a windows server. Not you would think a problem - dir /s > report.txt and your done right?

Wrong.

I have been told that the report must be by level and not following the folder structure. To explain. Imagine we have a server laid out like this -

E:
├───Animals
│   ├───Birds
│   ├───Mammals
│   └───Reptiles
├───Cities
│   └───Continents
│       ├───Africa
│       ├───Europe
│       └───North America
└───Plants
    ├───Flowers
    ├───Grasses
    └───Trees

Dir /s would produce -

Directory of E:
23/11/2017  11:57    <DIR>          .
23/11/2017  11:57    <DIR>          ..
23/11/2017  11:46    <DIR>          Animals
23/11/2017  11:51    <DIR>          Cities
23/11/2017  11:52    <DIR>          Plants

               0 File(s)            0 bytes

 Directory of E:\Animals

23/11/2017  11:46    <DIR>          .
23/11/2017  11:46    <DIR>          ..
23/11/2017  11:47    <DIR>          Birds
23/11/2017  11:49    <DIR>          Mammals
23/11/2017  11:50    <DIR>          Reptiles

               0 File(s)              0 bytes

 Directory of E:\Animals\Birds

23/11/2017  11:47    <DIR>          .
23/11/2017  11:47    <DIR>          ..
23/11/2017  11:47                 0 Duck.txt
23/11/2017  11:47                 0 Ostrich.txt
23/11/2017  11:47                 0 Pigeon.txt

               3 File(s)              0 bytes

 Directory of E:\Animals\Mammals

23/11/2017  11:49    <DIR>          .
23/11/2017  11:49    <DIR>          ..
23/11/2017  11:48                 0 Aardvark.txt
23/11/2017  11:48                 0 cat.txt
23/11/2017  11:48                 0 dog.txt

               3 File(s)              0 bytes

 Directory of E:\Animals\Reptiles

23/11/2017  11:50    <DIR>          .
23/11/2017  11:50    <DIR>          ..
23/11/2017  11:49                 0 Iguana.txt
23/11/2017  11:50                 0 Sand Lizard.txt

               2 File(s)              0 bytes

and so on

But they want -

Directory of E:

23/11/2017  11:57    <DIR>          .
23/11/2017  11:57    <DIR>          ..
23/11/2017  11:46    <DIR>          Animals
23/11/2017  11:51    <DIR>          Cities
23/11/2017  11:52    <DIR>          Plants

               0 File(s)            0 bytes

Directory of E:\Animals

23/11/2017  11:46    <DIR>          .
23/11/2017  11:46    <DIR>          ..
23/11/2017  11:47    <DIR>          Birds
23/11/2017  11:49    <DIR>          Mammals
23/11/2017  11:50    <DIR>          Reptiles

               0 File(s)              0 bytes

Directory of E:\Cities

23/11/2017  11:51    <DIR>          .
23/11/2017  11:51    <DIR>          ..
23/11/2017  11:51    <DIR>          Continents

               0 File(s)              0 bytes

Directory of E:\Plants

23/11/2017  11:52    <DIR>          .
23/11/2017  11:52    <DIR>          ..
23/11/2017  11:51    <DIR>          Flowers
23/11/2017  11:51    <DIR>          Grasses
23/11/2017  11:51    <DIR>          Trees

               0 File(s)              0 bytes

etc...

I have been told to manually list each folder in windows explorer and then screen print each one into a word document.

I mean what!??? There are over 14,000 folders! What did I do to deserve this?! Did I die in the night and not notice? Isn't Hell supposed to be full of flames and dudes wondering around carrying pitchforks??

I am not that familiar with the windows command line, but believe that there are for loops? Could using one of these and somehow looping through the levels be a solution? Is there a solution? (Oh How I hope there's a solution!).

Please someone save me from eternal screen print hell!


-Edit 1-

I have been reading the various web pages on the command line, and while I still do not understand the For /f command I have come across popd and pushd, which allow you to store the current directory on a virtual stack. Since my problem is that I need to somehow modify the behaviour of the dir command so that it lists folders by level and not by following the obvious folder structure I'm wondering if there is any way that I can use pushd and popd to do this?

Playing with the dir command has enabled me to strip the files out to be left with a list of directories (dir *. /s /b) but this still tries to follow the structure, in My example dir *. /s /b gives -

E:\Animals
E:\Cities
E:\Plants
E:\Animals\Birds
E:\Animals\Mammals
E:\Animals\Reptiles
E:\Cities\Continents
E:\Cities\Continents\Africa
E:\Cities\Continents\Europe
E:\Cities\Continents\North America
E:\Plants\Flowers
E:\Plants\Grasses
E:\Plants\Trees

I need to somehow get all the directories with the same number of levels grouped - so move the E:\plants\xxxx ones to below E:\Cities\Continents . I have no idea if I am any nearer a solution, sorting the output of dir /s /b does not work so it looks like I am going to have to somehow parse the output of dir /s /b.


-Edit 2 -

If I was doing this in a high level language - rather than a batch file - I'd take the output of dir /s /b and set a variable -say x - to 1. I'd then run through the dir output and copy any lines with x \'s in them to a new file. At the eof I'd increment x and run though the file again (with x as 2 this time it would pick out all the lines with 2 \'s in them). I'd continue to do so until no lines had x \'s in them and that would give me a file with the needed order. I'd then run through the new file performing a simple dir for each entry.

Thing is I have no idea how to do the above in a batch file - or even if its possible and I do not have access to a high level language for this project. I don't even think they would let me use Word vb and its years since I did any Visual Basic programming in any case.

解决方案

@echo off 
setlocal enabledelayedexpansion

(for /f "delims=" %%a in ('dir /b /s /ad') do (
  set "line=%%a"
  set "line=!line:\= !"
  set depth=100
  for %%b in (!line!) do set /a "depth +=1"
  echo !depth!#%%a
)) >temp
for /f "tokens=1* delims=#" %%a in ('sort temp') do echo %%b

Some of the used "tricks":

  • Your find /c approach fails, because it's not finding occurences, but lines with the searchstring (it doesn't matter if there is one or several findings in one line)
  • I used dir's switch /ad to show directories only.
  • for the counting, I use a for loop (for every word increase the counter by one)
  • Therefore, I first remove spaces from the string, then replace every \ with a space.
  • Starting the count with 100 instead of 0 may sound strange, but it enables the code to work with depths > 10 (sort sorts strings, so 4 would be greater than 12)
  • Sadly, I have to use a temp file (piping does re-enable echo on)

这篇关于我该怎么做呢 ? - 按级别生成Dir列表 - 而不是遵循结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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