Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Directory log

Status
Not open for further replies.

scottohum

IS-IT--Management
Jul 5, 2011
14
0
0
GB
im looking for a VBscript that will list all directories, contents of C:\ ..... and save it as a .txt file.
Can anyone help please?
Thanks
Scott
 
What have YOU tried so far and where in YOUR code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
this is what i have...
but it seems to have a permisions problem:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForAppending = 2
Dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")

LogFile = "c:\list.log"
Dim objLogFile:Set objLogFile = objFSO.CreateTextFile(logfile, 2, True)

objStartFolder = "C:\"

Set objFolder = objFSO.GetFolder(objStartFolder)
objLogFile.Write objFolder.Path
objLogFile.Writeline
Set colFiles = objFolder.Files
For Each objFile in colFiles
objLogFile.Write objFile.Name
objLogFile.Writeline
Next


ShowSubfolders objFSO.GetFolder(objStartFolder)

Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
objLogFile.Write Subfolder.Path
objLogFile.Writeline
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
objLogFile.Write objFile.Name
objLogFile.Writeline
Next
ShowSubFolders Subfolder
Next
End Sub

objLogFile.Close
 
You really only need one function. Also, you'll want to move the files loop outside the folders loop.

You can use Writeline to write to the file, no need for both write and writeline.

Code:
Const ForAppending = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim objFSO
Dim objLogFile

strLogFile = "c:\list.log"
strStartFolder = "C:\"

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        objLogFile.Writeline Subfolder.Path
        ShowSubFolders Subfolder
    Next

    Set objFolder = objFSO.GetFolder(Subfolder.Path)
    Set colFiles = objFolder.Files
    For Each objFile in colFiles
        objLogFile.Writeline objFile.Name
    Next
End Sub

Set objLogFile = objFSO.CreateTextFile(strlogfile, 2, True)

ShowSubfolders objFSO.GetFolder(strStartFolder)

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
i get :-S:


---------------------------
Windows Script Host
---------------------------
Script: C:\Documents and Settings\Email User\Desktop\list.vbs
Line: 17
Char: 5
Error: Object required: 'Subfolder'
Code: 800A01A8
Source: Microsoft VBScript runtime error

---------------------------
OK
---------------------------
 
Line 17

Set objFolder = objFSO.GetFolder([red]folder[/red].Path)

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Geates, why not simply this for the Sub ?
Code:
Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        objLogFile.Writeline Subfolder.Path
        ShowSubFolders Subfolder
    Next
    For Each objFile in Folder.Files
        objLogFile.Writeline objFile.Name
    Next
End Sub
 
Personally, I would do it that way but I wanted to maintain the OPs original code so he could make that affirmation.

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Your help is much appreciated guys!
sadly it got as far as the administrator folder and it came up with an access denied error.

But this script worked for me in the end:

Option Explicit
Const FOR_READING = 1
Const FOR_WRITING = 2
Const FOR_APPENDING = 8

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim Folder, FolderItem, f, LogFile
Dim strDirectory : strDirectory = "C:\"
Set Folder = objFSO.GetFolder(strDirectory)
LogFile = "C:\FilesList.txt"
Set f = objFSO.CreateTextFile(LogFile, 2)

getDirList Folder, f
f.Close
Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "notepad " & LogFile

Sub getDirList(pCurrentDir, f)
On Error Resume Next
Dim aItem, bItem
For Each aItem In pCurrentDir.Files
If Err.Number = 424 Or Err.Number = 70 Then
Err.Clear
Else
f.Writeline(aItem)
End If
Next
 
This is likely to happen (from the perspective of my environment). Also, if you are running Windows 7, you will also likely encounter a runaway execution or run out of memory as the Application Data folder seems to be a link to itself.

I would get rid of the OERN (On Error Resume Next). A script should not trap and bypass errors but rather not encounter them in the first place. Although, given the nature of this script, including OERN is mostly justified.

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Hi ! The following script can list Folders and sub-Folders with their sizes and their paths and the output result is in HTML Table
Code:
Dim ShellO: Set ShellO = CreateObject("WScript.Shell")
Dim FSO: Set FSO = CreateObject("Scripting.FileSystemObject")
Dim SListe: Dim Schemin
'Dossier à traiter
 
Schemin = InputBox("Entrez le chemin Absolu du dossier à lister "&vbCrlf&"Exemple c:\Program Files "&vbCrlf &_
"ou bien la Lettre du Lecteur exemple C:\ ou bien D:\","Arboréscence + Taille Dossier","c:\")
If Schemin = "" Then WScript.Quit 
'Dossier Bureau de windows + "\"
SListe = ShellO.SpecialFolders("Desktop")
If Right(SListe, 1) <> "\" Then SListe = SListe & "\"
'Ouverture du fichier contenant l'arborescence du répertoire à traiter vers le Bureau
Dim Fichier: Set Fichier = FSO.CreateTextFile(SListe & "Liste.html", 1, True) 
 
strHTML="<html><body text=white><style type='text/css'>"&_
"a:link {color: #F19105;}"&_
"a:visited {color: #F19105;}"&_
"a:active {color: #F19105;}"&_
"a:hover {color: #FF9900;background-color: rgb(255, 255, 255);}"&_
"</style>"
 
strHTML=strHTML & "<center><h2><B><font color=red>Liste des Dossiers et Sous-Dossiers dans " & Schemin & " et leurs tailles </font></B></h2></center>" & _
 "<center><body bgcolor=#1234568><table border='3' cellpadding='1' style='border-collapse: collapse; font size:11pt' bordercolor='#CCCCCC' width='auto' id='Table1'></center>" & 

_
 "<tr><td><center><strong>Chemin des Dossiers :</strong></center></td>" &_
 "<td><center><strong>Taille :</strong></center></td></tr>"
'Fichier.WriteLine (Schemin & "<br>")			  
Fichier.WriteLine strHTML 'Ecrire la structure du Tableau en HTML
ListerDossier Schemin, Fichier 'Remplissage dynamique des données dans le Tableau 
Fichier.WriteLine "</table></body></html>" 'ici on ferme notre tableau par la balise </table>
'Fermeture du fichier contenant l'arborescence du répertoire à traiter
Fichier.Close

Call Explorer("Liste.html")
 
Function ListerDossier(Schemin, Fichier) 'Lister l'arborescence du dossier
On Error Resume Next
Dim FSO: Set FSO = CreateObject("Scripting.FileSystemObject")
Dim ObjRep: Set ObjRep = FSO.GetFolder(Schemin) 'dossier
Dim ObjSubRep: Set ObjSubRep = ObjRep.SubFolders 'sous-dossiers
Dim ObjSubRepItem
For Each ObjSubRepItem In ObjSubRep 'Traiter chaque sous-dossiers
 
Set f = fso.GetFolder(ObjSubRepItem)
SizeKo = Round(FormatNumber(f.Size)/(1024),0) & " Ko" 'Taille en Ko avec 2 chiffres après la Virgule
SizeMo = Round(FormatNumber(f.Size)/(1048576),0) & " Mo"'Taille en Mo avec 2 chiffres après la Virgule
SizeGo = Round(FormatNumber(f.Size)/(1073741824),0) & " Go" 'Taille en Go avec 2 chiffres après la Virgule
 
If f.size < 1024 Then 
Fichier.WriteLine ("<tr><td><a href='" & ObjSubRepItem.Path & "'>" & ObjSubRepItem.Path & "</td><td>" & f.size & " Octet </a></td></tr>")
elseif f.size < 1048576 Then 
Fichier.WriteLine ("<tr><td><a href='" & ObjSubRepItem.Path & "'>" & ObjSubRepItem.Path & "</td><td>" & SizeKo & "</a></td></tr>")
elseif f.size < 1073741824 Then 
Fichier.WriteLine ("<tr><td><a href='" & ObjSubRepItem.Path & "'>" & ObjSubRepItem.Path & "</td><td>" & SizeMo & "</a></td></tr>") 
else
Fichier.WriteLine ("<tr><td><a href='" & ObjSubRepItem.Path & "'>" & ObjSubRepItem.Path & "</td><td>" & SizeGo & "</a></td></tr>") 
end if
 ListerDossier ObjSubRepItem.Path, Fichier 'traiter les sous-dossiers
Next
End Function

Function Explorer(File)
set ws = CreateObject("wscript.shell")
ws.Run "iexplore " & File
end Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top