Solved

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

Posted on 2009-05-06
16
594 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
Comment Utility
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
Comment Utility
It is a .vbs script BTW.
0
 
LVL 1

Author Comment

by:fireguy1125
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:fireguy1125
Comment Utility
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
Comment Utility
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
Comment Utility
Sure sounds great!
0
 
LVL 10

Expert Comment

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

Expert Comment

by:TakedaT
Comment Utility
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
Comment Utility
thanks, line 2 has 2 different sources? what's the "d:\test\new folder" for?
0
 
LVL 10

Expert Comment

by:TakedaT
Comment Utility
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
Comment Utility
Works like a charm! Thanks for all your help!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

14 Experts available now in Live!

Get 1:1 Help Now