|
Option Explicit
' https://www.codeproject.com/articles/553/vbscript-directory-crawler
' Usage:
' cscript.exe /nologo Tree.vbs %temp%
Call CheckAndReopenConsole()
Dim sTargetFolder : sTargetFolder = "." ' Default to current path
If WScript.Arguments.Count >= 1 Then sTargetFolder = WScript.Arguments(0)
' WScript.Echo VbCrLf & "/" & Replace(Space(78), " ", "*") & "\" & VbCrLf
' Call GetWorkingFolderRecursion1(sTargetFolder, 0, True, "|", "")
' WScript.Echo VbCrLf & "\" & Replace(Space(78), " ", "*") & "/" & VbCrLf
' WScript.Echo VbCrLf & VbCrLf
WScript.Echo VbCrLf & "/" & Replace(Space(78), " ", "*") & "\" & VbCrLf
Call GetWorkingFolderRecursion2(sTargetFolder, 0, True, "")
WScript.Echo VbCrLf & "\" & Replace(Space(78), " ", "*") & "/" & VbCrLf
Call Pause()
' ------------------------------------------------------------------------------
Sub CheckAndReopenConsole
If UCase(Right(WScript.FullName, 12)) <> "\CSCRIPT.EXE" Then
Dim sArgs : sArgs = " "
Dim sArg, sCMD
' WScript.Echo WScript.Arguments.Count
If WScript.Arguments.Count > 0 Then
For Each sArg in WScript.Arguments
sArgs = sArgs & sArg & " "
Next
End If
sCMD = "CSCRIPT.EXE
CreateObject("WScript.Shell").Run(sCMD)
WScript.Quit 911
End If
End Sub
' ------------------------------------------------------------------------------
Sub Pause()
WScript.Echo ("Press Enter to continue")
Do
Loop Until WScript.StdIn.Read(1) <> ""
End Sub
' ------------------------------------------------------------------------------
function GetWorkingFolderRecursion1(sCurrentFolder, iFolderCount, bInitialRun, sSpacer, sLeftIndication)
Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim oFolder : Set oFolder = oFSO.GetFolder(sCurrentFolder)
Dim oSubFolders : Set oSubFolders = oFolder.SubFolders
If bInitialRun = True then
WScript.Echo oFolder.name
sLeftIndication = ""
iFolderCount = oSubFolders.count
bInitialRun = False
End If
Dim iRemainingCount : iRemainingCount = oSubFolders.count
Dim oSubFolder
For Each oSubFolder In oSubFolders
sLeftIndication = sLeftIndication & Space(3) & sSpacer
WScript.Echo sLeftIndication & "-- " & oSubFolder.name
If iRemainingCount = 1 then
sLeftIndication = Left(sLeftIndication, Len(sLeftIndication) - 1)
sLeftIndication = sLeftIndication & " "
End If
iRemainingCount = GetWorkingFolderRecursion1 (sCurrentFolder & "\" & oSubFolder.name, iRemainingCount, bInitialRun, sSpacer, sLeftIndication)
Next
If Len(sLeftIndication) > 3 then
sLeftIndication = Left(sLeftIndication, Len(sLeftIndication) - 4)
End If
Set oFSO = nothing
GetWorkingFolderRecursion1 = iFolderCount - 1
End function
' ------------------------------------------------------------------------------
function GetWorkingFolderRecursion2(sCurrentFolder, iFolderCount, bInitialRun, sLeftIndication)
' Requires CMD/Terminal/Console to be using a better font such as Lucida Console
Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim oFolder : Set oFolder = oFSO.GetFolder(sCurrentFolder)
Dim oSubFolders : Set oSubFolders = oFolder.SubFolders
If bInitialRun = True then
WScript.Echo oFolder.name
sLeftIndication = ""
iFolderCount = oSubFolders.count
bInitialRun = False
End If
Dim iRemainingCount : iRemainingCount = oSubFolders.count
Dim sShowRemainingCount : sShowRemainingCount = "" ' " [" & iRemainingCount & "]"
Dim oSubFolder
For Each oSubFolder In oSubFolders
Select Case iRemainingCount
Case 2
sLeftIndication = Replace(sLeftIndication, ChrW(9500), ChrW(9474)) & Space(3) & ChrW(9500)
WScript.Echo sLeftIndication & ChrW(9472) & Chr(32) & oSubFolder.name & sShowRemainingCount
sLeftIndication = Left(sLeftIndication, Len(sLeftIndication) - 1)
sLeftIndication = sLeftIndication & ChrW(9500)
Case 1
sLeftIndication = Replace(sLeftIndication, ChrW(9500), ChrW(9474)) & Space(3) & ChrW(9492)
WScript.Echo sLeftIndication & ChrW(9472) & Chr(32) & oSubFolder.name & sShowRemainingCount
sLeftIndication = Left(sLeftIndication, Len(sLeftIndication) - 1)
sLeftIndication = sLeftIndication & Chr(183)
Case Else
sLeftIndication = Replace(sLeftIndication, ChrW(9500), ChrW(9474)) & Space(3) & ChrW(9500)
WScript.Echo sLeftIndication & ChrW(9472) & Chr(32) & oSubFolder.name & sShowRemainingCount
End Select
iRemainingCount = GetWorkingFolderRecursion2 (sCurrentFolder & "\" & oSubFolder.name, iRemainingCount, bInitialRun, sLeftIndication)
Next
If Len(sLeftIndication) > 3 then
sLeftIndication = Left(sLeftIndication, Len(sLeftIndication) - 4)
End If
Set oFSO = nothing
GetWorkingFolderRecursion2 = iFolderCount - 1
End function
|
|
|
|
|
So thanks for sixteen years of useful service (Microsoft manages to get some things right first time around).
One thing though, and if I missed it in the text above please forgive me I seemed to have crushed my x-ray specks last night and can only see out a remaining corner, without a mention of CSCRIPT.EXE running this, one will be hardpressed to explain why he has to sit before his monitor and depress the "OK" button too get all the dismissals of the windows messages displayed. The ECHO loop does end. Eventually.
The explanation as to what is seen in the image included is that this "console" is at the behest of Cscript, not Wscript.
So -> cscript.exe bertha_index.vbs c:\z > c:\tree.txt
|
|
|
|
|
Is there a way I can examine office document properties (The internal ones such as Author, Comments etc) using a vbscript?
I can't find any reference on the MS VBScript site for any FSO Object that might help
|
|
|
|
|
'VBScript file to get the author and comments
'Tested with Word 2000
Dim strAuthor, strComments
Set objWord = CreateObject("Word.Application") 'Open an instance of Word
'Open an word document and return the "Documents" object
Set objDoc = objWord.Documents.Open("D:\Debug\Test1.doc")
strAuthor = objWord.ActiveDocument.BuiltInDocumentProperties(3) 'wdPropertyAuthor
strComments = objWord.ActiveDocument.BuiltInDocumentProperties(5) 'wdPropertyComments
MsgBox "This document's author is " & strAuthor & vbCrLf &" Comments: " & strComments
'msgbox objWord.Version
'To avoid the "Do you want to save your changes?" dialog box, the script
'sets the Saved property to TRUE before terminating Word.
objDoc.Saved = TRUE
objWord.Quit
=========================
Do not look into FSO because they can only read the binary format of word.
Use COM to access method of word
Refer MSDN for reference
|
|
|
|
|
Very intrigued with the code, however, when running it on a Win2000 system, (logged on user has Admin permissions), the program runs but dies apparently when it hits a "system directory" Error message is:
d:\_script\bertha_index.vbs(58,2) Microsoft VBScript runtime error: Permission denied
The last directory displayed was "System Volume Information" which is a "system directory".
Any suggestions on how to bypass system or hidden directories to preclude this error?
Again, enjoyed the scripts concept, and hope to hear from you in the near future.
Thanks for your time and efforts.
Doug Cranston
|
|
|
|
|
To bypass, you can use the attributes property on the folder object to see if it a system folder or not, and skip as needed.
js
|
|
|
|
|
Thanks for your response...Been a long time.. Will try out your concept, maybe this weekend, if my to do list from my better half lets me.
Have a great day.
Dougc
|
|
|
|
|
It's failing because that folder is only accessible to the SYSTEM user by default. Modify the security settings so that the Administrator has full access rights to the folder, and after applying them, you'll be able to go into that directory as well. This particular issue has caused me a few headaches.
|
|
|
|
|
I have a html-page where i want to display dynamicly all pictures
from a certain directory.
Someone any idea's howto do this
PBA
|
|
|
|
|
Hi
I have a question
How can I do a filesearch using VBScript
say for example if I give c:\test\*.txt
as input it should list all the .txt file
under test directory.
Can anyone help me?
Regards
Gnanave
|
|
|
|
|
Not sure if this might be a supplement to help you, worth a shot. Check out MS (HOWTO: Search Directories to Find or List Files)
I used some of the sample code from this page and tweaked it to build a tool to retrieve all files within a directory (& subs) into an Access database... it was VERY helpful. As it searches (and stores if you like) the files, you can split the extension to retieve only those files. You can also use this to exclude file-types within your search. The code is VB, but i used in Access just to make my life easier to store into a Database. Email me & i'll send you the tool, if you like (Access 97). Good luck.
http://support.microsoft.com/support/kb/articles/Q185/4/76.ASP
|
|
|
|
|
I like it, it is a nice introduction to the use of FileSystemObject with VBScript.
I started playing with the code, here is a small patch:
dim sf, fileColl, line
for each sf in foldcol
spacer = spacer + space(3) + "|"
'wscript.echo spacer + "-- " + sf.name
' If you wanted to show the number of bytes, use these lines instead of above
set fileColl = sf.Files
line = spacer + "-- " + sf.name
if fileColl.Count = 0 then
wscript.echo line + " (empty)"
else
wscript.echo line + _
" (uses " + cstr(FormatNumber(sf.Size, 0)) + " bytes with " + _
cstr(FormatNumber(fileColl.Count, 0)) + " files)"
end if
I believe you should have no problem locating my patch in the source...
Notice the use of '_' to continue a line (I don't know if it well documented), and the use of the Count property to have the number of elements in a collection (I couldn't locate this property in the MSDN doc!).
Of course, you can use the fileColl to list the files or do something on them.
Another trick for newbies like me:
you can run the script by double-clicking on it, of course, but if you want to run it from a command line, you can either type:
start vbscript_prog.vbs (with same result as double-clicking, ie. having each echo in a message box)
wscript vbscript_prog.vbs (idem)
or
cscript /nologo vbscript_prog.vbs (with output on the standard output, so can be redirected to a file)
Try cscript /? for more options.
|
|
|
|
|