Solved

Rem SubFolders

Posted on 2014-03-30
13
200 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 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