Rename file to folder name

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 15 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
paradies
Posts: 2
Last visit: Mon Apr 07, 2008 6:26 am

Rename file to folder name

Post by paradies »

Hey everyone pretty new to this scripting thing so I thought I'd ask to see if there is quick answer (is there ever though?) Anyway I have a bunch of folders each with only 1 file in them. I want to rename the files to same name of folder they are contained in.Ex. 1234one.doc -> 12341234.docI have about 1500 folders/files that I need to do this to. Any help would be much appreciated. Thanks
User avatar
paradies
Posts: 2
Last visit: Mon Apr 07, 2008 6:26 am

Rename file to folder name

Post by paradies »

Hey everyone pretty new to this scripting thing so I thought I'd ask to see if there is quick answer (is there ever though?) Anyway I have a bunch of folders each with only 1 file in them. I want to rename the files to same name of folder they are contained in.Ex. 1234one.doc -> 12341234.docI have about 1500 folders/files that I need to do this to. Any help would be much appreciated. Thanks
User avatar
donj
Posts: 416
Last visit: Thu May 29, 2008 5:08 am

Rename file to folder name

Post by donj »

Sure... typing off the top of my head to the syntax may not be quite right. Assuming your folders are all contained in D:Folders..

Set fso = createobject("Scripting.FileSystemObject")
Set root = fso.getfolder("D:folder")
for each folder in root.subfolders
foldername = folder.name
for each file in folder.files
file.name = foldername
next
next

Something like that. That won't maintain the filename extension, but you could grab it

ext = right(file.name,4)
file.name = foldername & ext

Something like that. Not sure exactly what you want to achieve, but hopefully this is a starter.
User avatar
donj
Posts: 416
Last visit: Thu May 29, 2008 5:08 am

Rename file to folder name

Post by donj »

Probably not :). I'm actually sure it's easier to do in PowerShell - possibly a one-liner - but in this case I think the VBScript is pretty clear to read, and I'm glad it did the job.
This topic is 15 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked