Solved What is the oppsite command for Tree in Shell/Batch Script

September 27, 2012 at 10:52:00
Specs: Windows 7
I need a command which will create an entire directory structure from an input file.

In other words, a command that does the opposite of what "tree" does.

Is there such a command, or do you know any work around?


See More: What is the oppsite command for Tree in Shell/Batch Script

Report •


✔ Best Answer
September 29, 2012 at 00:46:18
yep, this one was a real bitch, and i still f_d it up. Back to school for this d_a. My trials didn't quite go far enough, so it looked ok. I needed one more level... (sigh... broke my balls this one did.) I'll try another disaster:

@echo off & setlocal enabledelayedexpansion
set tabc=0
for /f "tokens=* delims=@" %%a in (tree.txt) do (
set x=%%a
set x=!x: =_ !
set lc=!tabc!
set tabc=0
call :tabcount !x!
::echo ============= tabs: !tabc! hc: !hc! lc: !lc!
if !tabc! equ !lc! md !hc! & set lcd=!hc!
if !tabc! gtr !lc! cd !lcd! & md !hc! & set lcd=!hc!

if !tabc! lss !lc! (
set /a back=lc-tabc
for /L %%i in (1 1 !back!) do cd ..
md !hc! & set lcd=!hc!
)
)
goto :eof

:tabcount
if "%1" equ "" goto :eof
set /a tabc+=1
set hc=%1
shift
goto :tabcount



#1
September 27, 2012 at 12:02:47
mkdir will make a directory structure:

mkdir c:\one\two\three\etc

Will make all those directories.

Tony


Report •

#2
September 27, 2012 at 15:24:01
What does the contents of the input file look like?
A FOR loop and MKDIR should accomplish what you want.

Does the structure have to come from an input file or do you want to duplicate an existing file structure?
XCOPY /T should handle this instance.

When your only tool is a hammer, every problem looks like a nail.


Report •

#3
September 28, 2012 at 17:52:43
input file is a tab seperated tree structure like

sample ((single tabe replaces ------))
root1
------sub1.1
------------sub1.1.1
------------sub1.1.2
------sub1.2
root2
------sub2.1

etc ...


Report •

Related Solutions

#4
September 28, 2012 at 20:51:43
this one gave me a good workout! ;-)

@echo off & setlocal enabledelayedexpansion
set c=0
for /f "tokens=* delims=@" %%a in (tree.txt) do (
set x=%%a
::NOTE: this is a TAB (char 9) and not space(s) in the following line.
set x=!x:	=_	!
set lc=!c!
set c=0
call :tabcount !x!
::echo ============= tabs: !c!  hc: !hc!  lc: !lc!
if !c! geq !lc! md !hc! & cd !hc!

if !c! lss !lc! (
set /a back=lc-c+1
set c=!back!
for /L %%i in (1 1 !back!) do cd ..
md !hc!
)
)
goto :eof

:tabcount
if "%1" equ "" goto :eof
set /a c+=1
set hc=%1
shift
goto :tabcount
::======= end script

If you cut/paste this, the tab char may survive the transition. To check if it is a true tab, you can use edit /79, like so:
EDIT /79 eert.bat
and see if there is a tab, and not a space or spaces, where the tab should be, after the colon, and after the _, in this line:

set x=!x:	=_	!


(a tab looks much like a lower-case "o")

Report •

#5
September 28, 2012 at 21:55:50
[EDIT] Added an empty line check just in case. [/EDIT]

No guarantees it will work properly, but here it is anyway:

@echo off
SetLocal EnableDelayedExpansion


:: tabbed tree file
set in_file="d:\home\bat\test.txt"
:: root directory
pushd d:\test

set last_level=-1

(
    for /f %%a in ('type !in_file!^|find /c /v ""') do for /l %%b in (1 1 %%a) do (
        set line=
        set /p line=
        if defined line (
            set curr_level=0
            for %%c in (!line^:^	^= ^"^	^" !) do if %%c=="	" set /a curr_level+=1
        
            for /l %%d in (!curr_level! 1 !last_level!) do cd ..
            for %%e in (!curr_level!) do set line=!line:~%%e!
            md "!line!"
            cd "!line!"
            set last_level=!curr_level!
        )
    )
) < !in_file!
    

popd
pause


Report •

#6
September 28, 2012 at 22:10:04
Hey nbrane! I think you might have a few kinks to work out:


