|
-
Jul 22nd, 2011, 11:12 AM
#1
Thread Starter
New Member
[RESOLVED] Renaming files based on age
I have a machine that saves files using machine information which really is quite random.
What i want to do is move and rename these files sequentially. I have done that, but my code does not do it based on the time the files were saved and I'm stumped.
I want to save the files (.prn - text files) as 1.prn, 2.prn, 3.prn etc. based on 1.prn being the first the machine wrote. Right now it seems to be a random order, I have not looked, but maybe alphabetical ...
I appreciate any help you can give me.
FC
Here is what I have (it is a snippet,so some variables will not make sense here):
vb Code:
spath = "c:\program files\outputprn\"
spattern = "*.prn"
Set cFiles = New Collection
'get the files to be renamed
sFile = Dir$(spath & spattern)
Do While Len(sFile) > 0
If (sFile <> "." And sFile <> "..") Then
cFiles.Add sFile
End If
sFile = Dir$()
Loop
'rename the files
For iFile = 1 To cFiles.Count
Name spath & cFiles.Item(iFile) As newpath & Format$(cFiles.Item(iFile), "0")
Next iFile
Last edited by Hack; Jul 22nd, 2011 at 11:15 AM.
Reason: Added Highlight Tags And Some Indenting
-
Jul 22nd, 2011, 11:41 AM
#2
Re: Renaming files based on age
Welcome to VBForums 
I've got no idea if Dir returns items in any particular order or not, because I don't rely on things like that - it is one of those things that could be dealt with by Windows rather than VB, and thus not behave the same way all the time (it might change in a different version of Windows, or on the same one if a setting is changed,etc).
What I would recommend is determining the file time (using the FileDateTime function), and then sort the list accordingly before renaming them.
There is a good article on sorting linked from our Classic VB FAQs (in the FAQ forum)
-
Jul 22nd, 2011, 11:57 AM
#3
Re: Renaming files based on age
u can get the date and time of file modified using FileDateTime() function.
edited: sorry already told above by si the geek.
-
Jul 22nd, 2011, 12:50 PM
#4
Thread Starter
New Member
Re: Renaming files based on age
Thanks, but I'm not so good at arrays and collections to begin with ...
Obviously, I'm loading the files into a collection, but do I use filedatetime to bring the files into the array, sort them from within, or bring them out that way?
It took me many revisions to get this far!
But again, thanks for your help, you're just assuming I'm smarter than I really am!!!
-
Jul 22nd, 2011, 03:24 PM
#5
Re: Renaming files based on age
The easiest way based on what you've got already (and probably the fastest to run) is to create the array array after the "Loop" line (so you know what size it needs to be), then fill it from the collection (and the FileDateTime value), and then sort the array based on the time.
Creating the array will be like this (Variant is safe for String and Date data):
Code:
Dim arrFiles() as Variant
ReDim arrFiles(1, cFiles.Count) as Variant
Your "rename the files" section will need to be modified to use the array rather than the collection.
The arrays articles in the "Data types/Variables" section of the FAQs will be useful.
-
Jul 25th, 2011, 10:46 AM
#6
Thread Starter
New Member
Re: Renaming files based on age
I tried this weekend, with little success ...
I ended up having to start again as I screwed up my collection as well...
I will try to read more on the array section, but it seems to be a bit over my head.
BTW, I live in Vancouver now, but was born in Devon, just down the road from you. Thanks for your help, but I think your a bit beyond me.
Thanks anyway
-
Jul 26th, 2011, 01:34 AM
#7
Re: Renaming files based on age
Greetings from Essex to an Ex-pat.......
You could continue to use a Collection and also use a hidden ListBox to perform the sort. (I'm not a great fan of using controls like this but it works and saves you having to write / locate a sort routine)
Code:
Option Explicit
'
' Requires: a Reference to Microsoft Scripting Run Time
' a ListBox, named lstFiles with the Sorted Property set to True
' a CommandButton, named Command
'
Private Sub Command_Click()
Dim fso As FileSystemObject
Dim objFolder As Folder
Dim objFile As File
Dim colFiles As Collection
Dim intI As Integer
Dim sPath As String
Dim sExtn As String
sPath = "C:\Program Files\outputprn\"
sExtn = ".prn"
Set colFiles = New Collection
Set fso = New FileSystemObject
'
' Get the Folder we're interested in
'
Set objFolder = fso.GetFolder(sPath)
'
' Iterate through the Files in the Folder
'
For Each objFile In objFolder.Files
'
' Does this File have the extension
' we are looking for?
'
If InStr(objFile.Name, sExt) > 0 Then
'
' Yes, add the Last Modified Date to the hidden ListBox
' Set the Itemdata for this file as the 'file number'
' Add the filename to the collection with a key
' equal to the 'file number'
' Note that the Key has to be a String Type variable
'
lstFiles.AddItem CDbl(objFile.DateLastModified)
lstFiles.ItemData(lstFiles.NewIndex) = intI
colFiles.Add objFile.Name, CStr(intI)
intI = intI + 1
End If
Next
'
' The ListBox will have been sorted
' now go through the listbox using the ItemData value
' as the key for the collection, pick up the
' corresponding filename and output to
' the Immediate Window
'
For intI = 0 To lstFiles.ListCount - 1
'
' This is where you could put your Renaming code
' Name spath & colFiles(CStr(lstFiles.ItemData(inti))) As ....etc
'
Debug.Print colFiles(CStr(lstFiles.ItemData(intI)))
Next intI
End Sub
Private Sub Form_Load()
lstFiles.Visible = False
End Sub
Converting the Date to a Double type should cater for regional date formats.
It might seem a little complicated but hopefully the comments will help you understand the logic.
Last edited by Doogle; Jul 26th, 2011 at 01:47 AM.
Reason: Tidied up a bit
-
Jul 27th, 2011, 07:21 PM
#8
Thread Starter
New Member
Re: Renaming files based on age
Listbox!!!
Brilliant !!! (as John Cleese would say ...)
All I needed was:
(newpath is global variable picked up in a popup form, should have a gg in front I know, but I'm slowly learning ...)
Code:
Name sPath & colFiles(CStr(lstFiles.ItemData(intI))) As newpath & intI + 1 & ".prn"
I appreciate the help from all immensely.
Take Care
Frank
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
|