|
-
July 23rd, 2007, 06:11 PM
#1
Counting files in a directory
I've exhausted my knowledge and ability to locate the answer on the net.. I need your help..... :'(
Im trying to write a program (obviously), that goes through a directory and records the name of every file, and prints it to a text file.
There's enough code to the program to not post it here, but if anyone needs to see it, i'll gladly put it up on 1 of those free code sharing sites (if i can remember the address...)
Basically, all of the program is done, except for the writing file names to a text file part :P
The program opens the file, and will write anything I specifically tell it to.
I know its browsing to the directory I want it to record, and the output directory is working as well. it just doesn't count the number of files, nor does it store file names into a variable.. But if I say "print #1, "Test"" it works fine.
My way of doing it is a for next loop. Its supposed to count the number of files (not folders) in a directory, and for i = 0 to (number of files in directory)
now since I want the program to be generic, and not have to manually enter the number of files... I need to know how to make the program count the number of files upon the "cmdWrite_click" event
and also I need to know how to put a file name into a variable.
if you have any ideas, please share!!!
Thanks guys
-
July 23rd, 2007, 06:19 PM
#2
Re: Counting files in a directory
Post the part of the code that doesn't work. You could use FSO, or plain VB code. Not hard.
-
July 24th, 2007, 01:09 PM
#3
Re: Counting files in a directory
 Originally Posted by dglienna
Post the part of the code that doesn't work. You could use FSO, or plain VB code. Not hard.
I dont really have any code that "Doesn't work".
For the part im stuck at, I dont know where to begin :S
Here's some Psudo-Code:
=============================
open fileX for append
For i = 0 to (total number of files in directory)
get file(i) name, write to fileX
next i
close fileX
=============================
Like I said above, The program browses to the directory, stores the directory in a variable, and also stores the output directory in a variable (where the file is to be opened and saved).
The psudo-code is to open a file, and for each file it finds, its to write the name in a txt file.
I also need it to go into all subdirectories, and each of their subdirectories, till it gets EVERY file in the chosen directory.
example:
C:\Test
file1
file2
file3
folder1
file4
file5
folder2
folder3
folder4
file6
file7
so the output of file.txt should be:
file1
file2
file3
file4
file5
file6
file7
-
July 24th, 2007, 02:19 PM
#4
Re: Counting files in a directory
You could run the DOS command "dir /b /s >>filename.txt". This would result in a file listing all of the filenames in the directory tree:
C:\WINNT\FONTS
C:\WINNT\system32
C:\WINNT\test.txt
C:\WINNT\FONTS\tahoma.ttf
C:\WINNT\FONTS\tahomabd.ttf
C:\WINNT\system32\12520437.cpx
C:\WINNT\system32\12520850.cpx
C:\WINNT\system32\comcat.dll
C:\WINNT\system32\crviewer.dll
C:\WINNT\system32\dbnmpntw.dll
C:\WINNT\system32\drivers
C:\WINNT\system32\drvssrvr.hlp
C:\WINNT\system32\ds16gt.dll
C:\WINNT\system32\ds32gt.dll
C:\WINNT\system32\HLINK.DLL
C:\WINNT\system32\hlink.srg
C:\WINNT\system32\HLINKPRX.DLL
C:\WINNT\system32\MFCANS32.DLL
C:\WINNT\system32\misc.srg
C:\WINNT\system32\MISC2.SRG
C:\WINNT\system32\mscpxl32.dll
C:\WINNT\system32\MSJET35.DLL
C:\WINNT\system32\MSJINT35.DLL
C:\WINNT\system32\MSJTER35.DLL
C:\WINNT\system32\msoffice.srg
C:\WINNT\system32\MSRD2x35.DLL
C:\WINNT\system32\MSREPL35.DLL
C:\WINNT\system32\mstext35.dll
C:\WINNT\system32\odbc16gt.dll
C:\WINNT\system32\odbc32.dll
C:\WINNT\system32\odbc32gt.dll
C:\WINNT\system32\odbcad32.exe
C:\WINNT\system32\odbccp32.cpl
C:\WINNT\system32\odbccp32.dll
C:\WINNT\system32\odbccr32.dll
C:\WINNT\system32\odbcinst.hlp
C:\WINNT\system32\odbcint.dll
C:\WINNT\system32\odbcjet.hlp
C:\WINNT\system32\odbcji32.dll
C:\WINNT\system32\odbcjt32.dll
C:\WINNT\system32\odbcjtnw.hlp
C:\WINNT\system32\odbctl32.dll
C:\WINNT\system32\odbctrac.dll
C:\WINNT\system32\oleaut32.dll
C:\WINNT\system32\SELFREG.DLL
C:\WINNT\system32\sqlsrv32.dll
C:\WINNT\system32\stdole2.tlb
C:\WINNT\system32\URLMON.DLL
C:\WINNT\system32\W95FIBER.DLL
C:\WINNT\system32\wininet.dll
C:\WINNT\system32\WINSSPI.DLL
C:\WINNT\system32\drivers\etc
C:\WINNT\system32\drivers\etc\Hosts
Then you could open the file and count the number of lines.
-
July 24th, 2007, 04:03 PM
#5
Re: Counting files in a directory
Well, that'd take a long time with 400K files in a folder.
Why do you need a list of filenames? Just use a filepicker to choose the one that you want.
-
July 25th, 2007, 07:07 AM
#6
Re: Counting files in a directory
Well I mean his program would loop thru and count the files (lines). I hope you didn't think I was suggesting he manually count them!
-
July 25th, 2007, 07:55 AM
#7
Re: Counting files in a directory
Here is a code that uses FileSystemObject to count files in a folder C:, and to show a list of files. Maybe it can help.
Code:
Private Sub Form_Load()
Call ShowFileList("C:")
End Sub
Sub ShowFileList(folderspec)
Dim fs, f, f1, fc, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.Files
Call MsgBox("Number of files: " & fc.Count)
For Each f1 In fc
s = s & f1.Name
s = s & vbCrLf
Next
MsgBox s
End Sub
-
July 25th, 2007, 09:46 AM
#8
Re: Counting files in a directory
The following code is meant to run in a form. It uses FSO to recursively scan all files in all subfolders of a starting path and write the names out to a given file.
Code:
Option Explicit
Private FSO As New FileSystemObject
Private FileCount As Long
Private Sub ScanFolder(Fold As Folder, OutFile As Integer)
Dim fi As File
Dim fo As Folder
For Each fi In Fold.Files
Print #OutFile, fi.Path
FileCount = FileCount + 1
Next
For Each fo In Fold.SubFolders
ScanFolder fo, OutFile
Next
End Sub
Private Function CountAndWrite(SearchPath As String, OutFileName As String) As Long
Dim FNum As Integer
Dim fld As Folder
FNum = FreeFile
Set fld = FSO.GetFolder(SearchPath)
If fld Is Nothing Then Exit Function
Open OutFileName For Append As #FNum
FileCount = 0
ScanFolder fld, FNum
Close #FNum
FreeFile FNum
CountAndWrite = FileCount
End Function
Private Sub btnStart_Click()
Dim c As Long
' specify your own output file and starting folder here
c = CountAndWrite("C:\starting\path", app.path & "\files.txt")
End Sub
Oh yes: set a reference to 'Microsoft Scripting Runtime' to use the FileSystemObject.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|