-
Dec 3rd, 2017, 03:44 PM
#1
Thread Starter
Frenzied Member
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
-
Dec 3rd, 2017, 04:19 PM
#2
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.
-
Dec 3rd, 2017, 05:26 PM
#3
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)
-
Dec 3rd, 2017, 08:30 PM
#4
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.
-
Dec 3rd, 2017, 09:12 PM
#5
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).
-
Dec 8th, 2017, 03:03 PM
#6
Thread Starter
Frenzied Member
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!
-
Dec 8th, 2017, 03:52 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|