Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a Folder

Status
Not open for further replies.

tincup23

IS-IT--Management
Jan 13, 2010
7
US
Hi, I'm new to programming an I would like to know how I could create a folder on an existing directory structure. I have a need to create a folder in a directory which has subfolders. I would like to have the script read from the directory and place the folder where I would like. I have over 1000+ folders from which this directory must sort.

I.E - C:\Home\From\Folder (the folder has subfolders)
\To\Folder
this for an FTP Site - clients login and they have two root folders which never change (To and From) and inside of those folders are subfolders. I would like to be able to place a folder into either To or From folders. Presently, I have to go thru each clients To and From folders to add a folder.

Any help would be most appreciated and grateful.

Thanks,
 
What's the reason you have to go through every folder - or do your mean manually.

to automate this

Code:
set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
set objClientHomeFolders = objFSO.GetFolder("C:\Home\Clients").SubFolders

for each objFolder in objClientHomeFolders
    objFSO.CreateFolder objFolder.Path & "\To\Foldername"
    objFSO.CreateFolder objFolder.Path & "\From\Foldername"
next

Is this what you are looking for?

-Dan
 
Thannks Dan for you quick response, the reason I must go thru every folder is each folder is a clients home folder (\UserHome\To\folder or \UserHome\From\Folder)

In order for me to place a folder in the To or From Directory, I have to go to every users home folder, then open up the To or From folder then place the folder in that folder.

The script would need to be able to read all directories and based on whether it's To or From folder chosen, then place a new folder.

Sorry, I hope I explain that clearly enough, I apologize, I'm not a programmer so I don't know how to decipher that code. However, I'm learning.....
 
...based on whether it's To or From folder chosen..."
Who chooses the To or From folder?

The above script will iterate all subfolders in c:\Home\Clients and add a folder named "foldername" to both the To and From folder - however, both To and From must already exists or the script will error out.

-Geates (Dan)
 
Thanks Dan, I was able to decipher the code a bit(with the aide of boodk and online forums). Yes, the to or from folder is chosen by the user - either me or someone would decide we need to create a new folder in the To or From directory.

I was able to use the script you provided to create new folders. However, is there a way to provide error checking if the folder already exists and have the script continue on to the next folder? Is there a way to have it print out to a list of the results? With a 1000+ folders, I would liike to know which ones were or weren't there.

Thanks Dan for all your gracious help.
 
You can use

Code:
If Not objFSO.FolderExists("folderNameToCheckFor") Then
    objFSO.CreateFolder("folderNameToCheckFor")
End If

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Thanks Foada,

How exactly would I implement that If statement? I'm very new to programming and I'm learning. Thanks for your assistance. Using the code previously posted, show me what it would look like as to the results I'm seeking.
 
Something like this
Code:
set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
set objClientHomeFolders = objFSO.GetFolder("C:\Home\Clients").SubFolders

for each objFolder in objClientHomeFolders
    If Not objFSO.FolderExist(objFolder.Path & "\To\Foldername" Then
        objFSO.CreateFolder objFolder.Path & "\To\Foldername"
    End If
    If Not objFSO.FolderExist(objFolder.Path & "\From\Foldername" Then
        objFSO.CreateFolder objFolder.Path & "\From\Foldername"
    End If
next

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Thanks Foada, I tried you're code; however, it's returning an error: Expected ')' . Would this be because of the ( between objFSO.FolderExist(objFolder.Path statement?

Also, how could this be modified to print out a list of what folders had folders added and the ones where no folder was added?

Thanks again,

 
Sorry about the typo
Code:
Const mc_strLogFile = "C:\Log.txt"

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objClientHomeFolders = objFSO.GetFolder("C:\Projects").SubFolders

If Not objFSO.FileExists(mc_strLogFile) Then
	objFSO.CreateTextFile(mc_strLogFile)
End If

Set objFile = objFSO.GetFile(mc_strLogFile)
Set objStream = objFile.OpenAsTextStream(2)

For Each objFolder in objClientHomeFolders
    If Not objFSO.FolderExists(objFolder.Path & "\Foldername") Then
        objFSO.CreateFolder(objFolder.Path & "\Foldername")
        objStream.WriteLine(objFolder.Path & "\Foldername")
    End If
    If Not objFSO.FolderExists(objFolder.Path & "\Foldername") Then
        objFSO.CreateFolder(objFolder.Path & "\Foldername")
        objStream.WriteLine(objFolder.Path & "\Foldername")
    End If
Next



If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Thanks Foada, It works perfectly, thanks for saving me from having to input over a 1000+ folders manually. This programming stuff is really cool. I can't wait until I'm as verse as you. Thanks again!


BTW, if I wanted improve upon this (not that I'm capable), let's say to have it rename or correct mispelled folders (folde should be folder) and or have it placed the correct folder and copy it's contents from the older foldet to the new folder and then delete the old folder. Is this possible?
 
Foada,

One more quick question? This script only works if the directory path exist. If had something like C:\Projects\To Folder\ - this would copy any "foldername" into the To Folder directory. So, if the To Folder path isn't there then it doesn't copy the "foldername". How would I get it to create the To Folder and copy the "Foldername" into it?

I've run the revised script; however, if the folder not found it stops. Therefore, I have to go back and create it in that directory, then re-run the script until it finds antother missing folder path.

Also, could you give me a script that would list all folders and subfolders in directory or drive? (i.e. D:\Projects -> need to list all folders and subfolder within this network shared drive.

Thanks again Foada, I'm sorry I've been bothersome and I'm not as learned on this. Hopefully, some day I will. Thanks for the article, it's helpful and good reading.
 
You can use the objFSO.FolderExists method to check if the folder exists. Have a look at this:
Code:
Const mc_strDirectory = "C:\Folder1\Folder2\Folder3\Folder4\Folder5\"
Dim iX
Dim iStart
Dim sTmp
Dim objFSO

Set objFSO = CreateObject("Scripting.FileSystemObject")
'This will check for a network unc path
iStart = 4
If Left(mc_strDirectory, 2) = "\\" Then 
	iStart = InStr(3, mc_strDirectory, "\") + 1
End If

'Create the folders for the path
If Not objFSO.FolderExists(mc_strDirectory) Then
	For iX = iStart to Len(mc_strDirectory)
		iX = InStr(iX, mc_strDirectory, "\")
		sTmp = Left(mc_strDirectory, iX)
		If Not objFSO.FolderExists(sTmp) Then
			objFSO.CreateFolder(sTmp)
		End If
	Next
End If

You are going to need to put together the pieces from the given code samples to customize it fit your app but you should have everything that you need. I don't know all the details of your app so it is difficult to write the whole thing for you and that is not really what this forum is for.

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top