Windows Vista Forums

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

  1. #1


    sa Guest

    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

  2. #2


    Pegasus \(MVP\) Guest

    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

    > 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

  3. #3


    sa Guest

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

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

    > "sa" <agarwasa2...@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!- Hide quoted text -
    >
    > - Show quoted text -
    Thank you very much for your help this morning.

    SA

      My System SpecsSystem Spec

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy File to users home drives if a certain file exists Dolfan849 VB Script 5 23 Sep 2009
if a file exists NeilOz PowerShell 6 03 Jan 2008
Creating folder based on filename of a file Orrin PowerShell 16 17 Dec 2007
Save As - file already exists Bob Lund Vista General 2 10 Apr 2007
Creating file in system folder Yuri Gurskiy Vista security 2 01 Aug 2006