fork download
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <errno.h>
  4.  
  5. #define MAXPATH 256
  6.  
  7. void renameAndMove(char *pFileName, char *pszSrcDir, char *pszDestDir, char cPrefix, int iFileCount)
  8. {
  9. char szMoveSrc[MAXPATH]; // ex) gazou\任意文字列.jpg
  10. char szMoveDest[MAXPATH]; // ex) gazou2\A-0001.jpg
  11.  
  12. sprintf(szMoveSrc, "%s\\%c\\%s", pszSrcDir, cPrefix, pFileName);
  13. sprintf(szMoveDest, "%s\\%c-%04d.jpg", pszDestDir, cPrefix, iFileCount);
  14.  
  15. // MoveFileは移動先が存在すると失敗するため先に移動先を削除しておく
  16. DeleteFile(szMoveDest);
  17.  
  18. if(0 == MoveFile(szMoveSrc, szMoveDest)){
  19. printf("*書類移動失敗 [%s] <- [%s]\n", szMoveDest, szMoveSrc);
  20. }else{
  21. printf("書類移動成功 [%s] <- [%s]\n", szMoveDest, szMoveSrc);
  22. }
  23. }
  24.  
  25. int main()
  26. {
  27. WIN32_FIND_DATA FindFileData;
  28. HANDLE hFind;
  29. int iTargetDirCount;
  30. int iTargetFileCount;
  31. char *pszSrcDir = "gazou";
  32. char *pszDestDir = "gazou2";
  33. char *pszPrefixChars = "ABCDEF";
  34. char szFindTarget[MAXPATH]; // ex) gazou\A\*.jpg
  35.  
  36. // MoveFileは移動先フォルダが存在しないと失敗するため先に移動先フォルダを作成しておく
  37. CreateDirectory(pszDestDir, NULL);
  38.  
  39. for(iTargetDirCount = 0; iTargetDirCount < strlen(pszPrefixChars); iTargetDirCount++){
  40. iTargetFileCount = 0;
  41.  
  42. // A~Fまでのフォルダ内検索文字列を作成。対象はjpgファイルのみ。
  43. sprintf(szFindTarget, "%s\\%c\\*.jpg", pszSrcDir, pszPrefixChars[iTargetDirCount]);
  44.  
  45. // 最初のファイルを取得
  46. hFind = FindFirstFile(szFindTarget, &FindFileData);
  47. if (hFind == INVALID_HANDLE_VALUE) {
  48. printf ("書類識別子取得失敗。失敗番号:%d。取得対象:%s\n", GetLastError(), szFindTarget);
  49. } else {
  50. printf ("書類識別子取得成功。取得対象:%s\n", szFindTarget);
  51. renameAndMove(FindFileData.cFileName, pszSrcDir, pszDestDir, pszPrefixChars[iTargetDirCount], ++iTargetFileCount);
  52.  
  53. // 2個目以降のファイルを取得するループ
  54. while(FindNextFile(hFind, &FindFileData) != 0){
  55. renameAndMove(FindFileData.cFileName, pszSrcDir, pszDestDir, pszPrefixChars[iTargetDirCount], ++iTargetFileCount);
  56. }
  57.  
  58. if(FindClose(hFind) == 0){
  59. printf("書類識別子閉鎖失敗\n");
  60. }else{
  61. printf("書類識別子閉鎖成功\n");
  62. }
  63. }
  64. printf("\n");
  65. }
  66. return 0;
  67. }
  68.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:2:21: error: windows.h: No such file or directory
prog.c: In function ‘renameAndMove’:
prog.c:16: warning: implicit declaration of function ‘DeleteFile’
prog.c:18: warning: implicit declaration of function ‘MoveFile’
prog.c: In function ‘main’:
prog.c:27: error: ‘WIN32_FIND_DATA’ undeclared (first use in this function)
prog.c:27: error: (Each undeclared identifier is reported only once
prog.c:27: error: for each function it appears in.)
prog.c:27: error: expected ‘;’ before ‘FindFileData’
prog.c:28: error: ‘HANDLE’ undeclared (first use in this function)
prog.c:28: error: expected ‘;’ before ‘hFind’
prog.c:37: warning: implicit declaration of function ‘CreateDirectory’
prog.c:39: warning: implicit declaration of function ‘strlen’
prog.c:39: warning: incompatible implicit declaration of built-in function ‘strlen’
prog.c:46: error: ‘hFind’ undeclared (first use in this function)
prog.c:46: warning: implicit declaration of function ‘FindFirstFile’
prog.c:46: error: ‘FindFileData’ undeclared (first use in this function)
prog.c:47: error: ‘INVALID_HANDLE_VALUE’ undeclared (first use in this function)
prog.c:48: warning: implicit declaration of function ‘GetLastError’
prog.c:54: warning: implicit declaration of function ‘FindNextFile’
prog.c:58: warning: implicit declaration of function ‘FindClose’
stdout
Standard output is empty