Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy and Rename File VBScript

Tags:

vbscript

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
like image 261
Brad Avatar asked Sep 12 '12 17:09

Brad


People also ask

How do I rename a file in VBA?

To RENAME an Excel file that is stored on your computer, you need to use the “NAME” statement. In this statement, you need to define the old file name and the new name that you want to apply. But there's one thing that you need to remember the file must be closed.

How do I copy a file from one directory to another in vbscript?

The following scripts demonstrate how to copy a file from one local folder to another. In the first step, the script creates a File System Object. The CopyFile method, a file system object method, performs the file copy operation. The CopyFile method takes two parameters, the source file and the destination.


1 Answers

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"
like image 190
Jap Mul Avatar answered Sep 30 '22 00:09

Jap Mul