work with a file name that changes based on that days date

Posted on 2009-12-17
Last Modified: 2012-05-08
I need help with visual basic code for the following:

The file name is in this format:  rmr121709.lmc

I need to select the file in a directory that matches that days date, then rename the file to oldfile.txt, run a script on the file then rename the file back to rmr & todays date & .lmc.

Question by:Danean
    LVL 18

    Accepted Solution

    Dim fileName As String = "rmr" & DateTime.Today.ToString("MMddyy") & ".lmc"
    File.Move(fileName, "oldfile.txt")

    ' ... run script here

    File.Move("oldfile.txt", fileName)
    LVL 27

    Expert Comment

    All you need to do is search directory for the day you want, pick that file.
    Then run the script first then you can do two things.
    1. You can then re name it
    2. You can move it to another directory for  later archiving or
         just in case you have to find it again to re-run it.

    I personnally when keep the name the same and move it to a directory for archiving.
    LVL 65

    Expert Comment

    Hi, this VBS code will rename the file that you are referring to for today's date.


    strFolder = "C:\Backups"
    If Right(strFolder, 1) <> "\" Then strFolder = strFolder & "\"
    strTodaysFile = "rmr" & Right("0" & Month(Date), 2) & Right("0" & Day(Date), 2) & Right(Year(Date), 2) & ".lmc"
    strNewFileName = "oldfile.txt"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists(strFolder & strTodaysFile) = True Then
    	If objFSO.FileExists(strFolder & strNewFileName) = True Then objFSO.DeleteFile strFolder & strNewFileName, True
    	objFSO.MoveFile strFolder & strTodaysFile, strFolder & strNewFileName
    	WScript.Echo "Cannot find " & strFolder & strTodaysFile
    End If

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now