Announcement

Collapse
No announcement yet.

Writing file to 'Users' temp directory ???

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Writing file to 'Users' temp directory ???

    I need to be able to write a file to the current Users Temp directory.

    Is there a system variable that can be used? The program needs some how to find out current users temp directory. The directory would be in the path:

    c:\Users\{current user}\AppData\MyApp\Temp

    How do I find out the current user?

  • #2
    GetTempPath
    Also see Creating and Using a Temporary File
    Sincerely,

    Steve Rossell
    PowerBASIC Staff

    Comment


    • #3
      My humble effort...

      Code:
      TmpPath$ = ENVIRON$( "APPDATA" ) + "\OpenView"
      IF DIR$( TmpPath$, ONLY 16 ) = "" THEN MKDIR TmpPath$
      Regards,
      Marc

      Comment


      • #4
        AppData would typically be used for Application INI files and stuff that needs constant changing but remains on the machine, aka things that would not be cleaned up during TEMP file removal. If you wish to have a Temp directory under AppData\YourApp you need to manage it completely yourself the way Marc describes.

        If it's TRULY a temp file it belongs in the user Temp directory in my opinion. ENVIRON$("TEMP") is the logged on user's Temp directory, this is where I would store it.
        Last edited by George Bleck; 28 Aug 2009, 01:59 PM.
        <b>George W. Bleck</b>
        <img src='http://www.blecktech.com/myemail.gif'>

        Comment


        • #5
          If it's TRULY a temp file it belongs in the user Temp directory in my opinion. ENVIRON$("TEMP") is the logged on user's Temp directory, this is where I would store it.
          GetTempPath() as suggested above is the better choice. ENVIRON$("TEMP") is used as one of the search paths by GetTempPath() (see SDK doc for complete search path, which varies with O/S).

          FWIW, the folder returned by ENVIRON$("TEMP") may or may not be writable - for that matter it may not even exist, come to think about it! - but GetTempPath() will always return a writable folder.


          MCM
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            Actually, according to the docs for GetTempPath: "Note that the function does not verify that the path exists." There was also a case where GetTempPath used to fail without returning the expected zero error code, many years back-- I don't recall the circumstances. Some odd case, like a Win95 machine using a \\server\share temp path.

            It will certainly pay you to check that the path is functional, whichever approach you use.

            Comment


            • #7
              Yes, and there was confusement about using TMP or TEMP
              Since 98 i never had any trouble with this API, this seems to be a non-issue, we use the temp really often (the customer does actually)
              hellobasic

              Comment


              • #8
                Windows NT/2000/XP: The GetTempPath function does not verify that the directory specified by the TMP or TEMP environment variables exists. The function gets the temporary file path as follows:


                The path specified by the TMP environment variable.
                The path specified by the TEMP environment variable, if TMP is not defined.
                The Windows directory, if both TMP and TEMP are not defined.
                I use GetTempPath() all the time, too, and no problems.

                Maybe this is MS' way of saying "don't mess with the TMP or TEMP environment variables, which the system will set for you at startup!"

                MCM
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  ???
                  Code:
                  TempPath = IIF$(ISValidFolder(ENVIRON$("TEMP")), ENVIRON$("TEMP), _ 
                                  IIF$ (IsValidFolder (ENVIRON$("TMP")), ENVIRON$("TMP"), _ 
                                         ShGetFolderPath (%CSIDL_WINDOWS)))
                  
                  ...
                  FUNCTION IsValidFolder (szPath AS ASCIIZ) AS LONG
                  
                    LOCAL  FA AS LONG
                  
                    FA         = GetFileAttributes(szPath)
                    'MSGBOX "FA=" & HEX$(FA,8) & $CRLF & szPath
                    IF FA = -1& THEN
                        FUNCTION = 0
                    ELSE
                       FUNCTION = (FA AND %FILE_ATTRIBUTE_DIRECTORY) = %FILE_ATTRIBUTE_DIRECTORY
                    END IF
                  
                  END FUNCTION
                  But that does not test for 'writable'. I guess you could add a file name and use...
                  Code:
                  ' FIXED. A Wildcard here is automatically an invalid output file name
                  FUNCTION IsValidOutFileName(szFN AS ASCIIZ) AS LONG
                      ' see if a file name is valid by trying to open it
                      ' returns true if we can create the file...or if it already exists else false
                  
                      LOCAL hFile AS LONG, E AS LONG, fs AS LONG
                      LOCAL iPos AS LONG
                  
                  
                      iPos = INSTR (szFN, ANY "?*")
                      IF ISTRUE iPos THEN
                          FUNCTION = %FALSE
                          EXIT FUNCTION
                      END IF
                  
                      IF DIR$(szFn) <> "" THEN  ' file exists, so the name must be valid
                          FUNCTION  = -1&
                          EXIT FUNCTION
                      END IF
                  
                      hFile = FREEFILE
                      OPEN szFN FOR BINARY AS hFILE
                      E = ERRCLEAR
                      'MSGBOX "E on open of " & szfn & " =" & STR$(E)
                      IF ISFALSE E THEN
                          fs = LOF(hFile)
                          CLOSE hFile
                          IF fs = 0 THEN   ' we just created it...empty..
                             KILL szFN
                          END IF
                      END IF
                      FUNCTION = (E=0)
                  END FUNCTION
                  MCM
                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment

                  Working...
                  X