Solved

Rem SubFolders

Posted on 2014-03-30
13
194 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

14 Experts available now in Live!

Get 1:1 Help Now