Solved

vbscript to move files, rename them with unique names and with .rtf extension

Posted on 2008-06-18
4
1,209 Views
Last Modified: 2012-06-27
I am trying to put together a script that will move files from one folder into another and give them a unique name once they are moved with a .rtf extension.  
        objFSO.MoveFile objFile.Path, strDestinationFolder & NewFileName
   
If I finish the script with the line above to move the files to the destination folder and rename it, how do I rename it with a unique filename each time and make the extension .rtf?

Any advice would be appreciated.

Thank you.
0
Comment
Question by:cbones
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 14

Expert Comment

by:sungenwang
ID: 21818892
The script below will rename a file with a  datetime stamp. It should be unique up to the seconds.

sew

strSourceFile = "c:\temp\test.txt"
strDestinationFolder = "c:\temp1\"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strSourceFile)
 
' use date/time stamp as new file name
strNewFileName = Year(Now) & PadStr(Month(Now),2) & PadStr(Day(Now),2) & PadStr(Hour(Now),2) & PadStr(Minute(Now),2) & PadStr(Second(Now),2) & ".rtf"
objFSO.MoveFile objFile.Path, strDestinationFolder & strNewFileName
 
Function PadStr(str, intLength)
	intNumOfZeros = intLength-Len(str)
	If intNumOfZeros > 0 Then
		PadStr = String(intNumOfZeros, "0") & str
	Else
		PadStr = str
	End If
End Function

Open in new window

0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 50 total points
ID: 21819030
Hi, above that line:
objFSO.MoveFile objFile.Path, strDestinationFolder & NewFileName

you can put this
NewFileName = Left(NewFileName, Len(NewFileName) - 4) & ".rtf"

which will strip the last four characters off from NewFileName, and replace it with .rtf

Regards,

Rob.
0
 

Author Comment

by:cbones
ID: 21821086
I would need the script to scan through the folder for multiple files.  Is there a way to have it scan for all files that start with the same name.
Ex:   c:\test\test1
        c:\test\test2
        c:\test\test3
Have it scan for all files starting with test, then move the files renaming it with the date like in the above script.
Thank you.
0
 
LVL 14

Accepted Solution

by:
sungenwang earned 200 total points
ID: 21821983
The code below should do it. The output would be:
<path>\test_YYYYMMDDHHMMSS_X.rtf where X is a sequential counter (1,2,3,4,etc)

sew

strSourceFolder = "c:\temp\"
strDestinationFolder = "c:\temp1\"
strFileNameToMatch = "test"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strSourceFolder)
intCount = 1
For Each objFile In objFolder.Files
	strFileName = objFSO.GetFileName(objFile)
	If LCase(Left(strFileName, 4)) = strFileNameToMatch Then
		' use date/time stamp as new file name
		strNewFileName = strFileNameToMatch & "_" & Year(Now) & PadStr(Month(Now),2) & PadStr(Day(Now),2) & PadStr(Hour(Now),2) & PadStr(Minute(Now),2) & PadStr(Second(Now),2) & "_" & intCount & ".rtf"
		objFSO.MoveFile objFile.Path, strDestinationFolder & strNewFileName
		intCount = intCount + 1
	End If
Next
 
Function PadStr(str, intLength)
	intNumOfZeros = intLength-Len(str)
	If intNumOfZeros > 0 Then
		PadStr = String(intNumOfZeros, "0") & str
	Else
		PadStr = str
	End If
End Function

Open in new window

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question