VBS Directory Tree

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
User avatar
PepsiHog
Automation Wizard
Posts: 517
Joined: Wed Apr 08, 2009 4:19 pm
Location: Florida

VBS Directory Tree

Post by PepsiHog » Sun Oct 14, 2012 10:19 pm

Hello Everyone,

I went online and found bits and pieces of how to do this, but I'm stuck. What I want is a vbscript that will create a directory tree and write it to a text file.

In this attempt, I got the second part to work. But I don't know what to change "wscript" to in the first part. And there is a bunch I still don't understand in the first part, but if I knew what "wscript" should be, I'd probably be able to get it working.

Code: Select all


VBSTART
'First Part
' Show simple directory tree

Option Explicit
Dim sArg, oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Get folder (default is current directory)
If Wscript.Arguments.Count > 0 Then
sArg = Wscript.Arguments(0)
Else
sArg = "."
End If
sArg = oFSO.GetAbsolutePathName(sArg)

' Process entire tree (if valid folder)
If oFSO.FolderExists(sArg) Then
Wscript.Echo "Folder tree for:", sArg
ShowTree "", oFSO.GetFolder(sArg)
End If

Set oFSO = Nothing
Wscript.Quit(0)


Sub ShowTree(sIndent, oFolder)
Dim oSubFolder, ix
ix = 1
For Each oSubFolder In oFolder.SubFolders
Wscript.Echo sIndent & "+--" & oSubFolder.Name
If ix <> oFolder.SubFolders.Count Then
ShowTree sIndent & "| ", oSubFolder
Else
ShowTree sIndent & " ", oSubFolder
End If
ix = ix + 1
Next
End Sub

'Second Part

Set fso = CreateObject("Scripting.FileSystemObject" )
Set file = fso.OpenTextFile("C:\ListFile.txt",2,1)
strFile = "c:\ListFile2.txt"
strLine = "This is a test."
Set objOutFile = fso.CreateTextFile(strFile,True)
objOutFile.Write(strLine)
VBEND

If you have an easier way of doing this, I sure would appreciate the help.
If you paste the first part into notepad and name it something.vbs, it works, but I wanted to get rid of the messages and write the result to a file.

Thanks for helping.
Windows 7

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2024) AND enjoy programming. (That's my little piece of heaven!)

The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Mon Oct 15, 2012 3:06 am

DOS has had a tree command for as long as I've been using it. Would you consider a step back in time?

Code: Select all

IfFileExists>%temp_dir%tree.txt
  DeleteFile>%temp_dir%tree.txt
EndIf

Let>INPUT_BROWSE=2
Input>BaseFolderForTree,Select a folder to be treed.

Let>RP_WINDOWMODE=0
Let>RP_WAIT=1
Run>cmd /c tree "%BaseFolderForTree%" > %temp_dir%tree.txt

Let>RP_WINDOWMODE=1
Let>RP_WAIT=0
Run>Notepad.exe %temp_dir%tree.txt

User avatar
PepsiHog
Automation Wizard
Posts: 517
Joined: Wed Apr 08, 2009 4:19 pm
Location: Florida

Hello

Post by PepsiHog » Mon Oct 15, 2012 12:48 pm

@JRL

I am working on concepts for writing a file explorer or something of that nature. I actually wrote a macro that will build a tree, but it's painfully slow. So I looked and found the VBScript and it is way faster. Just for more professional look, I would rather not need to rely on a dos window and dos command. Internal is just better.

I thought to mention about wanting it to be internal, but didn't. Guess I should have.

Any thoughts on the vbscript?

Thanks,
PepsiHog
Windows 7

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2024) AND enjoy programming. (That's my little piece of heaven!)

The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Oct 15, 2012 2:51 pm

WScript is the Windows Scripting Host.

The first place this is referred to is in WScript.Arguments.Count. What this is doing is looking for the arguments passed to the script on the command line.

You could replace all that with function variables.

I would put the code into a function and set up the parms you want to send to it, then you can get rid of the wscript.arguments stuff and just refer to the function definition parameters.

The other place it is referred to is with WScript.Echo. All this does is echo information back to the command line. You could probably get rid of this completely as it will just popup message boxes. I'm guessing that instead of doing this you would rather construct a string. So initialise a string at the start of the function then replace each Echo with the string being appended to with that value. Return this string as your function's result which Macro Scheduler can then use.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
PepsiHog
Automation Wizard
Posts: 517
Joined: Wed Apr 08, 2009 4:19 pm
Location: Florida

Drink Pepsi....or else

Post by PepsiHog » Wed Oct 17, 2012 2:26 am

@Marcus

Thanks for the help. I am starting to understand more about vbs. So many questions, just learning the Function command wasn't going to cut it. So I started looking up each command. I must be in the ZONE, because my brain kept up. :lol:

But I have a question.

In the above script one line is

Wscript.Quit(0)

(Ignor part 2, as if it's not there. I added that and it will be added to part1.)

OK, maybe two questions. First, since the only programming below this is a srt, is an exit needed?

Second, what is the command I would use in MS?

I did try just Quit(0), but with no luck.

Thanks again,
PepsiHog
Windows 7

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2024) AND enjoy programming. (That's my little piece of heaven!)

The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed Oct 17, 2012 11:55 am

If you're putting this into a Function then instead of WScript.Quit use Exit Function. If in a Sub use Exit Sub.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts