![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 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 Specs![]() |
| | #3 (permalink) |
| | 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 - SA |
My System Specs![]() |
![]() |
| 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 | |||