Jump to content
Sign in to follow this  
JustDoIt

If a file doesn't exist

Recommended Posts

JustDoIt

Sorry for this newbie question. I tried a search on this board but it came back nothing; maybe my keyword wasn't correct.

If a file, say "C:\temp\testing.txt" doesn't exist, can I write

if not FileExists("C:\temp\testing.txt") then

I don't want to do this

if FileExists("C:\temp\testing.txt") then ...
Else .... then
endif

Thanks.

Share this post


Link to post
Share on other sites
SmOke_N

Sorry for this newbie question. I tried a search on this board but it came back nothing; maybe my keyword wasn't correct.

If a file, say "C:\temp\testing.txt" doesn't exist, can I write

if not FileExists("C:\temp\testing.txt") then

I don't want to do this

if FileExists("C:\temp\testing.txt") then ...
Else .... then
endif

Thanks.

"NOT" is a bool that checks to see if the condition is ... well ... "Not" true. What you have should work (are you saying it's failing?).

If Not FileExists(SomeFile.ext) Then Do Something

Or

If FileExists(SomeFile.ext) = 0 Then Do Something

Both would do the same thing essentially.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
PsaltyDS

I don't want to do this

if FileExists("C:\temp\testing.txt") then ...
Else .... then
endif
Minor nit to pick, as SmOke_N already answered your question: Don't put 'Then' after an 'Else' but you do after 'ElseIf':

If FileExists($sFile1) Then
      MsgBox(64, "Found", "File " & $sFile1 & " exists.")
ElseIf FileExists($sFile2) Then
      MsgBox(64, "Found", "File " & $sFile2 & " exists.")
Else
      MsgBox(16, "Not Found", "Neither file " & $sFile1 & " nor file "  & $sFile2 & " exists!")
EndIf

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
JustDoIt

Thanks SmOke_N. Both "If Not FileExists(SomeFile.ext) Then Do Something" and "If FileExists(SomeFile.ext) = 0 Then Do Something" worked fine.

Thanks PsaltyDS. I'll try out your code later to see how it works.

Wondering if you both can help me on this (?) I am looking for an autoit command that equivalents to this statement

open "aFile" for input as "SomeFileName"

TIA

Edited by JustDoIt

Share this post


Link to post
Share on other sites
Achilles

Wondering if you both can help me on this (?) I am looking for an autoit command that equivalents to this statement

open "aFile" for input as "SomeFileName"

TIA

ShellExecute() runs stuff (it can be anything).

Run() runs .exe files and can be used to receive information from the program that is running.

EDIT: I suggest the helpfile and look for both, it will tell you more about how to use each

Edited by Piano_Man

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

Share this post


Link to post
Share on other sites
Paulie

Wondering if you both can help me on this (?) I am looking for an autoit command that equivalents to this statement

open "aFile" for input as "SomeFileName"

TIA

Are you looking for something like the "browse" button, to allow easy file selection? If so - FileOpenDialog

Or perhaps an inputbox with a prompt? - Inputbox

Share this post


Link to post
Share on other sites
SmOke_N

open "aFile" for input as "SomeFileName"

TIA

Not sure where the other two where going, but if you're looking to open a file for writing....

DIM $hFile ; AS FILE

$hFile = FileOpen(SomeFileName, 2);2 erases previous content (and or creates new file)... should be the same as (open "aFile" for input as "SomeFileName")
FileWrite($hFile, "Something to write to file")
FileClose($hFile);Close hFile
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
Achilles

Not sure where the other two where going, but if you're looking to open a file for writing....

DIM $hFile ; AS FILE

$hFile = FileOpen(SomeFileName, 2);2 erases previous content (and or creates new file)... should be the same as (open "aFile" for input as "SomeFileName")
FileWrite($hFile, "Something to write to file")
FileClose($hFile);Close hFile
Quite the variation of answers here... I thought he meant open a program so the user can edit it or view it

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

Share this post


Link to post
Share on other sites
SmOke_N

Quite the variation of answers here... I thought he meant open a program so the user can edit it or view it

It's a "basic language" syntax.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
Paulie

Not sure where the other two where going, but if you're looking to open a file for writing....

Lol!

I didn't understand what he wanted either.... I took a guess.

I only speak autoit :">

Share this post


Link to post
Share on other sites
KJohn

Thanks SmOke_N. Both "If Not FileExists(SomeFile.ext) Then Do Something" and "If FileExists(SomeFile.ext) = 0 Then Do Something" worked fine.

Thanks PsaltyDS. I'll try out your code later to see how it works.

Wondering if you both can help me on this (?) I am looking for an autoit command that equivalents to this statement

open "aFile" for input as "SomeFileName"

TIA

$filename = FileOpenDialog ("Select a file", "C:", "*.*", 3)
If Not @error Then ShellExecute ($filename)

Is this what you wanted?

Share this post


Link to post
Share on other sites
Richard Robertson

He asked using "open for input". He wants to read a file.

$filehandle = FileOpen("blah.txt")

$text = FileRead($filehandle)

FileClose($filehandle)

Share this post


Link to post
Share on other sites
JustDoIt

Thank you all for the overwhelming responses to my question.

The statement I mentioned earlier is actually BASIC language. What it does is to open a file to write, then save the file under different name in the buffer. I'll have other statements to copy the file's content into a string, then kill the old file. Can you comment on my code below ?

DIM $aString
$file = FileOpen("oldFile", 1)
$line = FileReadLine($file)
FileWriteLine($aString, $line)
FileClose($file)
FileDelete (oldFile)

Share this post


Link to post
Share on other sites
Achilles

I usually do this:

$file = FileExist("filename")

if $file = 0 then (whatever you want to do)

If this is a bad way to do it, then someone please correct me. I rather do it the correct way, but this method seems to work well.

What you have is good code convention (as far as I know), but I usually shorten it a bit to this:

If Not FileExist("filename") then 
;Do whatever 
EndIf

Or if you want to proceed with the filing existing just take out the Not.


My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

Share this post


Link to post
Share on other sites
SmOke_N

What you have is good code convention (as far as I know), but I usually shorten it a bit to this:

If Not FileExist("filename") then 
;Do whatever 
EndIf

Or if you want to proceed with the filing existing just take out the Not.

I used to do it this way, but I prefer Volly's method the last few months... and I believe it's even faster.

Instead of:

If NOT FileExists("name") THEN ;Do SomethingoÝ÷ جy«­¢+Ù%¥±á¥ÍÑÌ ÅÕ½Ðí¹µÅÕ½Ðì¤ôÀQ!8

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.