Results 1 to 8 of 8

Thread: [RESOLVED] Renaming files based on age

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    4

    Resolved [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:
    1. spath = "c:\program files\outputprn\"
    2. spattern = "*.prn"
    3.  
    4. Set cFiles = New Collection
    5.  
    6. 'get the files to be renamed
    7. sFile = Dir$(spath & spattern)
    8. Do While Len(sFile) > 0
    9.      If (sFile <> "." And sFile <> "..") Then
    10.          cFiles.Add sFile
    11.      End If
    12. sFile = Dir$()
    13. Loop
    14.  
    15. 'rename the files
    16. For iFile = 1 To cFiles.Count
    17.      Name spath & cFiles.Item(iFile) As newpath & Format$(cFiles.Item(iFile), "0")
    18. Next iFile
    Last edited by Hack; Jul 22nd, 2011 at 11:15 AM. Reason: Added Highlight Tags And Some Indenting

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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)

  3. #3
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    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.
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    4

    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!!!

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    4

    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

  7. #7
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    4

    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
  •  



Click Here to Expand Forum to Full Width