Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Rem SubFolders

Posted on 2014-03-30
13
Medium Priority
?
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 35

Assisted Solution

by:Dan Craciun
Dan Craciun earned 1000 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
Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 

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 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

722 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