Please Read: Significant Update Planned, Migrating Forum Software This Month

See more
See less

rename folder script

  • Filter
  • Time
  • Show
Clear All
new posts

  • rename folder script

    Hi everyone,

    Can anybody help me write a short script on how to rename folder?

    I have a daily differential backup running and I want to run a script that rename backed up folder to today's date and then create a new folder with a new name. It's something like this:

    1. Rename the backed up folder to today's date
    2. Create a new folder to a specific name.

    I guess that is simple.

    Thanks so much

  • #2
    Re: rename folder script

    The following visual basic script will rename the directory C:\Backup to C:\DDMMYYYY and the create the directory C:\Backup. Just Modify the path of the Backup directory to your path.

    strMonth = Month(Date)
    If Len(strMonth) = 1 Then
    strMonth = "0" & strMonth
    End If
    strDay = Day(Date)
    If Len(strDay) = 1 Then
    strDay = "0" & strDay
    End If
    strYear = Year(Date)
    StrDate = StrDay & StrMonth & StrYear
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.MoveFolder "C:\Backup" , "C:\" & StrDate
    Set objFolder = objFSO.CreateFolder("C:\Backup")
    MCSA 2000/2003


    • #3
      Re: rename folder script

      To limit the amount of calls to Date() function to 1 be...

      strDate = date()
      strDate = Year(strDate)*10000 + Month(strDate)*100 + Day(strDate)

      ( YYYYMMDD )

      strFolderOldName = "C:\Backup"
      strFolderNewName = "C:\%Today% (was_Backup)"
      strCreateFolder = "c:\newFolder\newSubfolder\%Today%\MyFolder"
      dt = date() : dt = Year(dt)*1e4 + Month(dt)*1e2 + Day(dt)
      Call ReplaceVariable(strFolderNewName, "%TODAY%", dt)
      Call ReplaceVariable(strCreateFolder, "%TODAY%", dt)
      '1. Rename Folder
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      If objFSO.FolderExists(strFolderOldName) Then _
      objFSO.MoveFolder strFolderOldName, strFolderNewName
      Set objFSO = Nothing
      '2. Create a New Folder or path
      Sub ReplaceVariable(ByRef StringToExtract, _
          ByVal MatchPattern, ByVal ReplacementText)
      	Dim regEx, CurrentMatch, CurrentMatches
      	Set regEx = New RegExp
      	 regEx.Pattern = MatchPattern
      	 regEx.IgnoreCase = True
      	 regEx.Global = True
      	 regEx.MultiLine = True
      	 StringToExtract = regEx.Replace(StringToExtract, _
      	Set regEx = Nothing
      End Sub
      Function MakeDir(strPath)
        Dim strParentPath
        With CreateObject("Scripting.FileSystemObject")
          If Not .FolderExists(strPath) Then
            strParentPath = .GetParentFolderName(strPath)
            If Not .FolderExists(strParentPath) Then MakeDir strParentPath
            If Not .FolderExists(strPath) Then .CreateFolder strPath
          End If 
          MakeDir = .FolderExists(strPath)
        End With
      End Function
      Last edited by Rems; 22nd September 2007, 00:55. Reason: add colors to the script

      This posting is provided "AS IS" with no warranties, and confers no rights.


      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts


      • #4
        Re: rename folder script

        Hi /Rems and Theman...

        Thanks very much for helping me with the script. It's really helpful for my work. I appreciate much.

        Have a great day!


        • #5
          Re: rename folder script

          I want to thank all of you again for making the renaming script for me. It works great. This is for my back up data onto a main folder called "backup". But I want to get a script that delete folders and subfolders that are older than 4 weeks. Deleting them manually takes so much time and attention and sometimes it doesn't delete all files and folders.

          I appreciate your help very much.



          • #6
            Re: rename folder script

            There was something a little while ago that did this...

            but I can't find it for now; but try this thread:
            This is my signature.