Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Automatic filename generation

Automatic filename generation

Scheduled Pinned Locked Moved The Lounge
csshelpquestionlounge
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Guy Lecomte
    wrote on last edited by
    #1

    Hello y'all gurus, the less the end user has to deal with files the better it is. I'm fed up with the "file / open" menus... X| . But you all know that the less the end user has to do, the more the developper has to code !;P . Now, my problem is simple. I'd like to manage automatic file names. It is the kind of name M$ uses to deal with temp files ( ex: SBCHDUD01232.TMP ! ). Do you know a way to generate some pseudo random filenames (depending on the computer, the users...). The solution must be compatible with distributed environnement. Interesting chalenge, isn't it ?:-D Best regards and thanks in advance Guy LECOMTE

    S M J 3 Replies Last reply
    0
    • G Guy Lecomte

      Hello y'all gurus, the less the end user has to deal with files the better it is. I'm fed up with the "file / open" menus... X| . But you all know that the less the end user has to do, the more the developper has to code !;P . Now, my problem is simple. I'd like to manage automatic file names. It is the kind of name M$ uses to deal with temp files ( ex: SBCHDUD01232.TMP ! ). Do you know a way to generate some pseudo random filenames (depending on the computer, the users...). The solution must be compatible with distributed environnement. Interesting chalenge, isn't it ?:-D Best regards and thanks in advance Guy LECOMTE

      S Offline
      S Offline
      Stephen Kellett
      wrote on last edited by
      #2

      have you looked at the tmpnam() function? (SBCS) or _wtmpnam() (UNICODE) or _ttmpnam() Stephen Kellett -- C++/Java/Win NT/Unix variants Memory leaks/corruptions/performance/system problems. UK based. Problems with RSI/WRULD? Contact me for advice.

      1 Reply Last reply
      0
      • G Guy Lecomte

        Hello y'all gurus, the less the end user has to deal with files the better it is. I'm fed up with the "file / open" menus... X| . But you all know that the less the end user has to do, the more the developper has to code !;P . Now, my problem is simple. I'd like to manage automatic file names. It is the kind of name M$ uses to deal with temp files ( ex: SBCHDUD01232.TMP ! ). Do you know a way to generate some pseudo random filenames (depending on the computer, the users...). The solution must be compatible with distributed environnement. Interesting chalenge, isn't it ?:-D Best regards and thanks in advance Guy LECOMTE

        M Offline
        M Offline
        Matt Gullett
        wrote on last edited by
        #3

        One possible solution is to generate a GUID, convert it to text and use this as a file name. (I copied portions of the code below from an app I have written, but I modified it for the filename stuff, so there may be a bug in this.) BOOL GetUniqueFileName(LPCSTR lpszDir, LPCSTR lpszFileExt, CString& strFileName) { BOOL bReturn = FALSE; GUID id; unsigned char* szText; HRESULT hr = ::CoCreateGuid(&id); if (hr == S_OK) hr = ::UuidToStringA(&id, &szText); if (hr == S_OK) { // build the new file name strFileName = lpszDir; if (strFileName != "" && strFileName.Right(1) != "\\") strFileName += "\\"; strFileName += (LPCSTR)szText; strFileName += "."; strFileName += lpszFileExt; // free memory allocated by UUIDToString RpcStringFree(&szText); // return TRUE bReturn = TRUE; } return bReturn; }

        P G 2 Replies Last reply
        0
        • M Matt Gullett

          One possible solution is to generate a GUID, convert it to text and use this as a file name. (I copied portions of the code below from an app I have written, but I modified it for the filename stuff, so there may be a bug in this.) BOOL GetUniqueFileName(LPCSTR lpszDir, LPCSTR lpszFileExt, CString& strFileName) { BOOL bReturn = FALSE; GUID id; unsigned char* szText; HRESULT hr = ::CoCreateGuid(&id); if (hr == S_OK) hr = ::UuidToStringA(&id, &szText); if (hr == S_OK) { // build the new file name strFileName = lpszDir; if (strFileName != "" && strFileName.Right(1) != "\\") strFileName += "\\"; strFileName += (LPCSTR)szText; strFileName += "."; strFileName += lpszFileExt; // free memory allocated by UUIDToString RpcStringFree(&szText); // return TRUE bReturn = TRUE; } return bReturn; }

          P Offline
          P Offline
          Pete Bassett
          wrote on last edited by
          #4

          Cant you use the GetTempFileName api call? heres some VB code I use Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long Public Function vbaGetTempFileName(ByVal strDir As String, ByVal strPrefix As String) As String Dim strTempPath As String * 255 Dim lngRetVal As Long lngRetVal = GetTempFileName(strDir, strPrefix, 0, strTempPath) If lngRetVal > 0 Then vbaGetTempFileName = Left$(strTempPath, InStr(strTempPath, Chr(0)) - 1) End If End Function

          M 1 Reply Last reply
          0
          • P Pete Bassett

            Cant you use the GetTempFileName api call? heres some VB code I use Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long Public Function vbaGetTempFileName(ByVal strDir As String, ByVal strPrefix As String) As String Dim strTempPath As String * 255 Dim lngRetVal As Long lngRetVal = GetTempFileName(strDir, strPrefix, 0, strTempPath) If lngRetVal > 0 Then vbaGetTempFileName = Left$(strTempPath, InStr(strTempPath, Chr(0)) - 1) End If End Function

            M Offline
            M Offline
            Matt Gullett
            wrote on last edited by
            #5

            Yes, you can use GetTempFileName, however, in many cases it is necessary to move files around after they are created. If you are going to do this, GetTempFileName can cause problems because the names it generates are only guaranteed to be unique for the directory in which you create them. If I am using files to store data that needs to be moved around, GetTempFileName could generate the same file name on different machines which could then be copied onto each other.

            1 Reply Last reply
            0
            • M Matt Gullett

              One possible solution is to generate a GUID, convert it to text and use this as a file name. (I copied portions of the code below from an app I have written, but I modified it for the filename stuff, so there may be a bug in this.) BOOL GetUniqueFileName(LPCSTR lpszDir, LPCSTR lpszFileExt, CString& strFileName) { BOOL bReturn = FALSE; GUID id; unsigned char* szText; HRESULT hr = ::CoCreateGuid(&id); if (hr == S_OK) hr = ::UuidToStringA(&id, &szText); if (hr == S_OK) { // build the new file name strFileName = lpszDir; if (strFileName != "" && strFileName.Right(1) != "\\") strFileName += "\\"; strFileName += (LPCSTR)szText; strFileName += "."; strFileName += lpszFileExt; // free memory allocated by UUIDToString RpcStringFree(&szText); // return TRUE bReturn = TRUE; } return bReturn; }

              G Offline
              G Offline
              Guy Lecomte
              wrote on last edited by
              #6

              This is the kind of stuff I'm looking for. The problem of tmpname solution is that you can't guaranty the uniqueness of the name: if you work in distributed network several machines (client in the mean of n-tier architecture) can produce the same file name ! In my program, I don't need a temp file (tmpname is produced for a local machine usage mainly). I need to keep the files produced lifetime (forever !) so the problem is not so easy as it seems . The GUID could be a solution but it is 128bytes coded. Maybe too long filenames. Has anybody encounter this kind of problem ? Besst regards Guy LECOMTE;P

              M 1 Reply Last reply
              0
              • G Guy Lecomte

                This is the kind of stuff I'm looking for. The problem of tmpname solution is that you can't guaranty the uniqueness of the name: if you work in distributed network several machines (client in the mean of n-tier architecture) can produce the same file name ! In my program, I don't need a temp file (tmpname is produced for a local machine usage mainly). I need to keep the files produced lifetime (forever !) so the problem is not so easy as it seems . The GUID could be a solution but it is 128bytes coded. Maybe too long filenames. Has anybody encounter this kind of problem ? Besst regards Guy LECOMTE;P

                M Offline
                M Offline
                Matt Gullett
                wrote on last edited by
                #7

                Actually the GUI solution is 128bits wide and produces a 36 BYTE (character) representation. I have actually used this method before for producing file names used in a process where I needed to allow users to attach files to a record in a database. I copied the users file (or files) to the server and saved it with a GUID based file name. Then I used the GUID file name as a reference in the database. The only problem I have encountered with this method is an admin who thought the files had to be "temporary" (because of the unusual names) and deleted the files.

                G 1 Reply Last reply
                0
                • M Matt Gullett

                  Actually the GUI solution is 128bits wide and produces a 36 BYTE (character) representation. I have actually used this method before for producing file names used in a process where I needed to allow users to attach files to a record in a database. I copied the users file (or files) to the server and saved it with a GUID based file name. Then I used the GUID file name as a reference in the database. The only problem I have encountered with this method is an admin who thought the files had to be "temporary" (because of the unusual names) and deleted the files.

                  G Offline
                  G Offline
                  Guy Lecomte
                  wrote on last edited by
                  #8

                  hey Matt, thanks for the quick reply. I think I will use this solution because what I have to do is exactly what you've (succesfully , it seems) done.:) :) :-D Thank you very much and best regards Guy LECOMTE

                  1 Reply Last reply
                  0
                  • G Guy Lecomte

                    Hello y'all gurus, the less the end user has to deal with files the better it is. I'm fed up with the "file / open" menus... X| . But you all know that the less the end user has to do, the more the developper has to code !;P . Now, my problem is simple. I'd like to manage automatic file names. It is the kind of name M$ uses to deal with temp files ( ex: SBCHDUD01232.TMP ! ). Do you know a way to generate some pseudo random filenames (depending on the computer, the users...). The solution must be compatible with distributed environnement. Interesting chalenge, isn't it ?:-D Best regards and thanks in advance Guy LECOMTE

                    J Offline
                    J Offline
                    Jim Howard
                    wrote on last edited by
                    #9

                    ::GetTempFileName( LPCTSTR lpPathName, // directory name LPCTSTR lpPrefixString, // file name prefix UINT uUnique, // integer LPTSTR lpTempFileName // file name buffer );

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups