Solved

Rem SubFolders

Posted on 2014-03-30
13
195 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 34

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 34

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
 

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 34

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 34

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 34

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
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 …

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now