Trying to rename files in a loop-VBForums
Results 1 to 7 of 7

Thread: Trying to rename files in a loop

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2012
    Posts
    643

    Trying to rename files in a loop

    What am I doing wrongly here please? All works except the line marked '...fails'

    Dim oFileSys As Scripting.FileSystemObject
    Dim oFolder As Scripting.Folder
    Dim oFile As Scripting.File

    Set oFileSys = New Scripting.FileSystemObject
    Set oFolder = oFileSys.GetFolder(NewPartnerSubDirA)

    x = 0
    For Each oFile In oFolder.Files
    x = x + 1
    Name oFile As "Bloggs" & CStr(x) ' *** this line fails ***
    Debug.Print x, oFile.Name, Len(oFile.Name) 'this line fine
    Next oFile

    Set oFolder = Nothing
    Set oFileSys = Nothing

  2. #2
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    389

    Re: Trying to rename files in a loop

    i nesver use scripting, but check oFile if you have the whole "path" included, or if you want to do this operation without the path, you need to change the current path to the folder you have the files you want to rename. the same apply to the renamed file, remember to include the path if you use the former, skip the path if you use the latter.

    edit: now looking at it, you write "oFile.Name" but not in the rename command why is that?
    shoudln't you use the filename as well when you rename?
    Last edited by baka; Dec 3rd, 2017 at 05:14 PM.

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,361

    Re: Trying to rename files in a loop

    As stated by baka..your problem is you're lacking your total path.

    Try

    Code:
    Name oFile As NewPartnerSubDirA & "Bloggs" & CStr(x)

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    18,060

    Re: Trying to rename files in a loop

    This:

    Code:
    Name oFile As NewPartnerSubDirA & "Bloggs" & CStr(x)
    Compiles as:

    Code:
    Name oFile.Path As NewPartnerSubDirA & "Bloggs" & CStr(x)
    Default properties can work for or against you. When in doubt, spell it out.

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,361

    Re: Trying to rename files in a loop

    Good point...was just using OP's...neglected to show default property.

    EDIT: but...it worked in an example just fine (only I used hardcoded paths instead of a variable).

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2012
    Posts
    643

    Re: Trying to rename files in a loop

    Many thanks all. Work has kept me away from this, but I've now tried it, and it works fine. Funny thing is the line I originally marked as OK now doesn't work!

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,123

    Re: Trying to rename files in a loop

    because you already renamed the file
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.