Jump to content

FileRead vs FileExist issue - (Moved)


Megaben3
 Share

Go to solution Solved by Danp2,

Recommended Posts

Hello,

Have a strange problem!

Readline from config.txt file and put string to variable 

Use this string with FileExist ( = no exist )

If I set the same variable with the same string, FileExist (= exist)

The difference, is the string source !

Sample code:

#include <Array.au3>
#include <String.au3>
#include <AutoItConstants.au3>
#include <WinAPIFiles.au3>

Global $sConfigPath = (@ScriptDir & "\Config.txt")

Func GetConfigFile()

  Global $sConfigOpen = FileOpen ($sConfigPath, $FO_READ)
    If $sConfigOpen = -1 Then; if open error
        MsgBox($MB_SYSTEMMODAL, "", "error reading Config.txt.")
    Exit
    EndIf
 For $nLine = 1 to 3
   Global $sReadConfig = FileReadLine ($sConfigOpen,$nLine)
       If $nLine = 1 then Global $sDownloadFolderPath = $sReadConfig
       If $nLine = 2 then Global $sLocalFolderPath = $sReadConfig
       If $nLine = 3 then Global $sTabloFolderPath = $sReadConfig; the value is good "D:\Media\Tablo\"
       ;Global $sTabloFolderPath = "Global $sTabloFolderPath = "D:\Media\Tablo\"; if I set this, Tablo exist = YES
       CheckIfTabloExist() ; result is NO, this path exist, no understand because ?

  Next

EndFunc

Func CheckIfTabloExist()

    IF FileExists($sTabloFolderPath) Then
        Global $sTabloExist = "YES"
    Else
        Global $sTabloExist = "NO"
        Beep (800, 1000)
        MsgBox($MB_SYSTEMMODAL, "", "TabloExist = : " & $sTabloExist)
        Exit
    EndIf
EndFunc

 

Thank you for your help!

Megaben3

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

Because you call CheckIfTabloExist 3 times. Only on the third call sTabloFolderPath exists and has  valid content.
I would modify your script as follows:

#include <Array.au3>
#include <String.au3>
#include <AutoItConstants.au3>
#include <WinAPIFiles.au3>

Global $hConfigOpen, $sReadConfig, $sDownloadFolderPath, $sLocalFolderPath, $sTabloFolderPath
Global $sConfigPath = (@ScriptDir & "\Config.txt")

Func GetConfigFile()
    $hConfigOpen = FileOpen($sConfigPath, $FO_READ)
    If $hConfigOpen = -1 Then Exit MsgBox($MB_SYSTEMMODAL, "", "error reading Config.txt.") ; If open error
    For $nLine = 1 To 3
        $sReadConfig = FileReadLine($hConfigOpen, $nLine)
        If $nLine = 1 Then $sDownloadFolderPath = $sReadConfig
        If $nLine = 2 Then $sLocalFolderPath = $sReadConfig
        If $nLine = 3 Then
            $sTabloFolderPath = $sReadConfig
            If Not FileExists($sTabloFolderPath) Then
                Beep(800, 1000)
                MsgBox($MB_SYSTEMMODAL, "", $sTabloFolderPath & " does not exist!")
                Exit
            EndiF
        Endif
    Next
EndFunc   ;==>GetConfigFile

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I would suggest changing several things --

  • Move your Global declarations outside of the functions
  • Move the call to CheckIfTabloExist outside of your For...Next. Otherwise, you call it multiple times when the value hasn't been set.

You may want to look at using FileReadToArray, which would allow you to do something like this --

Local $aFiles = FileReadToArray($sConfigPath)
$sDownloadFolderPath = $aFiles[0]
$sLocalFolderPath = $aFiles[1]
$sTabloFolderPath = $aFiles[2]

 

Link to comment
Share on other sites

  • Developers

Moved to the appropriate forum, as the Developer General Discussion forum very clearly states:

Quote

General development and scripting discussions.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team

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

Hello Danp2,

Your array method is the best, I love it !

Have the same problem.

Complete revised code :

#include <Array.au3>
#include <String.au3>
#include <AutoItConstants.au3>
#include <WinAPIFiles.au3>

Global $hConfigOpen, $sReadConfig, $sDownloadFolderPath, $sLocalFolderPath, $sTabloFolderPath, $sTabloExist
Global $sConfigPath = (@ScriptDir & "\Config.txt")
Global $aFiles = FileReadToArray($sConfigPath)

Call ("GetConfigFile")

Func GetConfigFile()

    $hConfigOpen = FileOpen($sConfigPath, $FO_READ)
    If $hConfigOpen = -1 Then Exit MsgBox($MB_SYSTEMMODAL, "", "error reading Config.txt.") ; If open error
        $sDownloadFolderPath = $aFiles[0]
        $sLocalFolderPath = $aFiles[1]
        $sTabloFolderPath = $aFiles[2]
            If Not FileExists($sTabloFolderPath) Then
                Beep(800, 1000)
                MsgBox($MB_SYSTEMMODAL, "", $sTabloFolderPath & " does not exist!"); always not exist
            EndiF
    MsgBox($MB_SYSTEMMODAL, "", $sTabloFolderPath); display good path
EndFunc   ;==>GetConfigFile

config - array.au3 Config.txt

Link to comment
Share on other sites

Hello,

Have find how display code in forum

#include <Array.au3>
#include <String.au3>
#include <AutoItConstants.au3>
#include <WinAPIFiles.au3>

Global $hConfigOpen, $sReadConfig, $sDownloadFolderPath, $sLocalFolderPath, $sTabloFolderPath, $sTabloExist
Global $sConfigPath = (@ScriptDir & "\Config.txt")
Global $aFiles = FileReadToArray($sConfigPath)

Call ("GetConfigFile")

Func GetConfigFile()

    $hConfigOpen = FileOpen($sConfigPath, $FO_READ)
    If $hConfigOpen = -1 Then Exit MsgBox($MB_SYSTEMMODAL, "", "error reading Config.txt.");If open error
        $sDownloadFolderPath = $aFiles[0]
        $sLocalFolderPath = $aFiles[1]
        $sTabloFolderPath = $aFiles[2]
            If Not FileExists($sTabloFolderPath) Then
                Beep(800, 1000)
                MsgBox($MB_SYSTEMMODAL, "", $sTabloFolderPath & " does not exist!");always not exist
            EndiF
    MsgBox($MB_SYSTEMMODAL, "", $sTabloFolderPath);display good path
EndFunc   ;==>GetConfigFile

 

Link to comment
Share on other sites

  • Solution

It's possible that the quotation marks are causing the issue. Have you tried removing them from the 3rd line?

P.S. You don't need these lines in your function as the contents have already been read into an array --

$hConfigOpen = FileOpen($sConfigPath, $FO_READ)
    If $hConfigOpen = -1 Then Exit MsgBox($MB_SYSTEMMODAL, "", "error reading Config.txt.");If open error

 

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