Create folder in vbscript

R

Richard M.

Hi,

I´m using vbscript, and the problem is if i put inthe
script
fs.CreateFolder(""c:\test"), the folder was created.
If i put
fs.CreateFolder(""c:\Program Files\xpto\123\client\") i
had an error "path not found".

He only creates one directory in c:, but doesn´t create
the subdirectories.

Please help, it´s URGENT

Best Regards
 
L

LiquidJ

Richard, you're going to have to check for the existance (or non-existance
as the case may be) of each subdirectory before calling CreateFolder(). In
your example, if any of the "Program Files", "xpto", or "123" directories do
not exist, you will not be able to create a "\Program Files\xpto\123\client"
directory using CreateFolder().


{L}



Hi,

I´m using vbscript, and the problem is if i put inthe
script
fs.CreateFolder(""c:\test"), the folder was created.
If i put
fs.CreateFolder(""c:\Program Files\xpto\123\client\") i
had an error "path not found".

He only creates one directory in c:, but doesn´t create
the subdirectories.

Please help, it´s URGENT

Best Regards
 
R

Richard M.

The only folder that exists is "Program Files".
How can i create the other subdirectories.
Can you give me an example?

Thanks,
 
L

LiquidJ

The same way you're trying to create the "client" directory...

Pseudocode:
If not exists (c:\Program Files)
CreateFolder(c:\Program Files)
end if

if not exists (c:\Program Files\xpto)
CreateFolder(c:\Program Files\xpto)
endif

if not exists (c:\program files\xpto\123)
CreateFolder(c:\program files\xpto\123)
endif

if not exists (c:\program files\xpto\123\client)
CreateFolder(c:\program files\xpto\123\client)
endif


Of course, the previous example can be cleaned up and optimized greatly, but
it shows the basic idea...


{L}



The only folder that exists is "Program Files".
How can i create the other subdirectories.
Can you give me an example?

Thanks,
 
R

Ricrard M.

Thanks.
-----Original Message-----
The same way you're trying to create the "client" directory...

Pseudocode:
If not exists (c:\Program Files)
CreateFolder(c:\Program Files)
end if

if not exists (c:\Program Files\xpto)
CreateFolder(c:\Program Files\xpto)
endif

if not exists (c:\program files\xpto\123)
CreateFolder(c:\program files\xpto\123)
endif

if not exists (c:\program files\xpto\123\client)
CreateFolder(c:\program files\xpto\123\client)
endif


Of course, the previous example can be cleaned up and optimized greatly, but
it shows the basic idea...


{L}



The only folder that exists is "Program Files".
How can i create the other subdirectories.
Can you give me an example?

Thanks,



.
 
D

Dave

Hi all,
heres a recursive proceedure I use to create the folder tree instead of
having it hardcoded.

hth,
Dave

'--------- CODE
Public objFSO

Sub Main()
Set objFSO = CreateObject("Scripting.FileSystemObject")
Call
GeneratePath("C:\test1\test2\test3\test4\test5\test6\test7\test8\test9")
End Sub

' ---------------------------------------------------------------------
'* @info Generate a folder tree from the path
'*
'* @param (String) Path
'* @return (Boolean) Folder Exists: Recursion continues (Y/N)
' ---------------------------------------------------------------------
Function GeneratePath(pFolderPath)
GeneratePath = False
If Not objFSO.FolderExists(pFolderPath) Then
If GeneratePath(objFSO.GetParentFolderName(pFolderPath)) Then
GeneratePath = True
Call objFSO.CreateFolder(pFolderPath)
End If
Else
GeneratePath = True
End If
End Function

Call Main
'------- END CODE
 
Joined
Nov 15, 2007
Messages
2
Reaction score
0
'This is the easiest way to do this recursive function to create the path structure
Sub CreateFolder( strPath )
Dim objFSO ' Could be made global..
Set objFSO = CreateObject("Scripting.FileSystemObject") 'Could be set global...
If Not objFSO.FolderExists( objFSO.GetParentFolderName(strPath) ) then Call CreateFolder( objFSO.GetParentFolderName(strPath) )
objFSO.CreateFolder( strPath )
End Sub
 
Joined
Jan 23, 2008
Messages
1
Reaction score
0
Creating the folder with date and month inside a directory

Hi Liqud

I am trying to create a folder with name as "<date> space <Month name>, for ex: 23 Jan'08

for doing this i am trying to create a vbs script and i am failing again and again

i have tried getting the script from the net, and i am trying to modify that and create a new one for my use to do my task

and i am new to this vbs scripts, having some idea ..how to do and how to execute...but not having enough exp. in writing that

please help me regarding this..how to create that ?

Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
set T = %time%
Const TargetDir = "D:testing'T'"
objFSO.CreateFolder(TargetDir)

i am testing with this one

thanks
raj
 
Joined
Nov 15, 2007
Messages
2
Reaction score
0
There was a potential bug in the original recursive create folder post... it is corrected below.

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")

'Recursive folder create, will create directories and Sub
Sub CreateFolder( strPath )
On Error Resume Next
If strPath <> "" Then 'Fixes endless recursion in some instances when at lowest directory
If Not objFSO.FolderExists( objFSO.GetParentFolderName(strPath) ) then Call CreateFolder( objFSO.GetParentFolderName(strPath) )
objFSO.CreateFolder( strPath )
End If
End Sub
 
Last edited:
Joined
Jun 17, 2008
Messages
2
Reaction score
0
Script looks good guys. I've been trying to put together a script to create folders, and set folder permissions using vbscript that has been added as a custom action to run in an msi. Was tearing my hair out as the .vbs script would create the folder when run on its own, but when run from within the msi it didn't do the create. Figured out in the end that the createObject for the filesystemobject was using wscript, and wscript just doesn't seem to work when run from within an msi. All good now and gonna look at incorporating your recursive folder creates.
 
Joined
Mar 13, 2009
Messages
1
Reaction score
0
a much easier way is shown in this function I just made, feel free to use it however you wish

if NOT makeFolder("c:\Program Files\xpto\123\client\") then wscript.echo "problem making folder"

'===============
'makeFolder it creates a folder and it's subfolders
'===============
function makeFolder(byVal pFolder)
makeFolder=FALSE
dim oShell
set oShell = WScript.CreateObject ("WSCript.shell")
oShell.run "cmd.exe /c md """ & pFolder & """"
makeFolder=TRUE
end function
 
Joined
Aug 19, 2009
Messages
1
Reaction score
0
Create folder using machine name

Hi All,

Can you help me in creating the VBS to create the folder with machine name at the specified location

Machine name=xxxxxx

the folder should be created at c:\demo\machine name

Thanks in advance
 
Joined
Dec 9, 2009
Messages
1
Reaction score
0
Making subfolder

Hi All,

I try to create a subfolder contain following information:

C:\Test\userIDDateTime

Can someone help me below is my code.
Dim oFSO
Dim Retry
Dim PFolder
Dim Pname
Dim FMain
Dim oMain

'Getting Project folder name exist on C drive.
Checkproject (filename)
Function Checkproject (filename)
Dim strRoot
strRoot = "C:\Test"
Set oFSO = CreateObject ("Scripting.FileSystemObject")
'Pname = Inputbox ("Please Enter Your Project Name")
If oFSO.FolderExists (strRoot) Then
Msgbox (strRoot)
Get_User_ID
Get_Date
Get_Time
MakeDir (strPath)
Else
Msgbox ("Please contact your QA or BA to create a project folder name")
End If
End Function
'Get User Initial
Function Get_User_ID
Dim Get_ID
Get_ID = Inputbox ("Please Enter your name initial")
If Get_ID = "" Then
Msgbox ("Initial can not leave blank, please re-enter your initial again")
Get_User_ID
End If
End Function
'Getting Date And Time.
Function Get_Date
Dim MyDate
Dim Short_Date
Dim strDate
MyDate = Date
Short_Date = (Right("00" & Month(Date()),2) & Right("00"&Day(Date()),2) & Right("00"&Year(Date()),2))
'Msgbox ("Your New date is:" & strDate)
End Function
Function Get_Time
Dim MyTime
Dim Short_Time
Dim strTime
MyTime = Time
Short_Time = (Right("00" & Hour(Time()),2) & Right("00"&Minute(Time()),2))
'strTime = Cstr (Short_Time)
'Msgbox ("Your New Time is:" & strTime)
End Function
Function MakeDir (strPath)
Dim strParentPath, objFSO
strPath = Cstr (strRoot & "/" & Get_ID & Short_Date & Short_Time)
Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
strParentPath = objFSO.GetParentFolderName(strPath)
If Not objFSO.FolderExists(strParentPath) Then MakeDir strParentPath
If Not objFSO.FolderExists(strPath) Then objFSO.CreateFolder(strPath)
On Error Goto 0
MakeDir = objFSO.FolderExists(strPath)
End Function
 
Joined
Mar 16, 2010
Messages
2
Reaction score
0
Could anyone please help me and see what is wrong with this script.

The whole purpose is to copy from folder A to Folder B . If Folder B doesn't exist craete one and copy all files from A to B.



function copyAllFiles(C:\lab, C:\toFolder)
'this function copies all files in the fromFolder to the toFolder
if the toFolder does not exist it is created
const OverWriteFiles = True
dim objFOS
set objFOS = CreateObject("Scripting.FileSystemObject")
call objFOS.createFolder(C:\toFolder)

call objFSO.CopyFile(C:\lab & "\*.*" ,C:\toFolder ,OverWriteFiles)
end function




I get an error ')' with this.
 
Joined
Mar 16, 2010
Messages
2
Reaction score
0
How about this?



function copyAllFiles(fromFolder, toFolder)
'this function copies all files in the fromFolder to the toFolder
if the toFolder does not exist it is created
const OverWriteFiles = True

dim objFOS

set objFOS = CreateObject("Scripting.FileSystemObject")

call objFOS.createFolder(C:\toFolder)


objFSO.CopyFile(fromFOlder & "\*.*" ,toFolder ,OverWriteFiles)

end function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top