Files and Folders
I know you can open a file and read it line by line of course but I was trying to find out if it's possible to open a folder and then read each individual file so that you could get the date it was created or possibly just once you open the folder just read each file.
# 1 Re: Files and Folders
You could do either one. What are you trying to accomplish?
# 2 Re: Files and Folders
First I want to read all files data paths and creation dates from a particular folder into a separate text file. I want to then check that text file later on and delete the files within it that are a month old.
# 3 Re: Files and Folders
This should get you started:
Option Explicit
'
' Add a reference to Microsoft Scripting Runtime
Sub GetFileInfo(strPath As String)
' On Error Resume Next
Dim s1 As String
Dim f As Object, fso As New FileSystemObject
Set f = fso.GetFolder(strPath)
s1 = f
MsgBox s1 & vbCrLf & _
"Date Created " & f.DateCreated & vbCrLf & _
"Last Modified " & f.DateLastModified & vbCrLf & _
"Last Accessed " & f.DateLastAccessed
End Sub
Private Sub Form_Activate()
GetFileInfo "c:\windows\"
End Sub
It shows them in a msgbox, but it does the timestamp.
# 4 Re: Files and Folders
Ok thank you. I believe what I am seeing is the date and time of the creation of the folder. Now how would I store the date of the files in that folder and if I can do this I can store them as strings, correct. Also how would I be able to compare these dates since they are strings.
Sorry I'm just very new to Visual Basic and trying to get used to it after essentially only using C++ to program.
# 5 Re: Files and Folders
Alright instead of the GetFolder Method I can simply use GetFile. Do you know how I can compare the dates and is there a way to read through a folder as you can read through a file like with EOF().
Thanks
# 6 Re: Files and Folders
Something like this. Then you could just write str to a file, and then read in, and split by lines, and then comma's to get the date info.
You could load each one into an array, if you need to.
Dim Str as string
str = str & s1 & _
", " & f.DateCreated & _
"," & f.DateLastModified & _
"," & f.DateLastAccessed & vbCrLf
# 7 Re: Files and Folders
Code:
Function GetFileInfo(strPath As String)
Open "z:\location.txt" For Output As #1
Dim filePath, fileDate As String
Dim days, limit As Integer
limit = 5
Dim f As Object, fso As New FileSystemObject
Dim FileList As Files, currFile As File
Set f = fso.GetFolder(strPath)
'Set f = fso.GetFile(strPath)
Set FileList = f.Files
For Each currFile In FileList
Write #1, currFile, currFile.DateCreated
Next
Write #1, "Files Deleted:"
While Not EOF(1)
Input #1, filePath, fileDate
days = DateDiff(d, fileDate, Now)
If (days > limit) Then
Write #1, filePath, days, "old"
Kill (filePath)
End If
Wend
End Function
Private Sub Form_Activate()
GetFileInfo "z:\Test Folder"
End Sub
Basically I can't get the days variable right. I don't think anything is storing in it.
# 8 Re: Files and Folders
Try Dim fileDate As Date, not as String.
If you get an error when trying to Input#, then
- leave it as string and add a Date variable: Dim fDate As Date
- Read in the filename, date
- fDate = CDate(fileDate)
- now do your DateDiff using fDate not fileDate
# 9 Re: Files and Folders
Look at another way:
Option Explicit
'
' Add a reference to Microsoft Scripting Runtime
Dim ct As Integer
Sub LoadListBox(strPath As String, SettingsList As ListBox)
On Error Resume Next
Dim s1 As String
SettingsList.Clear
Dim fso As New FileSystemObject, tso As TextStream
Set tso = fso.OpenTextFile(strPath)
Do While Not tso.AtEndOfStream
ct = ct + 1
s1 = tso.ReadLine
SettingsList.AddItem s1 & " "
DoEvents
Loop
tso.Close
End Sub
Private Sub Form_Load()
LoadListBox "c:\PASSWORD.txt", SettingsList
Debug.Print ct & " files"
End Sub
# 10 Re: Files and Folders
I think what my problem was that I did not close file from outputting before I tried to input. Also the way vb6 outputs into a file is like:
"This is my string",#date#
When inputting does it skip over the comma or do I have to put some code in so it reads over it. I've been trying to debug myself but I need a simple print function, comparable to cout in c++ but I don't know of one.
# 11 Re: Files and Folders
Write# gives you quotes and date# indicators, with commas.
If you use PRINT# you don't get any of those, and have to add them yourself.
It is easier, usually, unless you are doing a text-import from another app.
# 12 Re: Files and Folders
Yes, not closing your file will cause an EOF immediately. After you close it, you need to Open it. So, you will need 2 files, one to read from and one to write to. You cannot reliably read & write to the same file at the same time unless the file is opened As Binary or as a recordset.
Example: Read log line, test file date to Limit, and if needed: write to log and kill file
Additionally, I still believe you must change your fileDate variable to Date vs String.
# 13 Re: Files and Folders
Yes I've changed the variable in my program to Date and everything seems to be working now.
Thanks
# 14 Re: Files and Folders
Thanks for the help guys. This is my revised code.
Function GetFileInfo(strPath As String)
Open strPath For Input As #1
Dim filePath As String
Dim daysOld, limit As Integer
Dim f As Object, fso As New FileSystemObject
Dim FileList As Files, currFile As File
While Not EOF(1)
Input #1, filePath, limit 'Inputting filename and age limit in given format
Set f = fso.GetFolder(filePath) 'Creating a folder object to read from
Set FileList = f.Files 'Creates an array with the filenames from the given folder
For Each currFile In FileList
daysOld = DateDiff("d", currFile.DateCreated, Now)
If (daysOld > limit) Then
Kill (currFile)
End If
Next
Wend
End Function
Public Sub main()
GetFileInfo App.Path & "\FileList.txt"
End Sub
I wrote this in a module not a form and I need to add a timer for everytime it goes to the next input line for the folder path. How would I go about making this timer as I've tried searching and the only way I've seen is through a form
# 15 Re: Files and Folders
You want to run that section to delete certain files over and over?
What timing do you want. Hourly? Once per day?
# 16 Re: Files and Folders
I want to run basically the While Not EOF(1) section every 60 seconds so I can to give some memory relief. Basically once one folder has been read through and all the files that are too old are deleted, and its bout to go onto the next file path I want to pause for 60 seconds before it reads in the next folder path.
# 17 Re: Files and Folders
Create another variable for the folder and add a TIMER:
Option Explicit
Private Sub Form_Load()
Timer1.Interval = 500 ' 1/2 second
Timer1.Enabled = True
End Sub
Function GetFileInfo(strPath As String)
Dim filePath As String
Dim daysOld, limit As Integer
Dim f As Object, fso As New FileSystemObject
Dim FileList As Files, currFile As File
Open strPath For Input As #1
While Not EOF(1)
Input #1, filePath, limit 'Inputting filename and age limit in given format
Set f = fso.GetFolder(filePath) 'Creating a folder object to read from
Set FileList = f.Files 'Creates an array with the filenames from the given folder
For Each currFile In FileList
daysOld = DateDiff("d", currFile.DateCreated, Now)
If (daysOld > limit) Then
Kill (currFile)
End If
Next
Wend
Set f = Nothing
Set FileList = Nothing
End Function
Private Sub Timer1_Timer()
Static counter
Timer1.Enabled = False
If counter Mod 120 = 0 Then GetFileInfo (yourstring$)
Timer1.Enabled = True
End Sub
# 18 Re: Files and Folders
I believe the timer you posted is for a form but I'm trying to add it to a module.