Jump to content

Trouble with FileExists


 Share

Recommended Posts

Hi! First time poster long time lurker. Not sure if this section is appropriate, please forgive me if it's not.

I'm building an automation script for work and i'm having a bit of trouble with the FileExists function. Somehow it fails to identify existence, despite it definitely existing.

Code is included below. Basically these functions were activated through two separate hotkeys and they generate a PDF and ODS file. I want to have them link to each other so I only need to press one hotkey but to do that I need it to validate that the file has been generated. I am saving these files to a dropbox folder, but even if I save it locally it still doesn't verify existence. It can often take a couple seconds for the file to show up so I use a while loop which waits for a certain number of iterations before trying again, as sometimes openoffice fails to print PDFs for some inexplicable reason. 

I don't think the recursion is causing the problem, not sure if it's a syntax or logic error. 

Thanks very much!

 

Func print()
   _BlockInputEx(1, "{ESC}")
   WinActivate($proposalwindow)
   Sleep(100)
   Send("{ENTER}")
   Sleep(300)
   Send("^p")
   Sleep(500)
   Send("{ENTER}")
   Sleep(500)
   Send($copy, 1)
   Sleep(200)
   Send("!s")
   Sleep(300)

   While $exist = 0
   Sleep(200)

   $exist = FileExists("C:\file path changed to protect the innocent\" & $copy & ".pdf")

   If $exist = 0 Then
      $sleep = $sleep + 1

      If $sleep = 30 Then
      print()
      EndIf
   EndIf
   WEnd

   $sleep = 0
   $exist = 0
   _BlockInputEx(0)

save()

EndFunc ;==> Uses hotkey to print proposal

Func save()
   _BlockInputEx(1, "{ESC}")
   WinActivate($proposalwindow)
   Sleep(100)
   Send("^+s")
   Sleep(1750)
   Send($copy)
   Sleep(500)
   Send("!s")
   Sleep(4000)

   While $exist = 0
   Sleep(200)

   $exist = FileExists("C:\file path changed to protect the innocent\" & $copy & ".ods")

   If $exist = 0 Then
      $sleep = $sleep + 1

      If $sleep = 30 Then
      save()
      EndIf
   EndIf
   WEnd

   MouseClick("left", 2200, 400, 1, 0)
   Sleep(200)
   Send("{DEL}")
   Sleep(100)

   $sleep = 0
   $exist = 0
   _BlockInputEx(0)

nextprop()

EndFunc ;==> Uses hotkey to save proposal

 

Link to comment
Share on other sites

hello @Ganreizu, and welcome to the forum.

first thing comes to mind: are you sure $copy holds the correct value? i.e. the file name only, no path or extension?

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

Just now, orbs said:

hello @Ganreizu, and welcome to the forum.

first thing comes to mind: are you sure $copy holds the correct value? i.e. the file name only, no path or extension?

Thanks :)

Yes i'm sure. $copy is a merged address which is validated in a separate function. It is used in the document itself, so file type within the variable in this instance would be unacceptable. I've been using these functions for some time and can confirm complete consistency in it's I/O. I wasn't sure if FileExists was catching the program extensions so I added the ".pdf"/".ods" into the function to make sure. It fails to validate existence regardless of if I include that. :/

Also I should mention that $exist and anything else is all declared and whatnot.  

Link to comment
Share on other sites

logic issue: get rid of recursion. confusing, unnecessary, and makes code hard to follow.

logic question: in func print, you do nothing if $exist<>0. please explain.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

3 minutes ago, orbs said:

logic issue: get rid of recursion. confusing, unnecessary, and makes code hard to follow.

logic question: in func print, you do nothing if $exist<>0. please explain.

Recursion: Hmm okay I see your point. I only needed it to perform the initial print/saving steps again if FileExists failed after an arbitrary period of time. I suppose just pasting those steps into the loop rather than run recursion achieves the same goal.

Logic: The intention here is as explained in the first post. Openoffice calc is printing a PDF using microsoft print to pdf, as opposed to export as PDF, because these documents will be printed physically in the future. Occasionally, openoffice fails to print the PDF, meaning the file will never come into existence, so for a while loop that continues to check for if the file exists, it needs to have a counter that tracks how long it has been checking else we get an endless loop. Thus, if exist = 0, increment a counter where 1 = 200ms and re-perform the print actions when you've been checking FileExists for a total of 6000 ms. If the file does exist sometime within those 6 seconds, then break from the while and continue on. Problem is it never breaks from the while even if the file does exist. Hope that makes sense.

Here is the updated code removing unnecessary recursion:

Func print()
   _BlockInputEx(1, "{ESC}")
   WinActivate($proposalwindow)
   Sleep(100)
   Send("{ENTER}")
   Sleep(300)
   Send("^p")
   Sleep(500)
   Send("{ENTER}")
   Sleep(500)
   Send($copy, 1)
   Sleep(200)
   Send("!s")
   Sleep(300)

   While $exist = 0
   Sleep(200)

   $exist = FileExists("C:\file path changed to protect the innocent\" & $copy & ".pdf")

   If $exist = 0 Then
      $sleep = $sleep + 1

      If $sleep = 30 Then
       WinActivate($proposalwindow)
       Sleep(100)
       Send("{ENTER}")
       Sleep(300)
       Send("^p")
       Sleep(500)
       Send("{ENTER}")
       Sleep(500)
       Send($copy, 1)
       Sleep(200)
       Send("!s")
       Sleep(300)
      EndIf
   EndIf
   WEnd

   $sleep = 0
   $exist = 0
   _BlockInputEx(0)

save()

EndFunc ;==> Uses hotkey to print proposal

Func save()
   _BlockInputEx(1, "{ESC}")
   WinActivate($proposalwindow)
   Sleep(100)
   Send("^+s")
   Sleep(1750)
   Send($copy)
   Sleep(500)
   Send("!s")
   Sleep(4000)

   While $exist = 0
   Sleep(200)

   $exist = FileExists("C:\file path changed to protect the innocent\" & $copy & ".ods")

   If $exist = 0 Then
      $sleep = $sleep + 1

      If $sleep = 30 Then
      WinActivate($proposalwindow)
      Sleep(100)
      Send("^+s")
      Sleep(1750)
      Send($copy)
      Sleep(500)
      Send("!s")
      Sleep(4000)
      EndIf
   EndIf
   WEnd

   WinActivate($proposalwindow)
   Sleep(100)
   MouseClick("left", 2200, 400, 1, 0)
   Sleep(200)
   Send("{DEL}")
   Sleep(100)

   $sleep = 0
   $exist = 0
   _BlockInputEx(0)

nextprop()

EndFunc ;==> Uses hotkey to save proposal

 

Link to comment
Share on other sites

5 minutes ago, Earthshine said:

if the string that contains the path has spaces, make sure you are double quoting the entire path string properly.

It does. Both the path to the dropbox folder and the file name has spaces which I would prefer not to change. Can I get an example of proper double quoting? The FileExists help file doesn't show proper quoting, it only uses a single variable. The $copy variable is an address. So for example "123 Chrome Lane Detroit". Do I need to do string manipulation then?

Link to comment
Share on other sites

1 hour ago, AdamUL said:

Here are some examples of proper quotes for each function call.  

$exist = FileExists('"C:\file path changed to protect the innocent\' & $copy & '.pdf"')

$exist = FileExists('"C:\file path changed to protect the innocent\' & $copy & '.ods"')

 

Adam

Thank you!

 

I updated the code to include this and it still doesn't fall through. HOWEVER,  I DID get it to work when I used StringStripWS to remove all whitespace in the file name and using a local file path with no whitespace. I guess FileExists just hates whitespace a lot? 

It's not a big deal if the file name and file path doesn't have whitespace, but it's kind of annoying to work around it like that. Any ideas on how to have it accept whitespace?

Link to comment
Share on other sites

  • Developers

Can we all take a step back here! FileExists() works fine with spaces in the path and or filename:

$file = "C:\Program Files (x86)\AutoIt3\Extras\Editors\TextPad\Manual Install and Notes.htm"
ConsoleWrite('FileExists(' & $file & ') = ' & FileExists($file) & @CRLF) ;### Debug Console

So it is now your turn to provide a scriptlet that shows it doesn't. :)

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

23 minutes ago, Jos said:

Can we all take a step back here! FileExists() works fine with spaces in the path and or filename:

$file = "C:\Program Files (x86)\AutoIt3\Extras\Editors\TextPad\Manual Install and Notes.htm"
ConsoleWrite('FileExists(' & $file & ') = ' & FileExists($file) & @CRLF) ;### Debug Console

So it is now your turn to provide a scriptlet that shows it doesn't. :)

Jos

Truth. Thanks. This is what happens when you're an amateur with very little formal programming training.

So then I have figured out the problem methinks. I had narrowed down the behavior having something to do with whitespace. Clearly this wasn't something to do with FileExists, so I think the issue must have been with $copy. Because it's taking it's contents from a spreadsheet (address and town) which tends to have excess whitespaces, I used StringStripWS to remove leading+trailing whitespace before putting it through since when $copy is pasted into a save window those spaces would be removed during file creation, but included in FileExists. So by removing them it falls through! 

 

This is the code that performs as expected:

Func print()
   _BlockInputEx(1, "{ESC}")
   $copy = StringStripWS($copy, $STR_STRIPLEADING + $STR_STRIPTRAILING)
   WinActivate($proposalwindow)
   Sleep(100)
   Send("{ENTER}")
   Sleep(300)
   Send("^p")
   Sleep(500)
   Send("{ENTER}")
   Sleep(500)
   Send($copy, 1)
   Sleep(500)
   Send("!s")
   Sleep(300)

   While $exist = 0
   Sleep(200)

   $exist = FileExists("C:\file path changed to protect the innocent\" & $copy & ".pdf")

   If $exist = 0 Then
      $sleep = $sleep + 1

      If $sleep = 30 Then
       $sleep = 0
       WinActivate($proposalwindow)
       Sleep(100)
       Send("{ENTER}")
       Sleep(300)
       Send("^p")
       Sleep(500)
       Send("{ENTER}")
       Sleep(500)
       Send($copy, 1)
       Sleep(200)
       Send("!s")
       Sleep(300)
      EndIf
   EndIf
   WEnd

   $sleep = 0
   $exist = 0
   _BlockInputEx(0)

save()

EndFunc ;==> Uses hotkey to print proposal

Func save()
   _BlockInputEx(1, "{ESC}")
   $copy = StringStripWS($copy, $STR_STRIPLEADING + $STR_STRIPTRAILING)
   WinActivate($proposalwindow)
   Sleep(100)
   Send("^+s")
   Sleep(1750)
   Send($copy, 1)
   Sleep(500)
   Send("!s")
   Sleep(4000)

   While $exist = 0
   Sleep(200)

   $exist = FileExists("C:\file path changed to protect the innocent\" & $copy & ".ods")


   If $exist = 0 Then
      $sleep = $sleep + 1

      If $sleep = 30 Then
      $sleep = 0
      WinActivate($proposalwindow)
      Sleep(100)
      Send("^+s")
      Sleep(1750)
      Send($copy)
      Sleep(500)
      Send("!s")
      Sleep(4000)
      EndIf
   EndIf
   WEnd

   WinActivate($proposalwindow)
   Sleep(100)
   MouseClick("left", 2200, 400, 1, 0)
   Sleep(200)
   Send("{DEL}")
   Sleep(100)

   $sleep = 0
   $exist = 0
   _BlockInputEx(0)

nextprop()

EndFunc ;==> Uses hotkey to save proposal

 

Thank you so much everyone! :D

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...