General
cancel
Showing results for 
Search instead for 
Did you mean: 

change name while copying a file in vbs

kabucek
Advisor

change name while copying a file in vbs

hi all,

i have the following script,
and I want to change the name of destination file to
something.txt,
how to do it?

N=Now
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolder="L:\"
strNewDest = "Q:\"
Set objFolder = objFSO.GetFolder(strFolder)
For Each strFiles In objFolder.Files
If DateDiff("d",N, strFiles.DateLastModified) = -1 Then
objFSO.CopyFile strFiles , strNewDest & "\" & strFiles.Name
End If
Next

thanks

3 REPLIES
Gfuss
Trusted Contributor

Re: change name while copying a file in vbs

kabucek,

Using .MoveFile, you can specify the new name as shown below:


objFSO.MoveFile "C:\myFileName.txt" , "C:\myNewFileName.txt"
kabucek
Advisor

Re: change name while copying a file in vbs

but these files are ntbackup reports and the name changes periodically.
i want to catch "NewestFile" and copy that file.
thanks
Gfuss
Trusted Contributor

Re: change name while copying a file in vbs

Instead of using strFiles.Name for the destination, simply place in your new file name. For example:

objFSO.CopyFile strFiles , strNewDest & "\" & something.txt

If there are multiple ones, you could also just try adding the new extension:

objFSO.CopyFile strFiles , strNewDest & "\" & strFiles.Name & ".txt"