Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB Code to change filename while being copied

Status
Not open for further replies.

melidrie

MIS
Dec 2, 2016
1
0
0
NL
Hello i am new to this forum and i need some help :)

I have this code that search for a filename by name and modified Date,
It will copy the file to a new path and that works.
But while its being copied i want to rename it.

Who can help me :):)

'''''''''''''''''''''''''''''''''''''''''''''''''
'Code
'''''''''''''''''''''''''''''''''''''''''''''''''

'VARIABLES


bPath = "E:\DRIVERS\Groep 2\" 'DESTINATION PATH

aBeginWith = "holdings_" 'FILE BEGINS WITH

aDate = InputBox("Date", "DATE", Date) 'FILES CREATED AFTER THIS DATE


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set fso = CreateObject("Scripting.FileSystemObject")




'SOURCE PATH

Set aPath = fso.GetFolder("E:\DRIVERS\Groep 1\") 'SOURCE PATH


'SEARCH THE FILES AND COPY TO DESTINATION

For Each file In aPath.Files
If RIGHT(file.Name, 4) = ".txt" and Left(file.Name, Len(aBeginWith)) = aBeginWith and DateDiff("d", File.DateLastModified, aDate) = 0 then

file.Copy bPath & file.name


'objFSO.MoveFile "E:\DRIVERS\Groep 2\holdings_********.txt" , "E:\DRIVERS\Groep 2\test.txt"


End If


Next
 
You have it, just in the file.Copy command, replace "file.name" with the new filename you want.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top