How can I rename a file using cmd script?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brat33
    New Member
    • Jan 2010
    • 36

    How can I rename a file using cmd script?

    I have a .cmd file that is copying a .csv file from one server location to another server location. One line of code:

    Copy SourceLocation DestinationLoca tion

    I have added a line of code to rename the file and if I give it a specific name, it works fine. However, I want the new name to be CTMMDDYY with MMDDYY being yesterdays date. I tried to do something like this, just trying to get todays date to work for me:

    set Day = %date:~0,2%
    set mth = %date:~3,2%
    set yr = %date:~8,2%

    then performed a rename like this:

    rename CurrentName CT%day%-%mth%-%yr%.csv

    But nothing happens. Does anyone have any suggestions on how to make this work? How about going about getting yesterdays date in the cmd script?
  • sashi
    Recognized Expert Top Contributor
    • Jun 2006
    • 1749

    #2
    Hi Brat,

    Try the below sample, hope it works.

    Set FileDate=%Date: ~2%

    REN current_name %FileDate%.csv

    Comment

    • brat33
      New Member
      • Jan 2010
      • 36

      #3
      Thank you for the suggestion, but this resulted in only renaming the file to FileDate.csv. I can not seem to get the variable name to be understood within the cmd scripting. I am not sure what my next step is, so I am just searching the internet trying to come up with more ideas to try. Any other suggestions would be helpful.
      Thank you anyway! I was nice to have a reply :)

      Comment

      • sashi
        Recognized Expert Top Contributor
        • Jun 2006
        • 1749

        #4
        Hi Brat,

        Let me check in my collections if i have something that might help you. I have tonnes junks to dig up :)

        Will get back to you on this real soon. Take care & Have a great day ahead :)

        Comment

        • sashi
          Recognized Expert Top Contributor
          • Jun 2006
          • 1749

          #5
          Hi Brat,

          With this simple produces yesterday's date, take a look :)

          Code:
          FOR /F "TOKENS=1,2 DELIMS=/ " %%A IN ('DATE /T') DO SET mm=%%B
          FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('DATE /T') DO SET dd=%%B
          FOR /F "TOKENS=3* DELIMS=/ " %%A IN ('DATE /T') DO SET yyyy=%%B
          
          set /A dd=%dd%-1
          
          set Pdate=%mm%/%dd%/%yyyy%
          echo %Pdate%

          Comment

          • brat33
            New Member
            • Jan 2010
            • 36

            #6
            Do I need to do anything special with the .cmd script file in order to recognize the variable names used in the file? This is my first time working with a .cmd script and am not sure if I am missing something simple. The set command is still just setting the "new name" to the variable name, NOT the value of the variable. This does not seem like it should be hard to do. Like I said I have never worked with a .cmd script file before, so I am not sure if this is a valid question or not, but could I call a .vbs file from within the .cmd file and run the modification through visual basic scripting? I would need to place a reference to vb onto the machine running the script I know, but is this possible? I am missing something small again (typical of me).

            Comment

            • sashi
              Recognized Expert Top Contributor
              • Jun 2006
              • 1749

              #7
              Hi Brat,

              Yes, it is possible to call vbscript (.vbs) within (.cmd) script. You could also modify your vbscript with file append method.

              Kindly refer to below attached link, hope it helps :)

              Batch Scripts for Windows

              Comment

              Working...