Copy and Rename File VBScript

2020-08-17 06:28发布

问题:

I need to move a file with a name based off of a date to another folder.

The file structure is:
Source: \\network_location\folder\Filename_09-11-2012.txt

Destination: C:\Dump\Filename.txt

The source file is always 1 day behind. I am looking to rename the file while copying it.
The code I am trying to use is:

Sub Copy_And_Rename()
    Name "\\network_location\folder\Filename_"+Month(Now())+"-"+Day(Now()-1)+"-"+Year(Now())+".txt" As "C:\Dump\Filename.txt"
End Sub

回答1:

You can copy and rename a file with the FileSystemObject like this:

Set objFSO = CreateObject("Scripting.FileSystemObject")
' First parameter: original location\file
' Second parameter: new location\file
objFSO.CopyFile "C:\Test\folder1\name1.txt", "C:\Test\folder2\name2.txt"


回答2:

Code to copy and rename file

sourceFilePath = "C:\filePath\source.xlsx"
destinationFilePath = "C:\filePath\destination.xlsx"

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile sourceFilePath, destinationFilePath


标签: vbscript