"sa" <agarwasa2008@xxxxxx> wrote in message
news:ed3ac0c9-d0bc-4bb9-a34c-c0260acf3fc1@xxxxxx
> 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!