Automatic filename generation
-
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
-
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
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.
-
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
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; }
-
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; }
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
-
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
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.
-
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; }
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
-
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
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.
-
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.
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
-
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
::GetTempFileName( LPCTSTR lpPathName, // directory name LPCTSTR lpPrefixString, // file name prefix UINT uUnique, // integer LPTSTR lpTempFileName // file name buffer );