Solved

Script or Batch to Create New Folders within Folders Automatically on File Server

Posted on 2009-05-06
16
596 Views
Last Modified: 2013-12-02
First off, not too familar with scripting, just basic batch files to map network drives.

I need a script or batch, or some automated way to create 3 folders called: Individual, Group, Family.

These folders need to be placed in an already existing folder, called: Clinical Progress Notes.

The Clinical Progress Notes folder is located in various different folders and directories on our file server. For example: it can be in
\\fileserver\company\Dir1\Location\Person\Clinical Progress Notes
or it can be  in
\\fileserver\company\Dir78\Location21\Person\Clinical Progress notes

etc...
The only thing that stays the same is \\filerserver\company  any following directories can change.

So something along the lines of 'If Clinical Progress Notes Folder exists, then create folders Individual, Group, and Family" and "if the 3 folders already exist, then ignore" (so I dont want any conflicts with the folders if they already exist.

Thanks
0
Comment
Question by:fireguy1125
  • 9
  • 7
16 Comments
 
LVL 10

Accepted Solution

by:
TakedaT earned 500 total points
ID: 24318899
Give this a go.  Just change the strSource path on the second line to the actual path.
dim objFSO : set objFSO = CreateObject("Scripting.FileSystemObject")

dim strSource : strSource = "\\fileserver\company\"

dim strFolderName : strFolderName = "Clinical Progress Notes"

dim arrFolders(2)

arrFolders(0) = "Individual" : arrFolders(1) = "Group" : arrFolders(2) = "Family"

Set objDir = objFSO.GetFolder(strSource)
 

If not objFSO.FolderExists(strSource) then

	MsgBox "Source folder does not exist"

	wscript.quit

End If

SearchDir objDir,strFolderName

'For x=0 to 2

	'strCopyFrom = ""

	'SearchDir objDir,arrFolders(x)

	'If strCopyFrom <> "" then

	'	objFSO.CopyFile strCopyFrom,strDest

	'End If

'Next

Function SearchDir(strCurrentDir,strFolderName)

	For Each folder In strCurrentDir.SubFolders

		strCurDir =	mid(folder,instrrev(folder,"\")+1)

		If strCurDir = strFolderName then

			For i=0 to 2

				If not objFSO.FolderExists(folder&"\"&arrFolders(i)) then

					objFSO.CreateFolder(folder&"\"&arrFolders(i))

				End If

			Next

		End If

		SearchDir folder,strFolderName

	Next

End Function

Open in new window

0
 
LVL 10

Expert Comment

by:TakedaT
ID: 24318907
It is a .vbs script BTW.
0
 
LVL 1

Author Comment

by:fireguy1125
ID: 24319428
ok, i saved it as foldercreatescript.vbs on the desktop of the file server.  double clicked it, and nothing.  looking at cpu usage though it's up there with things like lsass.exe, system.exe, services.exe and wscript.exe using the bulk of it.  i'm assuming this means it's running and it's going to take a while, since it has to do this to around 2,000 folders?
0
 
LVL 10

Expert Comment

by:TakedaT
ID: 24319462
It may take a minute or two.  But that should be it.  Its only creating folders.  Did you make sure to give it a valid path in line 2?
0
 
LVL 1

Author Comment

by:fireguy1125
ID: 24319579
Yes, it still looks like it's running.  I made the appropriate change to the initial top folder as \\fileserver\company\share (with our actual path name)

of couse beneith that are several folders in the share folder, and several folders in those folders, then several more folders, until it actually reaches the Clinical Progress Notes Folder, since the location of the Clinical Progress Notes folder can be in thousands of other folders (peoples names and different locations)
0
 
LVL 10

Expert Comment

by:TakedaT
ID: 24319659
It would run much quicker if it was ran from the local machine where these folders are, but it should still work nonetheless.  Looking through a few thousand folders accross the network may take some time.  I initially forgot that it was a networked server when I said only a few minutes.

Also, I forgot to delete lines 13 to 19.  They are commented out anyway so they wont hurt, but you can replace them with :

MsgBox "Done"

to let you know when it is finished.
0
 
LVL 1

Author Comment

by:fireguy1125
ID: 24325230
Ok it worked! CPU back to normal today, and folders are created everywhere.  However, I ran into a little problem, i'm sure you can assist me with as well.  I had the script run in a section of folders that is no longer used (only for archive purposes), so the folders weren't necessary to create there.  No big deal, but that's probably why it took a lot longer.  Any way to get a script to delete the 3 folders only from the directory: \\fileserver\company\share\old\2008 and going back to 2004 same path. if you want me to open up new question i can do that.
0
 
LVL 10

Expert Comment

by:TakedaT
ID: 24325322
I can write that for you.  But it will delete ALL folders with those folder names, even those that were there before that script run yesterday.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 1

Author Comment

by:fireguy1125
ID: 24325379
That should be fine then, as long as I put the strSource to the subdirectory that the folders shouldn't be in correct?  As long as I put it in \\fileserver\company\share\old\2008 then run it in \\fileserver\company\share\old\2007 and so on.  This shouldn't delete anything before that folder? Which we created with the previous script, which was applied to \\fileserver\company\share.
0
 
LVL 10

Expert Comment

by:TakedaT
ID: 24325415
Yup...the search will begin at the strSource folder, nothing above is touched.  But, I can also have it check to make sure the folders are empty if you would like.  Sound good?
0
 
LVL 1

Author Comment

by:fireguy1125
ID: 24325443
Sure sounds great!
0
 
LVL 10

Expert Comment

by:TakedaT
ID: 24325463
OK..Just give me a little while.  I have some work to do here first.
0
 
LVL 10

Expert Comment

by:TakedaT
ID: 24325803
Try this.
dim objFSO : set objFSO = CreateObject("Scripting.FileSystemObject")

dim strSource : strSource = "D:\test\New Folder"'"\\fileserver\company\share\old\2008"

dim strFolderName : strFolderName = "Clinical Progress Notes"

dim arrFolders(2)

arrFolders(0) = "Individual" : arrFolders(1) = "Group" : arrFolders(2) = "Family"

Set objDir = objFSO.GetFolder(strSource)

 

If not objFSO.FolderExists(strSource) then

        MsgBox "Source folder does not exist"

        wscript.quit

End If

SearchDir objDir,strFolderName

MsgBox "Done"

Function SearchDir(strCurrentDir,strFolderName)

        For Each folder In strCurrentDir.SubFolders

			strCurDir = mid(folder,instrrev(folder,"\")+1)

			strParentDir = mid(replace(folder,"\"&strCurDir,""),instrrev(replace(folder,"\"&strCurDir,""),"\")+1)'mid(folder,instrrev(folder,"\")+1)

	        For i=0 to 2

	        	If strCurDir = arrFolders(i) and strParentDir = strFolderName then

	        		If CheckForFiles(folder) = False then

	        			objFSO.DeleteFolder(folder)

	        		End If

	        	End If

	        Next

        Next

        For Each folder In strCurrentDir.SubFolders

        	SearchDir folder,strFolderName

        Next

End Function

Function CheckForFiles(dir)

	CheckForFiles = False

	dim objDir2 : set objDir2 = objFSO.GetFolder(dir)

	For Each file2 In objDir2.Files

		CheckForFiles=True

	Next

End Function

Open in new window

0
 
LVL 1

Author Comment

by:fireguy1125
ID: 24325966
thanks, line 2 has 2 different sources? what's the "d:\test\new folder" for?
0
 
LVL 10

Expert Comment

by:TakedaT
ID: 24326633
Sorry, had to run to a meeting.  The second one is commented out with the'.  It was just for when I was testing.  You can delete both and put in what you need in quotes.
0
 
LVL 1

Author Comment

by:fireguy1125
ID: 24326687
Works like a charm! Thanks for all your help!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
If, like me, you have a lot of Dell servers in the estate you manage this article should save you a little time. When attempting to login to iDrac on any server I would be presented with two errors. The first reads "Do you want to run this applicati…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now