Batch rename folders

rdgfilings
rdgfilings used Ask the Experts™
on
There are over 300 sub-folders in one folder all have the same name formatting:
xxxx-xxxxxxx.yyyy.xxxx
,where "x"s are various number of characters, "yyyy" is year number (2012 for example). I want rename them to:
xxxxxxx.yyyy (remove anything before and including "-", and remove anything after year).

Is there a fast way to do it?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I think VBS might work the best for you.

Use this to actually rename the folders
Renaming Folders
Renames the folder C:\Scripts to C:\Repository.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory where name = 'c:\\Scripts'")
For Each objFolder in colFolders
    errResults = objFolder.Rename("C:\Script Repository")
    Wscript.Echo errResults
Next

Open in new window


Use this to nab the folder name
Retrieving Folder Properties
Lists the properties of the folder C:\Scripts.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService. _
    ExecQuery("Select * from Win32_Directory where name = 'c:\\Scripts'")
For Each objFolder in colFolders
    Wscript.Echo "Archive: " & objFolder.Archive
    Wscript.Echo "Caption: " & objFolder.Caption
    Wscript.Echo "Compressed: " & objFolder.Compressed
    Wscript.Echo "Compression method: " & objFolder.CompressionMethod
    Wscript.Echo "Creation date: " & objFolder.CreationDate
    Wscript.Echo "Encrypted: " & objFolder.Encrypted
    Wscript.Echo "Encryption method: " & objFolder.EncryptionMethod
    Wscript.Echo "Hidden: " & objFolder.Hidden
    Wscript.Echo "In use count: " & objFolder.InUseCount
    Wscript.Echo "Last accessed: " & objFolder.LastAccessed
    Wscript.Echo "Last modified: " & objFolder.LastModified
    Wscript.Echo "Name: " & objFolder.Name
    Wscript.Echo "Path: " & objFolder.Path
    Wscript.Echo "Readable: " & objFolder.Readable
    Wscript.Echo "System: " & objFolder.System
    Wscript.Echo "Writeable: " & objFolder.Writeable
Next

Open in new window


You are going to have some logic in there that copies the folder name into a string field, manipulates it, then actually renames the folder to the new string name. I'm not quite sure on how to manipulate a string in there yet, though.
Are these folder names always the same length?
If they are, we could use the MID command in VBS   :)

http://www.w3schools.com/vbscript/func_mid.asp

Author

Commented:
thanks, IT_Crowd. I will test the codes. The folder names are different in length (i.e. the number of "x"s is uncertain, the only certain number is "yyyy" - always 4)
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
In PowerShell:
gci C:\YourStartFolder -Filter "*-*.*-*" | ? {$_.PsIsContainer} | 
  rename-item -newname {$_.Name -replace ".*-(.*\..*)-.*", '$1'}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial