Jump to content

If a file doesn't exist


Recommended Posts

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.

Link to comment
Share on other sites

  • Moderators

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Moderators

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.

Link to comment
Share on other sites

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]
Link to comment
Share on other sites

  • Moderators

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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)
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

  • Moderators

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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...