Jump to content

export contents of a drive to a txt file


Recommended Posts

hello,

i can export the contents of a drive with batch scripting using > dir /B for bare heading format but i have a lot of drives and have to create a seperate script for each drive so my question is this..

how can i make a .vbs script (or other) to check for a drive and if it exists export its contents to a text file in bare format?

thank you for your time :)

Link to comment
Share on other sites


Here a vbs script that list all the files and folder on a drive, this leaves a text file where it was ran from.

I will add a loop to it later to go threw all local hardd drives later if you want.

Dim Act, CD, CName, colFiles, Ct1, Ct2,FileRpt, Fso, fVar1, fVar2, GB, Ln1, MB, objF, Ts, UName
Dim Subfolder , SubFDC, SubFDLA, SubFDrv, SubFDLM, SubFName, SubFPath, SubFType, ZCD
Dim Time1, Time2, Time3
Time1 = Timer()
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Act = CreateObject("Wscript.Shell")
CD = Act.CurrentDirectory
ZCD = CD : ZCD = Split(ZCD,":")
CName = Act.ExpandEnvironmentStrings("%ComputerName%")
UName = Act.ExpandEnvironmentStrings("%UserName%")
Ln1 = Space(2) & Chr(171) & " ----------- " & Chr(187) & Space(2)
MB = 1024 * 1024 : GB = MB * MB
FileRpt = (ZCD(0) & "_" & UName & "_DirList.Txt")
Ct1 = 0 : Ct2 = 0
Set Ts = Fso.CreateTextFile(FileRpt)
Ts.WriteLine Now() & Ln1 & UName & Ln1
' Ts.WriteBlankLines(2)
'''' START THE SEARCH THREW THE PARENT FOLDER AND SUB FOLDER
ShowSubfolders Fso.GetFolder(CD)
FinishScript
Function ShowSubFolders(Folder)
On Error Resume Next
For Each Subfolder in Folder.SubFolders
Ct1 = Ct1 + 1
SubFDrv = "Folder Drive : " & Subfolder.Drive
SubFPath = "Folder Path : " & Subfolder.Path
SubFName = "Folder Name : " & Subfolder.Name
SubFDC = "Folder Date Created : " & Subfolder.DateCreated
SubFDLA = "Folder Date Accessed : " & Subfolder.DateLastAccessed
SubFDLM = "Folder Date Modified : " & Subfolder.DateLastModified
SubFType = "Folder Type : " & Subfolder.Type
Set colFiles = Subfolder.Files
MakeReport
ShowSubFolders Subfolder
Next
Exit Function
End Function
''''
Function MakeReport
Set colFiles = Subfolder.Files
For Each objF in colFiles
Ct2 = Ct2 + 1
fVar1 = objF.Size
ConvertFileSize
Ts.WriteLine SubFDrv & vbCrLf & SubFPath & vbCrLf & SubFName & vbCrLf & SubFDC
Ts.WriteLine SubFDLA & vbCrLf & SubFDLM & vbCrLf & SubFType
Ts.WriteLine "File Path : " & objF.Path
Ts.WriteLine "File Name : " & objF.Name
Ts.WriteLine fVar1
Ts.WriteLine "File Date Created : " & objF.DateCreated
Ts.WriteLine "File Date Modified : " & objF.DateLastModified
Ts.WriteLine "File Date Accessed : " & objF.DateLastAccessed
Ts.WriteLine "Folder Type : " & objF.Type
Ts.WriteLine Ln1 & "Folder Counter : " & Ct1 & Ln1 & "File Counter : " & Ct2
Ts.WriteBlankLines(2)
Next
Exit Function
End Function
''''
Function ConvertFileSize
If objF.Size < 999999 Then
fVar1 = Left(objF.Size,3)
fVar1 = "File Size : " & fVar1 & " KB"
End If
If objF.Size > 1000000 Then
fVar1 = objF.Size /MB : fVar1 = Left(fVar1,4)
fVar1 = "File Size : " & fVar1 & " MB"
End If
If objF.Size > 1000000000 Then
fVar1 = objF.Size /GB : fVar1 = Left(fVar1,4)
fVar1 = "File Size : " & fVar1 & " GB"
End If
Exit Function
End Function
''''
Function FinishScript
Time2 = Timer()
Time3 = Time1 - Time2
Time3 = Replace(Time3,"-","")
If Time3 < 60 Then
Time3 = Left(Time3,4)
Time3 = Time3 & " Seconds"
Else
Time3 = Time3 / 60
Time3 = Left(Time3,4)
Time3 = Time3 & " Minutes"
End If
Ts.WriteLine Ln1 & "Folder Total : " & Ct1
Ts.WriteLine Ln1 & "File Total : " & Ct2
Ts.WriteLine Ln1 & "Script Time : " & Time3
Ts.Close
'''' ASK IF USER WANT TO OPEN FILE
Q1 = Act.Popup (Space(7) & "Completed Search" & vbCrLf & "Folder Total : " & Ct1 & vbCrLf &_
"File Total : " & Ct2 & vbCrLf & "Script Time : " & Time3 & vbCrLf &_
"Did You Want To Run This File?" & vbCrLf & FileRpt & vbCrLf &_
"If Nothing Is Selected In The" & vbCrLf & "Defualt Time Out, Of 5 Minute" & vbCrLf &_
"This Will Then Close And Exit", 301,"Completed Script", 4 + 64)
If Q1 = VbYes Then : Act.Run(FileRpt) : End If
If Q1 = -1 Then : On Error Goto 0 : End If
Exit Function
End Function

Link to comment
Share on other sites

quite simalerly to a cd drive finding loop this can be done with a single line:

FOR %%i IN (C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO IF EXIST %%i:\* dir %%i: /b >> %systemdrive%\listforme.txt:

*all on one line

this puts the output in the file listforme.txt on your main drive, beware this will also list the contents of cds and floppys if you have them in their drives at the time. if you dont want that remove the drive letter from the parentheses

Link to comment
Share on other sites

Here is a the VBs script that will search all hard drives on the local computer

Save as ListAll.Vbs

What I added to the original script I posted. This only checks to see if it a hard drive, floppy drives and CD or DVD drives do not get check with this loop
For Each strDrv In Drv 
If strDrv.DriveType = 2 Then
ShowSubfolders Fso.GetFolder(strDrv)
End If
Next

Dim Act, CD, CName, colFiles, Ct1, Ct2,FileRpt, Fso, fVar1, fVar2, GB, Ln1, MB, objF, Ts, UName
Dim Subfolder , SubFDC, SubFDLA, SubFDrv, SubFDLM, SubFName, SubFPath, SubFType, ZCD
Dim Time1, Time2, Time3
Time1 = Timer()
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Act = CreateObject("Wscript.Shell")
CD = Act.CurrentDirectory
ZCD = CD : ZCD = Split(ZCD,":")
CName = Act.ExpandEnvironmentStrings("%ComputerName%")
UName = Act.ExpandEnvironmentStrings("%UserName%")
Ln1 = Space(2) & Chr(171) & " ----------- " & Chr(187) & Space(2)
MB = 1024 * 1024 : GB = MB * MB
FileRpt = (CName & "_DirList.Txt")
Ct1 = 0 : Ct2 = 0
Set Ts = Fso.CreateTextFile(FileRpt)
Ts.WriteLine Now() & Ln1 & UName & Ln1
' Ts.WriteBlankLines(2)
'''' START THE SEARCH THREW THE PARENT FOLDER AND SUB FOLDER
Dim Drv, strDrv
Set Drv = Fso.Drives
For Each strDrv In Drv
If strDrv.DriveType = 2 Then
ShowSubfolders Fso.GetFolder(strDrv)
End If
Next
FinishScript
Function ShowSubFolders(Folder)
On Error Resume Next
For Each Subfolder in Folder.SubFolders
Ct1 = Ct1 + 1
SubFDrv = "Folder Drive : " & Subfolder.Drive
SubFPath = "Folder Path : " & Subfolder.Path
SubFName = "Folder Name : " & Subfolder.Name
Set colFiles = Subfolder.Files
MakeReport
ShowSubFolders Subfolder
Next
Exit Function
End Function
''''
Function MakeReport
Set colFiles = Subfolder.Files
For Each objF in colFiles
Ct2 = Ct2 + 1
fVar1 = objF.Size
ConvertFileSize
Ts.WriteLine SubFDrv & vbCrLf & SubFPath & vbCrLf & SubFName
Ts.WriteLine "File Name : " & objF.Name
Ts.WriteLine fVar1
Ts.WriteLine "File Date Created : " & objF.DateCreated
Ts.WriteLine "File Date Modified : " & objF.DateLastModified
Ts.WriteLine "File Date Accessed : " & objF.DateLastAccessed
Ts.WriteLine Ln1 & "Folder Counter : " & Ct1 & Ln1 & "File Counter : " & Ct2
Ts.WriteBlankLines(1)
Next
Exit Function
End Function
''''
Function ConvertFileSize
If objF.Size < 999999 Then
fVar1 = Left(objF.Size,3)
fVar1 = "File Size : " & fVar1 & " KB"
End If
If objF.Size > 1000000 Then
fVar1 = objF.Size /MB : fVar1 = Left(fVar1,4)
fVar1 = "File Size : " & fVar1 & " MB"
End If
If objF.Size > 1000000000 Then
fVar1 = objF.Size /GB : fVar1 = Left(fVar1,4)
fVar1 = "File Size : " & fVar1 & " GB"
End If
Exit Function
End Function
''''
Function FinishScript
Time2 = Timer()
Time3 = Time1 - Time2
Time3 = Replace(Time3,"-","")
If Time3 < 60 Then
Time3 = Left(Time3,4)
Time3 = Time3 & " Seconds"
Else
Time3 = Time3 / 60
Time3 = Left(Time3,4)
Time3 = Time3 & " Minutes"
End If
Ts.WriteLine Ln1 & "Folder Total : " & Ct1
Ts.WriteLine Ln1 & "File Total : " & Ct2
Ts.WriteLine Ln1 & "Script Time : " & Time3
Ts.Close
'''' ASK IF USER WANT TO OPEN FILE
Q1 = Act.Popup (Space(7) & "Completed Search" & vbCrLf & "Folder Total : " & Ct1 & vbCrLf &_
"File Total : " & Ct2 & vbCrLf & "Script Time : " & Time3 & vbCrLf &_
"Did You Want To Run This File?" & vbCrLf & FileRpt & vbCrLf &_
"If Nothing Is Selected In The" & vbCrLf & "Defualt Time Out, Of 5 Minute" & vbCrLf &_
"This Will Then Close And Exit", 301,"Completed Script", 4 + 64)
If Q1 = VbYes Then : Act.Run(FileRpt) : End If
If Q1 = -1 Then : On Error Goto 0 : End If
Exit Function
End Function

