Advertisement
yorath

Changing the Current Directory

Sep 24th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <tchar.h>
  4.  
  5. #define BUFSIZE MAX_PATH
  6.  
  7. void _tmain(int argc, TCHAR **argv)
  8. {
  9.    TCHAR Buffer[BUFSIZE];
  10.    DWORD dwRet;
  11.  
  12.    if(argc != 2)
  13.    {
  14.       _tprintf(TEXT("Usage: %s <dir>\n"), argv[0]);
  15.       return;
  16.    }
  17.  
  18.    dwRet = GetCurrentDirectory(BUFSIZE, Buffer);
  19.  
  20.    if( dwRet == 0 )
  21.    {
  22.       printf("GetCurrentDirectory failed (%d)\n", GetLastError());
  23.       return;
  24.    }
  25.    if(dwRet > BUFSIZE)
  26.    {
  27.       printf("Buffer too small; need %d characters\n", dwRet);
  28.       return;
  29.    }
  30.  
  31.    if( !SetCurrentDirectory(argv[1]))
  32.    {
  33.       printf("SetCurrentDirectory failed (%d)\n", GetLastError());
  34.       return;
  35.    }
  36.    _tprintf(TEXT("Set current directory to %s\n"), argv[1]);
  37.  
  38.    if( !SetCurrentDirectory(Buffer) )
  39.    {
  40.       printf("SetCurrentDirectory failed (%d)\n", GetLastError());
  41.       return;
  42.    }
  43.    _tprintf(TEXT("Restored previous directory (%s)\n"), Buffer);
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement