JustDoIt Posted August 28, 2007 Posted August 28, 2007 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.
Moderators SmOke_N Posted August 28, 2007 Moderators Posted August 28, 2007 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.
PsaltyDS Posted August 28, 2007 Posted August 28, 2007 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
JustDoIt Posted August 28, 2007 Author Posted August 28, 2007 (edited) 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 August 28, 2007 by JustDoIt
Achilles Posted August 28, 2007 Posted August 28, 2007 (edited) 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 August 28, 2007 by Piano_Man My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Paulie Posted August 28, 2007 Posted August 28, 2007 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" TIAAre you looking for something like the "browse" button, to allow easy file selection? If so - FileOpenDialog Or perhaps an inputbox with a prompt? - Inputbox
Moderators SmOke_N Posted August 28, 2007 Moderators Posted August 28, 2007 (edited) open "aFile" for input as "SomeFileName" TIANot 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 August 28, 2007 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.
Achilles Posted August 29, 2007 Posted August 29, 2007 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 hFileQuite 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]
Moderators SmOke_N Posted August 29, 2007 Moderators Posted August 29, 2007 Quite the variation of answers here... I thought he meant open a program so the user can edit it or view itIt'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.
Paulie Posted August 29, 2007 Posted August 29, 2007 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 :">
Achilles Posted August 29, 2007 Posted August 29, 2007 Lol!I didn't understand what he wanted either.... I took a guess.I only speak autoit :">and English apparently... My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
KJohn Posted August 29, 2007 Posted August 29, 2007 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?
Richard Robertson Posted August 29, 2007 Posted August 29, 2007 He asked using "open for input". He wants to read a file. $filehandle = FileOpen("blah.txt") $text = FileRead($filehandle) FileClose($filehandle)
JustDoIt Posted August 29, 2007 Author Posted August 29, 2007 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)
Bert Posted August 29, 2007 Posted August 29, 2007 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. The Vollatran project My blog: http://www.vollysinterestingshit.com/
Achilles Posted August 30, 2007 Posted August 30, 2007 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]
Moderators SmOke_N Posted August 30, 2007 Moderators Posted August 30, 2007 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now