How do I do this ? - Producing a Dir listing by level - not by following structure

左心房为你撑大大i 提交于 2019-12-02 09:49:34
@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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!