Jump to content

Checking if File Exists


Recommended Posts

Hi guys,

Wondering if you can help, basically trying to get a log of all computers with or without SSD.

I've created this and stuck on trying to see if the file already exists.

 

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <file.au3>

; Check the SSD status for C:\
Local $Name= @ComputerName
Local $SSD= DriveGetType("C:\", $DT_SSDSTATUS)
Local $Network= "\\nashom04\hdsource\SSD"
Local $iFileExists = FileExists($Name & ".txt")

FileChangeDir($Network)
MsgBox(0,"TEST", $iFileExists)
If $iFileExists Then
    FileWrite($Name&'.txt', $Name&' '&$SSD)
    MsgBox(0,"Thank You", $Name & " has been updated.")
    Exit
Else
    FileWrite($Name&'.txt', $Name&' '&$SSD)
    MsgBox(0,"Thank You", $Name & " has been updated.")
EndIf

 

What it's meant to do check the hard drive if it's a SSD or not and write a text file into a network directory.

All is good, however if I want to check if the user has already run the program or not, I use file exists.

Running into trouble trying to get it working, it keeps writing even if it exists.

 

Thanks for your help.

Link to comment
Share on other sites

You write the file to the network share.  Your check for the existence of the file checks whether it exists in the script's directory because you haven't changed directories yet.  So of course the file never exists in the script's directory.  You would be much better off using absolute paths instead of relative paths.  Then things like that wouldn't happen when changing directories.

Edited by TheXman
Fixed a typo
Link to comment
Share on other sites

Since you have the FileWrite and Msgbox in both conditions, it writes the file.

You should be able to remove the first set and just have Exit in the condition check.

Alternatively, you could change the logic and reduce a couple lines.
 

If Not $iFileExists Then
    FileWrite($Name&'.txt', $Name&' '&$SSD)
    MsgBox(0,"Thank You", $Name & " has been updated.")
EndIf

 

Link to comment
Share on other sites

  • 2 years later...

I wish to check if details.txt exists so I can use an include and run my script

if it does not exist I wish to create it along with (lets say) 2 variables; $USERNumber and $USERName that the user will type in in a popup window like What is the Number and What is the Full Name

and then go on running my script

any ideas?

 

Link to comment
Share on other sites

Hi @JCD

Please take note that you posted on an old topic (over two years old).  It is best-forum-practice to start a new topic even though the topic is similar, unless you are adding to the original post topic.

You might check out the INI functions in the help file.  You can use those to write to and read from an INI test file.  By reading from it you can assigned retrieved values to variables in your script.

So, you can use the same FileExists If logic and if it does not exist then create the file and populate it with the desired data.

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