Create Unique File Name in Windows

From PeformIQ Upgrade
Revision as of 17:21, 6 January 2011 by PeterHarding (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Example

 * See : http://msdn.microsoft.com/en-us/library/aa363875(VS.85).aspx


#include <windows.h>
#include <tchar.h>
#include <stdio.h>

#define BUFSIZE 512

int _tmain(int argc, TCHAR *argv[])
{
    HANDLE hFile;
    HANDLE hTempFile; 
    DWORD dwRetVal;
    DWORD dwBytesRead;
    DWORD dwBytesWritten; 
    DWORD dwBufSize=BUFSIZE;
    UINT uRetVal;
    TCHAR szTempName[BUFSIZE];  
    char buffer[BUFSIZE]; 
    TCHAR lpPathBuffer[BUFSIZE];
    BOOL fSuccess;

    if(argc != 2)
    {
        _tprintf(TEXT("Usage: %s <file>\n"), argv[0]);
        return -1;
    }

    // Open the existing file. 
    hFile = CreateFile(argv[1],               // file name 
                       GENERIC_READ,          // open for reading 
                       0,                     // do not share 
                       NULL,                  // default security 
                       OPEN_EXISTING,         // existing file only 
                       FILE_ATTRIBUTE_NORMAL, // normal file 
                       NULL);                 // no template 
    if (hFile == INVALID_HANDLE_VALUE) 
    { 
        printf ("First CreateFile failed (%d)\n", GetLastError());
        return (1);
    } 

     // Get the temp path.
    dwRetVal = GetTempPath(dwBufSize,     // length of the buffer
                           lpPathBuffer); // buffer for path 
    if (dwRetVal > dwBufSize || (dwRetVal == 0))
    {
        printf ("GetTempPath failed (%d)\n", GetLastError());
        return (2);
    }

    // Create a temporary file. 
    uRetVal = GetTempFileName(lpPathBuffer, // directory for tmp files
                              TEXT("NEW"),  // temp file name prefix 
                              0,            // create unique name 
                              szTempName);  // buffer for name 
    if (uRetVal == 0)
    {
        printf ("GetTempFileName failed (%d)\n", GetLastError());
        return (3);
    }

    // Create the new file to write the upper-case version to.
    hTempFile = CreateFile((LPTSTR) szTempName, // file name 
                           GENERIC_READ | GENERIC_WRITE, // open r-w 
                           0,                    // do not share 
                           NULL,                 // default security 
                           CREATE_ALWAYS,        // overwrite existing
                           FILE_ATTRIBUTE_NORMAL,// normal file 
                           NULL);                // no template 
    if (hTempFile == INVALID_HANDLE_VALUE) 
    { 
        printf ("Second CreateFile failed (%d)\n", GetLastError());
        return (4);
    } 

    // Read BUFSIZE blocks to the buffer. Change all characters in 
    // the buffer to upper case. Write the buffer to the temporary 
    // file. 
    do 
    {
        if (ReadFile(hFile, 
                     buffer, 
                     BUFSIZE, 
                     &dwBytesRead, 
                     NULL)) 
        { 
            CharUpperBuffA(buffer, dwBytesRead); 
            fSuccess = WriteFile(hTempFile, 
                                 buffer, 
                                 dwBytesRead,
                                 &dwBytesWritten, 
                                 NULL); 
            if (!fSuccess) 
            {
                printf ("WriteFile failed (%d)\n", GetLastError());
                return (5);
            }
        } 
        else
        {
            printf ("ReadFile failed (%d)\n", GetLastError());
            return (6);
        }
    } while (dwBytesRead == BUFSIZE); 

    // Close the handles to the files.
    fSuccess = CloseHandle (hFile);
    if (!fSuccess) 
    {
       printf ("CloseHandle failed (%d)\n", GetLastError());
       return (7);
    }
    fSuccess = CloseHandle (hTempFile);
    if (!fSuccess) 
    {
       printf ("CloseHandle failed (%d)\n", GetLastError());
       return (8);
    }

    // Move the temporary file to the new text file.
    fSuccess = MoveFileEx(szTempName, 
                          TEXT("Allcaps.txt"), 
                          MOVEFILE_REPLACE_EXISTING);
    if (!fSuccess)
    { 
        printf ("MoveFileEx failed (%d)\n", GetLastError());
        return (9);
    }
    else 
        _tprintf(TEXT("All caps version of %s written to Allcaps.txt\n"), argv[1]);
    return (0);
}