Solved

Rem SubFolders

Posted on 2014-03-30
13
198 Views
Last Modified: 2014-03-31
Hello All,

How do i rename sub folders within a main directory:

For eg: main directory is "C:\mainThisF"

And there are sub folders within that folder. I want each subfolder to be rename in this manner:

[folder one] becomes [folder one_V2]
[folder second23] becomes [folder second23_V2]

and so forth and each folder name has spaces in its name as shown above
0
Comment
Question by:Rayne
  • 7
  • 5
13 Comments
 
LVL 35

Assisted Solution

by:Dan Craciun
Dan Craciun earned 250 total points
ID: 39965281
This is how you can do it in Powershell:
gci "C:\mainThisF\" -Directory -Recurse | %{Rename-Item "$($_.fullname)" "$($_.fullname)_V2"}

Open in new window

HTH,
Dan
0
 

Author Comment

by:Rayne
ID: 39965289
Hello Dan,

Its not renaming...not sure if I missed anything....but i tried your file..
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39965290
What version of PS do you have?
I tested on 3 and it works. Not sure "-directory" is available in earlier versions.

BTW, you can find what version you have by opening Powershell console or Powershell ISE and typing
Host

Open in new window

0
Industry Leaders: 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!

 

Author Comment

by:Rayne
ID: 39965300
Hello Dan,

Sure,

Windows PowerShell
Copyright (C) 2012 Microsoft Corporation. All rights reserved.

PS C:\Users\ayyyy> Host


Name             : ConsoleHost
Version          : 3.0
InstanceId       : 7dac9493-a31a-41bc-b2d9-e6d71e2fc769
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace



PS C:\Users\ayyyy> Host
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39965315
OK, it should work.
What is the output of this:
gci "C:\mainThisF\" -Directory -Recurse | %{
    echo "$($_.fullname)_V2"
    Rename-Item "$($_.fullname)" "$($_.fullname)_V2"
 }

Open in new window

I've added an echo so you can see the new name.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39965318
Or, better, let us use an whatif (it's Sunday evening...)
gci "C:\mainThisF\" -Directory -Recurse | %{Rename-Item "$($_.fullname)" "$($_.fullname)_V2" -whatif}

Open in new window

0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 250 total points
ID: 39965386
Here is a VBA suggestion:
Sub RenameFolders(strRootFolder As String, strFolderSuffix As String)
    Dim strSubFolder As String
    
    strSubFolder = Dir(strRootFolder & "\*")
    Do Until strSubFolder = ""
        If (GetAttr(strRootFolder & "\" & strSubFolder) And vbDirectory) = vbDirectory Then
            If Left$(strSubFolder, 1) <> "." Then 'home or parent folde
                Name strSubFolder As strSubFolder & "\" & strFolderSuffix
            End If
        End If
        strSubFolder = Dir$()
    Loop
   
End Sub

Open in new window

0
 

Author Comment

by:Rayne
ID: 39965755
Hello Dan,

Its not working  - i am copying your code in a text file and saving as .bat and then double clicking it....no renaming happens


Graham - your code seems to stop after it enter the loop
0
 

Author Comment

by:Rayne
ID: 39965758
This line is "" after getting executed, hence it not entering the loop
strSubFolder = Dir(strRootFolder & "\*")
0
 

Author Closing Comment

by:Rayne
ID: 39965781
thanks all, i used vba
0
 

Author Comment

by:Rayne
ID: 39965782
fso :)
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39965876
OK, now I understand why it did not work :)

You either paste that line into powershell and press enter or save it in a file with the .ps1 extension.

It won't work as a .bat file, simply because it's not written in batch...
0
 

Author Comment

by:Rayne
ID: 39965996
Hmm, my illiteracy :(

Thank you Dan for clarifying...I know now
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
Utilizing an array to gracefully append to a list of EmailAddresses
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

685 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