Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Re: Creating a file and a folder iff it does not exists!!

Reply
 
Old 02-02-2009   #1 (permalink)
sa


 
 

Re: Creating a file and a folder iff it does not exists!!

Hi:

I am new to vbScript. I have the code below for preview. But what I am
basically trying to do is create the folder if it does not exists and
after that I have to create the file Foo.txt in that folder. Is there
a smarter and a concise way of doing it. May be by using not or any
other suggestions would be welcome to.

Thanks,
SA


Here's the Code:
-----------------------------------------------------------------------
dim objFSOC,folderC

folderC= folderP & "PCB\Output\BOM"
outFile="Foo.txt"

set objFSOC = CreateObject("Scripting.FileSystemObject")
'Creating the folder if it does not exists
if objFSOC.FolderExists (folderC) then
'do nothing
else
set objFolderC = objFSOC.CreateFolder(folderC)
end if

'creating the BOM.txt file
' Another Q: CAn i use folderC & outFile as I have used here
if objFSOC.FileExists(folderC & "\" & outFile) then
'do nothing
else
set objFileC = objFSOC.CreateTextFile(folderC & outFile
end if

objFolderC.close
objFileC.close

My System SpecsSystem Spec
Old 02-02-2009   #2 (permalink)
Pegasus \(MVP\)


 
 

Re: Creating a file and a folder if it does not exists!


"sa" <agarwasa2008@xxxxxx> wrote in message
news:ed3ac0c9-d0bc-4bb9-a34c-c0260acf3fc1@xxxxxx
Quote:

> Hi:
>
> I am new to vbScript. I have the code below for preview. But what I am
> basically trying to do is create the folder if it does not exists and
> after that I have to create the file Foo.txt in that folder. Is there
> a smarter and a concise way of doing it. May be by using not or any
> other suggestions would be welcome to.
>
> Thanks,
> SA
>
>
> Here's the Code:
> -----------------------------------------------------------------------
> dim objFSOC,folderC
>
> folderC= folderP & "PCB\Output\BOM"
> outFile="Foo.txt"
>
> set objFSOC = CreateObject("Scripting.FileSystemObject")
> 'Creating the folder if it does not exists
> if objFSOC.FolderExists (folderC) then
> 'do nothing
> else
> set objFolderC = objFSOC.CreateFolder(folderC)
> end if
>
> 'creating the BOM.txt file
> ' Another Q: CAn i use folderC & outFile as I have used here
> if objFSOC.FileExists(folderC & "\" & outFile) then
> 'do nothing
> else
> set objFileC = objFSOC.CreateTextFile(folderC & outFile
> end if
>
> objFolderC.close
> objFileC.close
You've got the basic idea. It is possible to tighten your code a little,
e.g. like so:

Instead of writing
============
set objFSOC = CreateObject("Scripting.FileSystemObject")
'Creating the folder if it does not exists
if objFSOC.FolderExists (folderC) then
'do nothing
else
set objFolderC = objFSOC.CreateFolder(folderC)
end if

============
you could write
folderC= folderP & "PCB\Output\BOM\"
if not objFSOC.FolderExists (folderC) then objFSOC.CreateFolder(folderC)

and by the same token:
if not objFSOC.FileExists(folderC & outFile) then
objFSOC.CreateTextFile(folderC & outFile)

Note the trailing backslash in the definition of "folderC".

There is no point in creating your object "objFileC" under the "if"
statement. You must create it later on, regardless of the pre-existence of
the file!


My System SpecsSystem Spec
Old 02-02-2009   #3 (permalink)
sa


 
 

Re: Creating a file and a folder if it does not exists!

On Feb 2, 7:45*am, "Pegasus \(MVP\)" <I....@xxxxxx> wrote:
Quote:

> "sa" <agarwasa2...@xxxxxx> wrote in message
>
> news:ed3ac0c9-d0bc-4bb9-a34c-c0260acf3fc1@xxxxxx
>
>
>
>
>
Quote:

> > Hi:
>
Quote:

> > I am new to vbScript. I have the code below for preview. But what I am
> > basically trying to do is create the folder if it does not exists and
> > after that I have to create the file Foo.txt in that folder. Is there
> > a smarter and a concise way of doing it. May be by using not or any
> > other suggestions would be welcome to.
>
Quote:

> > Thanks,
> > SA
>
Quote:

> > Here's the Code:
> > -----------------------------------------------------------------------
> > dim objFSOC,folderC
>
Quote:

> > folderC= folderP & "PCB\Output\BOM"
> > outFile="Foo.txt"
>
Quote:

> > set objFSOC = CreateObject("Scripting.FileSystemObject")
> > 'Creating the folder if it does not exists
> > if objFSOC.FolderExists (folderC) then
> > 'do nothing
> > else
> > set objFolderC = objFSOC.CreateFolder(folderC)
> > end if
>
Quote:

> > 'creating the BOM.txt file
> > ' Another Q: *CAn i use folderC & outFile as I have used here
> > if objFSOC.FileExists(folderC & "\" & outFile) then
> > * * * * * * *'do nothing
> > else
> > *set objFileC = objFSOC.CreateTextFile(folderC & outFile
> > end if
>
Quote:

> > objFolderC.close
> > objFileC.close
>
> You've got the basic idea. It is possible to tighten your code a little,
> e.g. like so:
>
> Instead of writing
> ============
> set objFSOC = CreateObject("Scripting.FileSystemObject")
> 'Creating the folder if it does not exists
> if objFSOC.FolderExists (folderC) then
> 'do nothing
> else
> set objFolderC = objFSOC.CreateFolder(folderC)
> end if
>
> ============
> you could write
> folderC= folderP & "PCB\Output\BOM\"
> if not objFSOC.FolderExists (folderC) then objFSOC.CreateFolder(folderC)
>
> and by the same token:
> if not objFSOC.FileExists(folderC & outFile) then
> objFSOC.CreateTextFile(folderC & outFile)
>
> Note the trailing backslash in the definition of "folderC".
>
> There is no point in creating your object "objFileC" under the "if"
> statement. You must create it later on, regardless of the pre-existence of
> the file!- Hide quoted text -
>
> - Show quoted text -
Thank you very much for your help this morning.

SA
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Copy File to users home drives if a certain file exists VB Script
if a file exists PowerShell
Creating folder based on filename of a file PowerShell
Save As - file already exists Vista General
Creating file in system folder Vista security


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46