Link to comment
Share on other sites

  • 2 weeks later...

Easiest way would be the batch file:

for %a in ( A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ) do dir %a:\ /a /b /s >> c:\filelist.txt

(Edit the batch file to only include the drives you want)

Link to comment
Share on other sites

This is how I would do it…

ListEm.cmd

@ECHO OFF &SETLOCAL ENABLEEXTENSIONS
FOR /F %%? in ('MOUNTVOL ^|FIND ":\" ^|FIND /V "A:\"') DO IF EXIST %%?* (CALL :SUB %%?)
ENDLOCAL &GOTO :EOF

:SUB
SET "LET=%1"
SET "OUT=%1_%LET:~0,1%_LIST_.TXT"
DIR/B/ON/A-D-H %1 >%OUT% 2>NUL ||TYPE NUL>%OUT%
FOR /F "DELIMS=" %%? IN ('DIR/B/S/ON/AD-H %1 ^2^>NUL ^|SORT') DO IF ERRORLEVEL 0 (
DIR/B/ON/A-D-H %%? >NUL 2>&1 &&(
>>%OUT% ECHO/
>>%OUT% ECHO/%%?
>>%OUT% DIR/B/ON/A-D-H %%?
)
)
GOTO :EOF

The file could be shortened a little, but I prefer the safeguards and format of the resulting files!

Each drive will hold the resulting directory listing text file at its root

<Edit>

Note

This version deliberately ignores hidden files and folders.

</Edit>

Edited by Yzöwl
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...