Creating Subdirectories

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
bmichael
Newbie
Posts: 8
Joined: Wed Dec 28, 2005 1:40 am
Location: Orange County, CA
Contact:

Creating Subdirectories

Post by bmichael » Wed Dec 28, 2005 6:50 pm

Is there an easy way to create a subdirectory rather than through multiple statements?
i.e.
createdir c:\test\username\docs1\marketing\graphics
(Does not work unless all of the directories already exist)

I have had to do this:

createdir c:\test\
createdir c:\test\username\
createdir c:\test\username\docs1\
createdir c:\test\username\docs1\marketing\
createdir c:\test\username\docs1\marketing\graphics

Although that is a feasible solution for one folder, it is very inefficient when variables are involved in the names, and ultimately many directories need to be created in the file copy scheme that I need to create.

I've looked on the forums, and haven't found a better way. Is there?
Thanks.
Bruce.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed Dec 28, 2005 7:04 pm

The quickest method that comes to mind is to use the DOS mkdir command:

Let>newdir=c:\test\username\docs1\marketing\graphics
Let>RP_WAIT=1
Let>RP_WINDOWMODE=0
Run>cmd.exe /c mkdir %newdir%
//replace cmd.exe with command.com if using Win9x

Or a nice recursive technique:

VBSTART
Sub CreateFullPath(path)
dim parent
set fso = CreateObject("Scripting.FileSystemObject")

path = fso.GetAbsolutePathname(path)
parent = fso.GetParentFolderName(path)

if not fso.FolderExists(parent) then
CreateFullPath parent
end if

if not fso.FolderExists(path) then
fso.CreateFolder(path)
end if
End Sub
VBEND
VBRun>CreateFullPath,c:\documents and settings\user\test\new\foo
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

bmichael
Newbie
Posts: 8
Joined: Wed Dec 28, 2005 1:40 am
Location: Orange County, CA
Contact:

the recursive VBscript is slick

Post by bmichael » Wed Dec 28, 2005 7:30 pm

Can I use Macro to pass a variable to the VBRun command?

VBRun>CreateFullPath,Variable_created_in_Macro

bmichael
Newbie
Posts: 8
Joined: Wed Dec 28, 2005 1:40 am
Location: Orange County, CA
Contact:

Figured it out!

Post by bmichael » Wed Dec 28, 2005 8:12 pm

Figured out the Variable question... easy enough to pass the variable from Macro... This allows me to create multiple paths to create and pass them through your VB script as a sub. Thanks!

Let>create_path=c:\test\username\test_user\did it work\
Gosub>DirCreate

SRT>DirCreate
VBSTART
Sub CreateFullPath(path)
dim parent
set fso = CreateObject("Scripting.FileSystemObject")

path = fso.GetAbsolutePathname(path)
parent = fso.GetParentFolderName(path)

if not fso.FolderExists(parent) then
CreateFullPath parent
end if

if not fso.FolderExists(path) then
fso.CreateFolder(path)
end if
End Sub
VBEND

VBRun>CreateFullPath,%create_path%
End>

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed Dec 28, 2005 9:15 pm

Hi,

You don't need to call the entire VBScript block more than once. Once you have declared the VBScript subroutine in the VBSTART/VBEND block it can be called any number of times with the VBRun function. You should rewrite your script like this:


VBSTART
Sub CreateFullPath(path)
dim parent
set fso = CreateObject("Scripting.FileSystemObject")

path = fso.GetAbsolutePathname(path)
parent = fso.GetParentFolderName(path)

if not fso.FolderExists(parent) then
CreateFullPath parent
end if

if not fso.FolderExists(path) then
fso.CreateFolder(path)
end if
End Sub
VBEND

Let>create_path=c:\test\username\test_user\did it work\
VBRun>CreateFullPath,create_path

//you can call CreateFullPath again and again and again ...

Let>create_path=c:\test\somedir\anotherplace\
VBRun>CreateFullPath,create_path

//and again if we like ...

Let>newdir=d:\newdir\subdir
VBRun>CreateFullPath,newdir

Also note that you do not need to put the variable in % symbols since it appears on it's own.

