#1 18 Sep 2013 00:42

questorfla
New Member
Registered: 17 Sep 2013
Posts: 1

batch script for specific format. No Links Allowed :( 3W's is example

I found your forum while researching a way to create a special directory listing.  There were some pretty unique ideas in this forum that came very close to what I needed.  I have tried modifying them with some success but still end up getting an output that needs modifications to be of use in the way I need it.

I manage a site that hosts a group of sub-sites made by various people.  the entry to their sites includes 3w's.themastersite/theirname/theirsubsite.

with that directory list, I have a perfect list of access ports for each sub-site.  VERY useful for organizing and making sure that all is correct.

What I need is a directory list that would show the full path from the top level to each sublevel and every sub-sub-level 4 levels deep beneath it.  Just the directory names.  no file names.  I have tried various incantations of dir and gotten nowhere.
the first problem is that there is a "buffer" folder between the master site and the sub-sites called "webroot". 
If I could run "DIR *.  /S/4"  while I was in the "3W's.mastersite.com" folder  and get 3w's.mastersite.com/webroot/johndoe/site1, next line 3w's.mastersite.com/webroot/johndoe/site2, and so on.
the Johndoe would eventually change to marysmith, and all the rest of the people.  the fictional "dir command" (as written) is what I would assume to be asking for directories only and 4 levels deep.

If anyone knows a way to accomplish this in a single run I would be ever so grateful.  the DIR command wont do it.  I have also tried a variation of the DU and the best I got at all was using powershell.  I would gladly accept even having webroot left in as I can run a search and replace to get rid of it and the backward slashes. (which is part of what I am having to do now smile  )  I have to run this list at least once a day and it gets tiring to say the least.

Offline

#2 18 Sep 2013 10:59

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: batch script for specific format. No Links Allowed :( 3W's is example

This works here:

@echo off
for /f "delims=" %%a in ('dir /ad /b /s ^| findstr \\.*\\.*\\.*\\.* ^| findstr /v \\.*\\.*\\.*\\.*\\') do echo %%a
pause

Offline

#3 19 Sep 2013 10:39

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,077

Re: batch script for specific format. No Links Allowed :( 3W's is example

While I guess foxidrive's method could probably get the job done the problem would be that it will still parse the entire directory structure -- with web content that could be rather large.

I propose this alternative method:

@echo off

set "dir=X:\yourDirectory"

for /d %%a in ("%dir%\*") do (
for /d %%b in ("%%~a\*")  do (
for /d %%c in ("%%~b\*")  do (
for /d %%d in ("%%~c\*")  do (
echo:"%%~d"
))))

goto :EOF

It's not very extensible or flexible, but should work fine.  With a bit of work I think a variation on this method it could be made to select an arbitrary number of directory levels.

Last edited by bluesxman (19 Sep 2013 10:41)


cmd | *sh | Ruby | Chef

Offline

#4 19 Sep 2013 10:51

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,077

Re: batch script for specific format. No Links Allowed :( 3W's is example

Actually, if you have robocopy (comes as standard with Vista and up; also available from Microsoft for anything older) you can do it like this:

robocopy /lev:4 /e /njh /njs /nc /ns /nfl /l X:\yourDirectory X:\not\a\real\directory

NB -- level "1" is the root of "yourDirectory" so you may need to increment the "/lev" switch by one to get the desired result.


cmd | *sh | Ruby | Chef

Offline

#5 19 Sep 2013 13:10

Aacini
Member
Registered: 05 Dec 2012
Posts: 143

Re: batch script for specific format. No Links Allowed :( 3W's is example

@echo off

rem DirLevel.bat - Antonio Perez Ayala

setlocal EnableDelayedExpansion

if "%1" neq "" goto begin
echo List directories up to a given level from current directory
echo/
echo    DirLevel level
goto :eof

:begin
set "curDir=%cd%"
if "%curDir:~-1%" neq "\" set "curDir=%curDir%\"
set "lastName="
for /D /R %%a in (*) do (
   set "dir=%%a"
   set "dir=!dir:*%curDir%=!"
   set level=0
   set "name="
   for %%b in ("!dir:\=" "!") do if "!level!" neq "%1" (
      set name=!name!\%%~b
      set /A level+=1
   )
   if "!name!" neq "!lastName!" (
      echo %curDir%!name:~1!
      set "lastName=!name!"
   )
)

Antonio

Offline

Board footer

Powered by FluxBB