ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Change name of files in a folder (https://www.excelbanter.com/excel-programming/358198-change-name-files-folder.html)

TISR

Change name of files in a folder
 
Hi to all
I am able to copy files into a folder using Scripting.FileSystemObject.
Once in the new folder I would like to change their name to reflect the new
month.
I have tried:
Const Dest = "C:\Mis documentos\12 December 2005"
Dim FSO As Object
Dim f, f1, fc
Set f = FSO.GetFolder(Dest)

For Each f1 In f.Files
Name f1.Path As Mid(f1.Name, 1, (Len(f1.Name) - 9)) & _
Format(DateSerial(Year(Date), Month(Date) - 1, 1), "yyyy-mm")
Next f1
And I have also tried
Dim wkbFolder As Application
Dim wkbSource As Workbook
Set wkbFolder.Path = "C:\Mis documentos\12 December 2005"
Set wkbSource = Application.ActiveWorkbook
For Each wkbSource In wkbFolder.Path
wkbSource.Name = Mid(wkbSource.Name, 1, (Len(wkbSource.Name) - 9)) & _
Format(DateSerial(Year(Date), Month(Date) - 1, 1), "yyyy-mm")
Next
Needless they don“t work
Can someone help? It would be appreciated.

Don Guillett

Change name of files in a folder
 
try this without copying first. It will just move and rename.

Sub movefile()
OldName = "C:\oldfoldername\oldfilename.xls"
NewName = "C:\newfoldername\newfilename.xls"
Name OldName As NewName
End Sub

--
Don Guillett
SalesAid Software

"TISR" wrote in message
...
Hi to all
I am able to copy files into a folder using Scripting.FileSystemObject.
Once in the new folder I would like to change their name to reflect the
new
month.
I have tried:
Const Dest = "C:\Mis documentos\12 December 2005"
Dim FSO As Object
Dim f, f1, fc
Set f = FSO.GetFolder(Dest)

For Each f1 In f.Files
Name f1.Path As Mid(f1.Name, 1, (Len(f1.Name) - 9)) & _
Format(DateSerial(Year(Date), Month(Date) - 1, 1), "yyyy-mm")
Next f1
And I have also tried
Dim wkbFolder As Application
Dim wkbSource As Workbook
Set wkbFolder.Path = "C:\Mis documentos\12 December 2005"
Set wkbSource = Application.ActiveWorkbook
For Each wkbSource In wkbFolder.Path
wkbSource.Name = Mid(wkbSource.Name, 1, (Len(wkbSource.Name) - 9)) & _
Format(DateSerial(Year(Date), Month(Date) - 1, 1), "yyyy-mm")
Next
Needless they don“t work
Can someone help? It would be appreciated.




Tom Ogilvy

Change name of files in a folder
 
Dim f as String, s as String
Const Dest = "C:\Mis documentos\12 December 2005\"
f = dir(Dest & "*.*")
do while f < ""
s = Mid(f, 1, (Len(f) - 9)) & _
Format(DateSerial(Year(Date), Month(Date) - 1, 1), "yyyy-mm")
Name Dest & f, Dest & s
f = dir()
Loop

I don't know what your filenames look like, so I am not sure what extensions
you have or how you are accounting for the extension in your code above. You
take the whole file name except for the right most 9 characters and you
replace those with 7 characters. So you will have to work that out.

--
Regards,
Tom Ogilvy


"TISR" wrote:

Hi to all
I am able to copy files into a folder using Scripting.FileSystemObject.
Once in the new folder I would like to change their name to reflect the new
month.
I have tried:
Const Dest = "C:\Mis documentos\12 December 2005"
Dim FSO As Object
Dim f, f1, fc
Set f = FSO.GetFolder(Dest)

For Each f1 In f.Files
Name f1.Path As Mid(f1.Name, 1, (Len(f1.Name) - 9)) & _
Format(DateSerial(Year(Date), Month(Date) - 1, 1), "yyyy-mm")
Next f1
And I have also tried
Dim wkbFolder As Application
Dim wkbSource As Workbook
Set wkbFolder.Path = "C:\Mis documentos\12 December 2005"
Set wkbSource = Application.ActiveWorkbook
For Each wkbSource In wkbFolder.Path
wkbSource.Name = Mid(wkbSource.Name, 1, (Len(wkbSource.Name) - 9)) & _
Format(DateSerial(Year(Date), Month(Date) - 1, 1), "yyyy-mm")
Next
Needless they don“t work
Can someone help? It would be appreciated.


TISR

Change name of files in a folder
 
Many thanks Tom for your answer
However when I try to use it gets stuck on the code line
Name Dest & f, Dest & s(Apparently needs an "As" instead of",")
Thus I had to change to the following:
Name Dest & f As Dest & s
It Didn“t work. then I tried:
Name f As s
It Didn“t work. then I tried:
Name f As Dir(Dest & s)
It didn“t work
The message keeps on saying that it cannot find the root directory access.
Any ideas?
Also what is the point of f= Dir() at the end of loop.
If you can help it would appreciated
"Tom Ogilvy" wrote:

Dim f as String, s as String
Const Dest = "C:\Mis documentos\12 December 2005\"
f = dir(Dest & "*.*")
do while f < ""
s = Mid(f, 1, (Len(f) - 9)) & _
Format(DateSerial(Year(Date), Month(Date) - 1, 1), "yyyy-mm")
Name Dest & f, Dest & s
f = dir()
Loop

I don't know what your filenames look like, so I am not sure what extensions
you have or how you are accounting for the extension in your code above. You
take the whole file name except for the right most 9 characters and you
replace those with 7 characters. So you will have to work that out.

--
Regards,
Tom Ogilvy


"TISR" wrote:

Hi to all
I am able to copy files into a folder using Scripting.FileSystemObject.
Once in the new folder I would like to change their name to reflect the new
month.
I have tried:
Const Dest = "C:\Mis documentos\12 December 2005"
Dim FSO As Object
Dim f, f1, fc
Set f = FSO.GetFolder(Dest)

For Each f1 In f.Files
Name f1.Path As Mid(f1.Name, 1, (Len(f1.Name) - 9)) & _
Format(DateSerial(Year(Date), Month(Date) - 1, 1), "yyyy-mm")
Next f1
And I have also tried
Dim wkbFolder As Application
Dim wkbSource As Workbook
Set wkbFolder.Path = "C:\Mis documentos\12 December 2005"
Set wkbSource = Application.ActiveWorkbook
For Each wkbSource In wkbFolder.Path
wkbSource.Name = Mid(wkbSource.Name, 1, (Len(wkbSource.Name) - 9)) & _
Format(DateSerial(Year(Date), Month(Date) - 1, 1), "yyyy-mm")
Next
Needless they don“t work
Can someone help? It would be appreciated.



All times are GMT +1. The time now is 12:04 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com