[RESOLVED] create sub dirs named for all files in parent dir ?
Hi,
What I want sounds like a simple task but I am drawing a blank. (Thing is, I have just enough vbs scripting experience to be dangerous.) Assistance would be appreciated.
I'd like a script that looks at a single folder and creates, within it, a set of new sub folders, one for each of the files that exist there.
I'd like all the new sub folders to be named for the files in the parent folder minus the extension.
So if the original dir of the parent folder is:
aud1.mp3
aud2.mp3
aud3.mp3
aud4.mp3
.
.
.
etc
then the folders created by the script should be named:
aud1
aud2
aud3
aud4
.
.
.
etc
and be in the original parent folder alongside the files.
(It would be good too if the script could ignore any pre existing folders in the parent directory if possible.)
Seems trivial but as I say I have drawn a blank, I hope I have been clear.
Thanks,
Brian H.
Toronto Ont
Re: create sub dirs named for all files in parent dir ?
Just curious why do you want to achieve this...
the reason why I am asking this is because this behaviour is similar to what the W32.DownAdup.B Trojan does... The trojan creates exe files with the folder/subfolder names (you want to create folder instead of files....) within that folder or subfolder...
Ps: BTW, I am no way hinting that you are trying to create a trojan...
Re: create sub dirs named for all files in parent dir ?
Quote:
Just curious why do you want to achieve this...
Well, let me put your mind at rest, I don't think I'd be any threat by writing trojans! I just don't have the scripting savvy / experience to be any good at it at all. ;)
What I am doing is chopping a bunch of long .mp3 files into 5 minute fragments using mp3splt.
I like to break long lecture type mp3's down to 5 min segments so I can skip quickly through and be only 2.5 minutes, max, away from the point I want to listen to. I am too lazy to hold the FF or RR button on my portable device to get to a point midway through a 3 hour lecture.
Once I have the folder named for the various files, I'll get the script to run the command line version of mp3splt against each file in turn using the corresponding folder as the destination.
I have a whole bunch to do all with the same parameters, so I thought I'd make a script to do the work for me. Its quite tedious manually and these machines are supposed to do the work for us aren't they ? :)
Once I have all the folders containing the fragmented mp3's, I can just copy all the folders over to my mp3 player all in one go leaving the original long mp3's behind in the parent directory.
That's all I am up to, honest.
As it turns out, I have surprised myself with a cludging together of a couple of example scripts at Hey Scripting Guy.
Code:
'Script reads file names in a folder and creates a correspondingly named empty direcory
'--------------------------------------------------------------------
'Derrived from:
'List All the Files in a Folder:
'http://www.microsoft.com/technet/scriptcenter/scripts/storage/files/stfivb08.mspx
'Create a Folder:
'http://www.microsoft.com/technet/scriptcenter/scripts/storage/folders/stfovb07.mspx
'--------------------------------------------------------------------
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='C:\test'} Where " _
& "ResultClass = CIM_DataFile")
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objFile In colFileList
'Wscript.Echo objFile.Name
strFN=objFile.Name
ll=len(strFN)-4
strFN=left(strFN,ll)
'Wscript.Echo strFN
Set objFolder = objFSO.CreateFolder(strFN)
Next
Its early yet, but looks promising.
Cheers & thanks for the interest, comments & suggestions welcome!
Brian H.
Re: create sub dirs named for all files in parent dir ?
The vertical slider bar on my code section worked fine in pre post preview mode. I don't understand why it doesn't work in the post. Is it a browser issue ?
If you really want my code, I'll send it. Let me know.
Brian H.
Re: create sub dirs named for all files in parent dir ?
Okay that puts my mind at rest :)
There are plenty of example in the forums which will tell you how to loop thru a folder and see what files they contain. once you get that, it is fairly simple getting the name of the file, stripping the extention and creating a folder if the folder doesn't exist. Do a search, if you get stuck let me know, i will post a sample code...
Re: create sub dirs named for all files in parent dir ?
Here's the entire program, soup to nuts:
Code:
Option Explicit
Sub Main()
On Error Resume Next
Dim strFile As String
Dim strBase As String
strBase = App.Path
If Right(strBase, 1) <> "\" Then strBase = strBase & "\"
strFile = Dir("*.mp3")
Do While Len(strFile) <> 0
MkDir strBase & Left(strFile, Len(strFile) - 4)
strFile = Dir
Loop
End Sub
Create a new project, remove the form, add a module, copy the above code to that module, then set the startup object to Sub Main. Compile to an exe and you're done.
To make it work, copy the exe to whichever folder you want to process and run the exe.