Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Rename Extensions in subfolders

Rename Extensions in subfolders

Scheduled Pinned Locked Moved Visual Basic
questiontoolshelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Serpent666
    wrote on last edited by
    #1

    Hey all, not 100% sure if this is the right forum for Visual Basic Scripting but i can only assume it is. Basicly put my current script will let you type a directory into an input box e.g C:\Temp\Files then it will rename every file in there with the extension .txt to .bak while all that works great, i have subfolders and it will not change the extensions of the txt files in them here is my current code for you to look at

    FILEPATH=inputbox("What is the directory?")

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    Set FileDirectory = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='" & FILEPATH & "'} Where " _
    & "ResultClass = CIM_DataFile")

    For Each objFile In FileDirectory
    If objFile.Extension = "txt" Then
    strbak = objFile.Drive & objFile.Path & objFile.FileName & "." & "bak"
    objFile.Copy(strbak)
    End If
    Next

    msgbox "Complete."

    im pretty sure i have to use Win32_SubDirectory.Name but i have tried to implement it and failed so can someone please help me to get my script to change subfolder file extensions?

    B 1 Reply Last reply
    0
    • S Serpent666

      Hey all, not 100% sure if this is the right forum for Visual Basic Scripting but i can only assume it is. Basicly put my current script will let you type a directory into an input box e.g C:\Temp\Files then it will rename every file in there with the extension .txt to .bak while all that works great, i have subfolders and it will not change the extensions of the txt files in them here is my current code for you to look at

      FILEPATH=inputbox("What is the directory?")

      strComputer = "."
      Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

      Set FileDirectory = objWMIService.ExecQuery _
      ("ASSOCIATORS OF {Win32_Directory.Name='" & FILEPATH & "'} Where " _
      & "ResultClass = CIM_DataFile")

      For Each objFile In FileDirectory
      If objFile.Extension = "txt" Then
      strbak = objFile.Drive & objFile.Path & objFile.FileName & "." & "bak"
      objFile.Copy(strbak)
      End If
      Next

      msgbox "Complete."

      im pretty sure i have to use Win32_SubDirectory.Name but i have tried to implement it and failed so can someone please help me to get my script to change subfolder file extensions?

      B Offline
      B Offline
      Ben Fair
      wrote on last edited by
      #2

      I don't believe there is a built in mechanism for simulating following the file system hierarchy, so you'll have to create your own. You need to put the logic of renaming all the .txt files to .bak in a folder into a sub and then use recursion to walk the directory structure. So, you'll have a sub that accepts either a folder object or a string that is the path (your choice) and then within the sub you'll process all the files and then have it call itself for each folder within the supplied path. Here's some pseudocode:

      sub ProcessFolder(folder)
      foreach(file in folder.files)
      rename file...

      foreach(folder in folder.folders)
          ProcessFolder(folder)
      

      end sub

      From your main script you'll just call the sub with for the root level of folder and then let it handle everything from there.

      Keep It Simple Stupid! (KISS)

      S 1 Reply Last reply
      0
      • B Ben Fair

        I don't believe there is a built in mechanism for simulating following the file system hierarchy, so you'll have to create your own. You need to put the logic of renaming all the .txt files to .bak in a folder into a sub and then use recursion to walk the directory structure. So, you'll have a sub that accepts either a folder object or a string that is the path (your choice) and then within the sub you'll process all the files and then have it call itself for each folder within the supplied path. Here's some pseudocode:

        sub ProcessFolder(folder)
        foreach(file in folder.files)
        rename file...

        foreach(folder in folder.folders)
            ProcessFolder(folder)
        

        end sub

        From your main script you'll just call the sub with for the root level of folder and then let it handle everything from there.

        Keep It Simple Stupid! (KISS)

        S Offline
        S Offline
        Serpent666
        wrote on last edited by
        #3

        i got it working after finding some code that monitors a folder including subfolders i just changed it around so that it checked for the extention and renamed it

        strComputer = "."
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        strFolderName=inputbox("ENTER THE FULL BACKUP PATH")
        msgbox "A messagebox will display on completion, this may take some time"

        Set colSubfolders = objWMIService.ExecQuery _
        ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
        & "Where AssocClass = Win32_Subdirectory " _
        & "ResultRole = PartComponent")

        arrFolderPath = Split(strFolderName, "\")
        strNewPath = ""
        For i = 1 to Ubound(arrFolderPath)
        strNewPath = strNewPath & "\\" & arrFolderPath(i)
        Next
        strPath = strNewPath & "\\"

        Set colFiles = objWMIService.ExecQuery _
        ("Select * from CIM_DataFile where Path = '" & strPath & "'")

        For Each objFile in colFiles
        If objFile.Extension = "txt" Then
        strBACKUP = objFile.Drive & objFile.Path & objFile.FileName & "." & "bak"
        objFile.Copy(strBACKUP)
        End If
        Next

        For Each objFolder in colSubfolders
        GetSubFolders strFolderName
        Next

        Sub GetSubFolders(strFolderName)
        Set colSubfolders2 = objWMIService.ExecQuery _
        ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
        & "Where AssocClass = Win32_Subdirectory " _
        & "ResultRole = PartComponent")

        For Each objFolder2 in colSubfolders2
            strFolderName = objFolder2.Name
            arrFolderPath = Split(strFolderName, "\\")
            strNewPath = ""
            For i = 1 to Ubound(arrFolderPath)
                strNewPath = strNewPath & "\\\\" & arrFolderPath(i)
            Next
            strPath = strNewPath & "\\\\"
        
            Set colFiles = objWMIService.ExecQuery \_
                ("Select \* from CIM\_DataFile where Path = '" & strPath & "'")
        
            For Each objFile in colFiles
          If objFile.Extension = "txt" Then
            strBACKUP = objFile.Drive & objFile.Path & objFile.FileName & "." & "bak"
        objFile.Copy(strBACKUP)
        End If
            Next
        
            GetSubFolders strFolderName
        Next
        

        End Sub

        thanks for the help!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups