Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am trying to create a VBScript that will read through a folder, copy and rename a specific set of files. I would like to prefix each renamed file with "me-YYMM-" filename. The names of the files will never change, but I want the renamed files to be named with the current YYMM when I run the script. This is what I would like to be able to do.

copy and rename - c:\dir1\file1 to c:\dir2\me-YYMM-file1
copy and rename - c:\dir1\file2 to c:\dir2\me-YYMM-file2

The dir1, dir2 and file names will always be static, but I may need to add or delete files from this process. Any help would be appreciated. For my purposes it could be a copy or a move. I have to be able to rename the files though.
Posted
Updated 18-Mar-11 4:15am
v2
Comments
Sergey Alexandrovich Kryukov 20-Mar-11 22:51pm    
OP commented:

Set objFSO = CreateObject("Scripting.FileSystemObject")
' Get the current year and month and copy/move file(s) to the same
' or different directory and prefix the file with YYMM-
Dim sDate
Dim sYear
Dim sMonth
' Get the current year and month for the file prefix
sYear = Right(Year(Now), 2)
sMonth = Month(Now)
If Len(sMonth) = 1 Then
sMonth = "0" & sMonth
End If
sDate = sYear & sMonth & "-"

objFSO.CopyFile "C:\dir1\file1-prt", "C:\dir2\" & sDate & "file1"
objFSO.CopyFile "C:\dir1\file2-prt", "C:\dir2\" & sDate & "file2"

copies and renames C:\dir1\file1-prt to C:\dir2\yymm-file1

I also tried to copy and rename the file to the same directory and it worked just fine. Also tried a move and rename to the same directory and it worked with no issues.

I know this may seem simple to some, but

1 solution

Well, to start off use FileSystemObject.CopyFile to copy the file (see doc[^])

Once you copy the file you need to rename it, right? to do so you FileSystemObject.MoveFile , see doc[^]
 
Share this answer
 
Comments
Danno57 18-Mar-11 10:48am    
I was able to use this and get the date to move the files. Thanks.
Yusuf 18-Mar-11 11:07am    
It just occurred to me that, what happens if you use only CopyFile and you give it the new name in the same folder? Will that do both the copy and rename or does it only renames the file?
Danno57 18-Mar-11 11:36am    
Just tried the CopyFile and rename. It leaves the original and makes a copy in the same directory.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900