Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

if a file exists

Closed Thread
 
Thread Tools Display Modes
Old 12-30-2007   #1 (permalink)
NeilOz
Guest


 

if a file exists

Guys, I've been playing around with a way to get a way to end a powershell
script if a file doesn't exist, but so far have failed miserably

Any ideas on the best way to do this?


Old 12-30-2007   #2 (permalink)
Marco Shaw [MVP]
Guest


 

Re: if a file exists

NeilOz wrote:
Quote:

> Guys, I've been playing around with a way to get a way to end a powershell
> script if a file doesn't exist, but so far have failed miserably
>
> Any ideas on the best way to do this?
>
>
Script:
"start"
if(!(test-path foo.file)){"file not found";break}
"end"

Output:
PSH> ./testpath.ps1
start
file not found
PSH>

Note: "end" does not show because of the break statement.

Marco



--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
Old 01-01-2008   #3 (permalink)
Jon
Guest


 

Re: if a file exists


"NeilOz" <NeilOz@xxxxxx> wrote in message
news:F2452105-8530-4B5F-B098-7E3837B039CF@xxxxxx
Quote:

> Guys, I've been playing around with a way to get a way to end a powershell
> script if a file doesn't exist, but so far have failed miserably
>
> Any ideas on the best way to do this?
>
>

Another option, for the sake of completion .....


[reflection.assembly]::LoadWithPartialName("Microsoft.VisualBasic")

if(!([Microsoft.VisualBasic.FileIO.FileSystem]::FileExists($profile))) {
Write-Host "No such file"; Return
}

--
Jon


Old 01-01-2008   #4 (permalink)
Shay Levi
Guest


 

Re: if a file exists


Which can be achieved without the PartialName loading

[System.IO.File]::Exists(<path>)


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> "NeilOz" <NeilOz@xxxxxx> wrote in message
> news:F2452105-8530-4B5F-B098-7E3837B039CF@xxxxxx
>
Quote:

>> Guys, I've been playing around with a way to get a way to end a
>> powershell script if a file doesn't exist, but so far have failed
>> miserably
>>
>> Any ideas on the best way to do this?
>>
> Another option, for the sake of completion .....
>
> [reflection.assembly]::LoadWithPartialName("Microsoft.VisualBasic")
>
> if(!([Microsoft.VisualBasic.FileIO.FileSystem]::FileExists($profile)))
> {
> Write-Host "No such file"; Return
> }

Old 01-01-2008   #5 (permalink)
Jon
Guest


 

Re: if a file exists


"Shay Levi" <no@xxxxxx> wrote in message
news:8766a944164f18ca1a6efc678646@xxxxxx
Quote:

>
> Which can be achieved without the PartialName loading
>
> [System.IO.File]::Exists(<path>)
>
>

True. Good call.

"Options, options ...." ;-)

--
Jon


Old 01-03-2008   #6 (permalink)
IT Staff
Guest


 

Re: if a file exists

Is there a way to perform 3 filename checks at one go ? Eg

$file_exists = [System.IO.File]::Exists(D:\test1.txt)

If i have test1.txt, test2.txt, abc.txt, are there any .net class do put
everything in similar fashion :

$file_exists = [System.IO.File]::Exists(D:\test1.txt, d:\test2.txt, abc.txt)

if *any* $file_exists, then write-host ....








"Shay Levi" <no@xxxxxx> wrote in message
news:8766a944164f18ca1a6efc678646@xxxxxx
Quote:

>
> Which can be achieved without the PartialName loading
>
> [System.IO.File]::Exists(<path>)
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>
>
>
Quote:

>> "NeilOz" <NeilOz@xxxxxx> wrote in message
>> news:F2452105-8530-4B5F-B098-7E3837B039CF@xxxxxx
>>
Quote:

>>> Guys, I've been playing around with a way to get a way to end a
>>> powershell script if a file doesn't exist, but so far have failed
>>> miserably
>>>
>>> Any ideas on the best way to do this?
>>>
>> Another option, for the sake of completion .....
>>
>> [reflection.assembly]::LoadWithPartialName("Microsoft.VisualBasic")
>>
>> if(!([Microsoft.VisualBasic.FileIO.FileSystem]::FileExists($profile)))
>> {
>> Write-Host "No such file"; Return
>> }
>
>

Old 01-03-2008   #7 (permalink)
Shay Levi
Guest


 

Re: if a file exists

Not with [System.IO.File]::Exists

You can do it with Test-Path:
"D:\test1.txt","d:\test2.txt","abc.txt" | test-path -pathtype leaf



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Is there a way to perform 3 filename checks at one go ? Eg
>
> $file_exists = [System.IO.File]::Exists(D:\test1.txt)
>
> If i have test1.txt, test2.txt, abc.txt, are there any .net class do
> put everything in similar fashion :
>
> $file_exists = [System.IO.File]::Exists(D:\test1.txt, d:\test2.txt,
> abc.txt)
>
> if *any* $file_exists, then write-host ....
>
> "Shay Levi" <no@xxxxxx> wrote in message
> news:8766a944164f18ca1a6efc678646@xxxxxx
>
Quote:

>> Which can be achieved without the PartialName loading
>>
>> [System.IO.File]::Exists(<path>)
>>
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
Quote:

>>> "NeilOz" <NeilOz@xxxxxx> wrote in message
>>> news:F2452105-8530-4B5F-B098-7E3837B039CF@xxxxxx
>>>
>>>> Guys, I've been playing around with a way to get a way to end a
>>>> powershell script if a file doesn't exist, but so far have failed
>>>> miserably
>>>>
>>>> Any ideas on the best way to do this?
>>>>
>>> Another option, for the sake of completion .....
>>>
>>> [reflection.assembly]::LoadWithPartialName("Microsoft.VisualBasic")
>>>
>>> if(!([Microsoft.VisualBasic.FileIO.FileSystem]::FileExists($profile)
>>> ))
>>> {
>>> Write-Host "No such file"; Return
>>> }

Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
ReadyBoost - Cache Exists RugRat Vista General 10 04-14-2008 10:32 PM
ultimate backup error: file already exists Chris Cowles Vista performance & maintenance 3 09-11-2007 11:51 PM
If file exists loop ryanlsanders@gmail.com PowerShell 5 08-16-2007 10:05 AM
Get "file already exists" on trying to save a new file (after rena D70neRd Vista General 1 07-07-2007 05:19 PM
Save As - file already exists Bob Lund Vista General 2 04-10-2007 11:32 PM








Vistax64.com 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 2005-2008

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 47 48 49 50