D:\test>tree /a
Folder PATH listing for volume WD_IDE
Volume serial number is C8A1-0E95
D:.
No subfolders exist


D:\test>type tree.txt
root1
        sub1.1
                sub1.1.1
                sub1.1.2
        sub1.2
root2
        sub2.1
D:\test>nbrane.bat

D:\test>tree /a
Folder PATH listing for volume WD_IDE
Volume serial number is C8A1-0E95
D:.
+---root1
|   \---sub1.1
|       +---sub1.1.1
|       |   \---sub1.1.2
|       \---sub1.2
+---root2
\---sub2.1

D:\test>


Report •

#7
September 28, 2012 at 23:56:27
You guys are awesome.... though not the perfect as yet, still just a few more yards to perfection I guess. Thanks a lot for you efforts. Do let me know if you do work on this more. I pondered with excel over the same, your solution is much better.

Report •

#8
September 29, 2012 at 00:05:46
though not the perfect as yet, still just a few more yards to perfection I guess

What's the problem?

If you don't explain it's not likely to be fixed.


Report •

#9
September 29, 2012 at 00:46:18
✔ Best Answer
yep, this one was a real bitch, and i still f_d it up. Back to school for this d_a. My trials didn't quite go far enough, so it looked ok. I needed one more level... (sigh... broke my balls this one did.) I'll try another disaster:

@echo off & setlocal enabledelayedexpansion
set tabc=0
for /f "tokens=* delims=@" %%a in (tree.txt) do (
set x=%%a
set x=!x: =_ !
set lc=!tabc!
set tabc=0
call :tabcount !x!
::echo ============= tabs: !tabc! hc: !hc! lc: !lc!
if !tabc! equ !lc! md !hc! & set lcd=!hc!
if !tabc! gtr !lc! cd !lcd! & md !hc! & set lcd=!hc!

if !tabc! lss !lc! (
set /a back=lc-tabc
for /L %%i in (1 1 !back!) do cd ..
md !hc! & set lcd=!hc!
)
)
goto :eof

:tabcount
if "%1" equ "" goto :eof
set /a tabc+=1
set hc=%1
shift
goto :tabcount


Report •

#10
September 29, 2012 at 02:01:21
@judago: hello!
it kicks an error due to the quotes around the !line! (md, cd) which don't seem to affect the performance anyway. nice job. Those quotes do need to be there at any rate due to MS BS (or contracted to "MSBS") for longfilenames. I wonder how many billion$ Gates blew on engineering that catastrophe. He really opened Pandora's box with that move, and now we will suffer indefinitely...

Report •

#11
September 29, 2012 at 03:19:16
@nbrane

Just out of interest what is the error, I can't seem to replicate it on my xp machine.


Report •

#12
September 29, 2012 at 18:56:55
I got "filename, dir.name or vol.label syntax is incorrect"
the line that kicked it was:
md " test2zd"
i think the quotes captured a tab, and that may have been the fault of my "test-set".
here's my testset:
test1a
	test2b
		test3c
	test2d
	test2e
		test3f
test1z
       	test2zd

(where all space is tabs of course.) Your script that I ran kicked that error and left off the very last one (until I removed the quotes, then it was fine).

Report •

#13
September 29, 2012 at 19:09:00
Yeah it looks like "test2zd" is indented with spaces and not tabs.

Report •

#14
September 29, 2012 at 19:18:17
I'm not really sure of what to do about that possibility. If I trim the spaces the visual aspect of the data is misleading, Removing the quotes does bad things if the names contain spaces(md making two directories instead of one) and converting "x spaces" to a tab has the edge case of directory names with "x spaces" embedded"

Anyhow it should be regular data since I doubt anyone entered the text by hand.

BTW: I'm pretty sure your script will have troubles with !'s.


Report •

#15
September 30, 2012 at 19:36:24
Yeah, after analyzing your code, mine sucks, Lol! Lots of holes and pitfalls await the unwary end-user. I used arbitrary @, which is includable in filenames, for one. for two, I did not code around the exclams (also includable in filenames). Yours definitely is the "best answer" but the judge (o.p) is not always up to speed either. (no offense to o.p., and thanks for the vote-of-confidence, but...just use Judago's code.)
I hope he keeps a backup copy of your code for when mine fails!

Report •


Ask Question