PDA

View Full Version : Renaming txt files



Flynazn
03-17-2008, 08:14 AM
Hi all,

I am looking for a way to rename a .txt file.
The problem is...I have an application that give an output file the same exact name each time it is run.

I want to take that file and rename it to a unique name each time...maybe by adding .001 to the end and incrementing it each time.

Please help...

Oorang
03-17-2008, 09:16 AM
Please post the code that is outputting the file. Thanks:)

Flynazn
03-17-2008, 10:03 AM
Hi Aaron,
sorry but I don't have that outputting file code. It is hardcoded (I think). All I know is that each time I run this application, it give me an output file like this...mar172008.txt....so I want to copy this file to another folder (c:\program files\results\mar172008.txt) then rename it will an original name...I was thinking of doing that by adding -001.txt to the end. I hope this makes it clearer. I can copy files over but don't know how to rename it with a different name each time around.

Flynazn
03-17-2008, 04:08 PM
Hi all,

I am looking for a way to rename a .txt file.
The problem is...I have an application that give an output file, the same exact name each time it is run.

I want to take that file and rename it to a unique name each time...maybe by adding .001 to the end and incrementing it each time.

Please help...

mdmackillop
03-17-2008, 04:34 PM
Assuming this is a non-office application, you could possibly run it as a Shell command from VBA and then rename the resultant file. Otherwise, get in touch with the application's writers.
Regards
MD

lucas
03-17-2008, 07:06 PM
Duplicate threads merged.

Flynazn (http://vbaexpress.com/forum/member.php?u=14131) vbmenu_register("postmenu_136305", true); please do not make multiple copies of the same post in different forums. As you can see you have had several people working on the same problem with no knowledge that anyone else was working on it........

Flynazn
03-20-2008, 05:25 AM
Hi all,

Here's more of the info for my problem: I have a file in
c:\program files\mar152008.txt which gets replaced with the same file name every time a new scan is made. I want to copy and rename that file in C:\program files\Results\mar...I am thinking a date/time stamp can be inserted here...or just increment it with a count. I found the attached code but not quite sure where to put everything in. First time using FileSystemObject...I looked at the microsoft page and not too much help. Couldn't use the attachment so I paste it below.


Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim oFile
WScript.Echo "--------------- Before"
For Each oFile In oFS.GetFolder( csDir ).Files
If 0 = StrComp( csExt, oFS.GetExtensionName( oFile.Name ), vbTextCompare ) Then
WScript.Echo 0, oFile.Name, Split( oFile.OpenAsTextStream( ForReading
).ReadLine, "," )( 1 )
Else
WScript.Echo 0, oFile.Name
End If
Next
WScript.Echo "--------------- Action"
For Each oFile In oFS.GetFolder( csDir ).Files
If 0 = StrComp( csExt, oFS.GetExtensionName( oFile.Name ), vbTextCompare ) Then
WScript.Echo 1, oFile.Name
renCSVFile oFile, oFS
WScript.Echo 2, oFile.Name
End If
WScript.Echo
Next
WScript.Echo "--------------- After"
For Each oFile In oFS.GetFolder( csDir ).Files
WScript.Echo 3, oFile.Name
Next
Sub renCSVFile( oFile, oFS )
Const ForReading = 1
Dim sNewName : sNewName = Split( oFile.OpenAsTextStream( ForReading ).ReadLine,
"," )( 1 )
Dim sNewNameBase : sNewNameBase = Replace( sNewName, ".txt", "", vbTextCompare )
sNewName = sNewNameBase & ".txt"
If oFile.Name = sNewName Then
WScript.Echo "no need to rename file", oFile.Name, "to", sNewName
Else
WScript.Echo "about to rename file", oFile.Name, "to", sNewName
If oFS.FileExists( oFS.GetParentFolderName( oFile.Path ) & "\" & sNewName ) Then
WScript.Echo "can't rename file", oFile.Name, "to", sNewName
Dim nCnt : nCnt = 0
Dim sNewNameCnt : sNewNameCnt = sNewNameBase & "-" & nCnt & ".txt"
Do While oFS.FileExists( oFS.GetParentFolderName( oFile.Path ) & "\" & sNewNameCnt )
nCnt = nCnt + 1
sNewNameCnt = sNewNameBase & "-" & nCnt & ".csv"
Loop
WScript.Echo "about to rename file", oFile.Name, "to", sNewNameCnt
oFile.Name = sNewNameCnt
Else
oFile.Name = sNewName
End If
End If
End Sub