?
Solved

VBScript rename folders

Posted on 2006-05-12
5
Medium Priority
?
291 Views
Last Modified: 2012-08-13
hi,

I have a folder, inside that folder, there are many folders, is there a way to loop through the folders(just go over the folder names, not the inside), if the foldername contain "_" then rename the folder before "_",
if the folder name contains space (it could have more than one space, trim off the space...


thx


(_ and space will not occur at the same time)))
0
Comment
Question by:mcrmg
  • 3
5 Comments
 
LVL 23

Expert Comment

by:basicinstinct
ID: 16672390
This works for me: you didn't say what you wanted to rename to, so I just put a string in there that you can change to whatever.  You didn't say whether you wanted the _ removed, it sounded like not, so I have left it.  If you want it removed you could change this:

mid(SubFolder.Name, n)

to this

+ mid(SubFolder.Name, n+1)


Copy the code below and paste it into an empty text file, call it rename.vbs (or something like that) and run it to see it work.

'START CODE
Option Explicit

Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")

Main

Sub Main
   Dim CurrentFolder: Set CurrentFolder = fso.GetFolder(".")
   CheckFolderNames CurrentFolder
End Sub

Sub CheckFolderNames (ByVal Folder)
   Dim SubFolders: Set SubFolders = Folder.SubFolders
   Dim SubFolder
   Dim n, newName
   For Each SubFolder In SubFolders
      n = InStr(SubFolder.Name,"_")
      if (n > 0) then
          newName = "newpart" + mid(SubFolder.Name, n)
        SubFolder.Move fso.GetParentFolderName(SubFolder) & "\" & newName
      else
      n = InStr(SubFolder.Name," ")
      if (n > 0) then
        newName = Replace(SubFolder.Name, " ", "")
        SubFolder.Move fso.GetParentFolderName(SubFolder) & "\" & newName
      end if
      end if
      CheckFolderNames SubFolder
   Next
End Sub
'END CODE
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16672418
Hi basicinstinct,

I think he wanted to keep everything to the left of the underscore or space if there was one present:

    if the foldername contain "_" then rename the folder before "_"

    if the folder name contains space (it could have more than one space, trim off the space

So...

    "Under_line" --> Under
    "Spaced out" --> Spaced

If no underscore or space is present, then keep the current name.

But I could be wrong...  =)
0
 

Author Comment

by:mcrmg
ID: 16674613
I would like the following if possible:


    "Under_line" --> Under
    "Spaced out" --> Spacedout


thx
0
 
LVL 23

Accepted Solution

by:
basicinstinct earned 800 total points
ID: 16675315
Hi, yes, just tweak the code like this:

'START CODE
Option Explicit

Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")

Main

Sub Main
   Dim CurrentFolder: Set CurrentFolder = fso.GetFolder(".")
   CheckFolderNames CurrentFolder
End Sub

Sub CheckFolderNames (ByVal Folder)
   Dim SubFolders: Set SubFolders = Folder.SubFolders
   Dim SubFolder
   Dim n, newName
   For Each SubFolder In SubFolders
      n = InStr(SubFolder.Name,"_")
      if (n > 0) then
          newName = left(SubFolder.Name, n-1)
       SubFolder.Move fso.GetParentFolderName(SubFolder) & "\" & newName
      else
     n = InStr(SubFolder.Name," ")
     if (n > 0) then
       newName = Replace(SubFolder.Name, " ", "")
       SubFolder.Move fso.GetParentFolderName(SubFolder) & "\" & newName
     end if
      end if
      CheckFolderNames SubFolder
   Next
End Sub
'END CODE
0
 
LVL 23

Assisted Solution

by:basicinstinct
basicinstinct earned 800 total points
ID: 16675323
The only difference is this line:

newName = "newpart" + mid(SubFolder.Name, n)

becomes this:

newName = left(SubFolder.Name, n-1)

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month16 days, 18 hours left to enroll

862 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