I always put my VBSTART/VBEND code at the top of the script. Then I can call any VBScript functions and subroutines in it at any time and any number of times from the rest of the script using VBRun/VBEval. It is wasteful to redeclare the same VBScript code more than once ...
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

bmichael
Newbie
Posts: 8
Joined: Wed Dec 28, 2005 1:40 am
Location: Orange County, CA
Contact:

THANKS!

Post by bmichael » Thu Dec 29, 2005 12:36 am

Thanks! I understand that I am not programming efficiently. I am just learning this scripting. This is my first attempt at something useful for us since I had our IT staff buy this software. I have changed the VB script as you suggested, and am posting what I've done so far in case anyone else wants to use it. We need to capture personal windows settings for a lot of users for a server migration, so this was written to move their data temporarily up to the network.

/* Program to Copy Critical Data from a user's local MS Window's system
to their Network home directory - Good for backup for local information
or when workstations need to be ghosted */

/* Script to Create Subdirectory Structure */
VBSTART
Sub CreateFullPath(path)
dim parent
set fso = CreateObject("Scripting.FileSystemObject")

path = fso.GetAbsolutePathname(path)
parent = fso.GetParentFolderName(path)

if not fso.FolderExists(parent) then
CreateFullPath parent
end if

if not fso.FolderExists(path) then
fso.CreateFolder(path)
end if
End Sub
VBEND

Let>CF_OVERWRITE=1

/* Copy RegEdit Settings that we want for later... */
Let>CopyDir_reg=U:\Profiles\%User_Name%\Registry\
VBRun>CreateFullPath,%CopyDir_Reg%
Run Program>REGEDIT /E %CopyDir_Reg%\Reg1.REG "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\IntelliForms\SPW"

/* All Subdirectories that need to be copied - Add to Array */
Let>SubDir[1]=C:\Documents and Settings\%User_Name%\My Documents
Let>CopyDir[1]=U:\Profiles\%User_Name%\My Documents\

Let>SubDir[2]=C:\Documents and Settings\%User_Name%\Favorites\
Let>CopyDir[2]=U:\Profiles\%User_Name%\Favorites\

Let>SubDir[3]=C:\Documents and Settings\%User_Name%\Desktop\
Let>CopyDir[3]=U:\Profiles\%User_Name%\Desktop\

/* Previous network Paths */
Let>SubDir[4]=C:\Documents and Settings\%User_Name%\Cookies\
Let>CopyDir[4]=U:\Profiles\%User_Name%\Cookies\

/* Includes Outlook .PST's if stored locally, and usual location for Archives */
Let>SubDir[5]=C:\Documents and Settings\%User_Name%\Local Settings\Application Data\Microsoft\Outlook
Let>CopyDir[5]=U:\Profiles\%User_Name%\Local Settings\Application Data\Microsoft\Outlook

/* Includes Normal.Dot, signatures, utlook AutoComplete, toolBar & Menu Settings */
Let>SubDir[6]=C:\Documents and Settings\%User_Name%\Application Data\Microsoft
Let>CopyDir[6]=U:\Profiles\%User_Name%\Application Data\Microsoft

/* Previous network Paths */
Let>SubDir[7]=C:\Documents and Settings\%User_Name%\NetHood\
Let>CopyDir[7]=U:\Profiles\%User_Name%\NetHood\

/* Create the directory structure first, then copy files */
Let>count=1
GOSUB>Begin_Loop_to_create_directory_structure

SRT>Begin_Loop_to_create_directory_structure
If>count>7
GOSUB>End_Loop_to_create_directory_structure
Else
Let>CopyPath=CopyDir[%count%]
VBRun>CreateFullPath,%CopyPath%

Let>SourcePath=SubDir[%count%]
Let>SourcePathFull=%SourcePath%*.*
CopyFile>%SourcePathFull%,%CopyPath%
Let>count=count+1
GOSUB>Begin_Loop_to_create_directory_structure
EndIf
End>

/* SUBROUTINE - Kick out of the Array Loop */
SRT>End_Loop_to_create_directory_structure
Message>Done!
End>

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts