Consulting

Results 1 to 7 of 7

Thread: Renaming txt files

  1. #1
    VBAX Regular
    Joined
    Jan 2008
    Posts
    13
    Location

    renaming txt files

    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...
    Last edited by Flynazn; 03-17-2008 at 04:02 PM.

  2. #2
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    Please post the code that is outputting the file. Thanks
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  3. #3
    VBAX Regular
    Joined
    Jan 2008
    Posts
    13
    Location
    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.

  4. #4
    VBAX Regular
    Joined
    Jan 2008
    Posts
    13
    Location

    Renaming txt files

    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...

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Duplicate threads merged.

    Flynazn 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........
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    VBAX Regular
    Joined
    Jan 2008
    Posts
    13
    Location

    More details

    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.

    [VBA]
    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
    [/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •