1 // FileToolsV20120605.cpp : Defines the entry point for the application.
   2 //
   3 //用vc6.0建立一个win32简单应用程序
   4
   5 #include "stdafx.h"
   6 #include <stdio.h>
   7 #include <fcntl.h>
   8 #include <io.h>
   9 #include <iostream>
  10 #include <fstream>
  11 #include <conio.h>
  12 #include <SHELLAPI.H>
  13
  14 //
  15
  16 //调试开关:
  17
  18 //#define FLAG_DEBUG
  19
  20
  21
  22 //
  23
  24 //要删除的目录
  25
  26 #define FIND_FILE_PATH "C:\\Program Files"
  27
  28 //要删除的文件类型
  29
  30 #define FIND_FILE_TYPE ".exe"
  31
  32 //
  33
  34
  35
  36 #define FILE_NAME_TYPE "*.*"
  37
  38
  39
  40
  41
  42
  43
  44 //最大的文件查找字节数
  45
  46 #define MAX_FIND_PATH 1000
  47
  48
  49
  50 //函数返回值:
  51
  52 #define RTN_OK (0)
  53
  54 #define RTN_ERR (1)
  55
  56
  57
  58 //休眠秒数
  59
  60 #define SLEEP_SECOND 100
  61
  62
  63
  64 //LOGO字符串
  65
  66 #define STR_LOGO "\
  67 ┌---------------------------------------------------------------┐\n\
  68 │Files Toos [版本 FTV01R01C01]                                  │ \n\
  69 │ Build By BinG @2012.3, Email:zww0815@gmail.com.               │ \n\
  70 │ 版权所有 (c) 2012 Zkf39219。保留所有权利。                    │ \n\
  71 └---------------------------------------------------------------┘\n"
  72
  73
  74
  75     //帮助信息字符串
  76
  77 #define STR_HELP ""\
  78     " 查看帮助\n"\
  79     "m 修改配置信息\n"\
  80     "q 退出\n"\
  81     "d 打开调试开关\n"\
  82     "c 清屏\n"\
  83     "t 测试\n"\
  84     "-----------------------------------\n"\
  85     "自己输入:y,自己不想输入:<任意键>\n"
  86
  87 //往屏幕打印信息
  88 #define PRINTTOSCR(str) do\
  89 {\
  90 printf("%s",str);\
  91 } while (0);
  92
  93
  94
  95
  96
  97 enum
  98 {
  99     FLAG_ZERO, //0 :默认0
 100     FLAG_VIEW_FILE,//1 :查看文件
 101     FLAG_DELETE, //2 :删除文件
 102     FLAG_INIT_OK, //3 :全局变量初始化标志
 103     FLAG_INIT_NO, //4 :全局变量没有初始化
 104     FLAG_ONE_LOG, //5 :打开程序只能有一个log标志
 105     FLAG_FILENAME_TIME, //6 :文件以时间命令。
 106     FLAG_FILENAME_ZHID, //7 :文件指定命名
 107 };
 108
 109
 110
 111
 112
 113
 114
 115 //-----------------------------------------------------------
 116
 117 // 函数声明
 118
 119 //-----------------------------------------------------------
 120
 121 #ifdef __cplusplus
 122 extern "C" {
 123 #endif //__cplusplus
 124
 125     void InitGlobalVar();
 126     void PrintLocalTime(void* flag);
 127     void fOpenFileFp(FILE** fp);
 128     void fCloseFileFp(FILE** fp);
 129     void VisitAllFiles(char * lpPath);
 130     void GetCurSysTime(char* str);
 131     void FileOptions();
 132     void CloseOrFreeGlobalVar();
 133     void DebugAndPrintAllGlobalVar();
 134     int FileOpt();
 135
 136 #ifdef __cplusplus
 137 }
 138 #endif //__cplusplus
 139
 140
 141
 142
 143
 144 #ifndef _USE_OLD_IOSTREAMS
 145
 146 using namespace std;
 147
 148 #endif
 149
 150
 151
 152 // maximum mumber of lines the output console should have
 153
 154 static const WORD MAX_CONSOLE_LINES = 500;
 155
 156 //-----------------------------------------------------------
 157
 158 // 全局变量声明(定义)
 159
 160 //-----------------------------------------------------------
 161
 162 //全局的文件计数与文件清理计数
 163
 164 static long g_lCheckFileNumber;
 165
 166 static long g_lCheckOkFileNumber;
 167
 168 static long g_lClearFileNumber;
 169
 170 static int g_flagDeleteFile;
 171
 172 static int g_flagOnlyOneLog = FLAG_ONE_LOG;
 173
 174
 175
 176 //
 177
 178 //用于全局初始化函数中的是否初始化标志。
 179
 180 //初始化时置1
 181
 182 //调用CloseOrFreeGlobalVar函数时重新置0
 183
 184 //默认值:FLAG_INIT_NO 0
 185
 186 static int g_flagInitFuncIsFinish=FLAG_INIT_NO;
 187
 188 /
 189
 190 ///全局的指针变量
 191
 192
 193
 194 /* 定义全局的FILE类型指针,供写日志 */
 195
 196 FILE* g_fpLog;
 197
 198 FILE* g_fpCheckOK;
 199
 200 /
 201
 202 //定义全局的查找文件路径及类型
 203
 204 char g_strFindFilePath[MAX_FIND_PATH];
 205
 206 char g_strFindFileType[10];
 207
 208
 209
 210 //全局的时间字符串
 211
 212 char g_pStrTime[50];
 213
 214
 215
 216 //全局的文件路径名字符串
 217
 218 char g_strTemFileName[80];
 219
 220
 221
 222 //函数访问次数
 223
 224 int g_iCount = 0;
 225
 226
 227 void RedirectIOToConsole()
 228 {
 229
 230     int hConHandle;
 231     long lStdHandle;
 232     CONSOLE_SCREEN_BUFFER_INFO coninfo;
 233     FILE *fp;
 234
 235     // 分配一个控制台程序
 236     AllocConsole();
 237
 238     // 设置足够大的屏幕缓存可以让我们滑动文本
 239     GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
 240
 241     // 设置控制台屏幕的高度
 242     coninfo.dwSize.Y = MAX_CONSOLE_LINES;
 243
 244     // 设置控制台屏幕缓存大小
 245     SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
 246
 247     // 获取标准输出句柄
 248     lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
 249
 250     //打开标准输出句柄,类似打开文件的方式如fopen,返回一个文件描述符
 251     hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
 252
 253     // 以可写的方式打开
 254     fp = _fdopen( hConHandle, "w" );
 255     *stdout = *fp;
 256     setvbuf( stdout, NULL, _IONBF, 0 );
 257
 258     // redirect unbuffered STDIN to the console
 259     lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
 260     hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
 261     fp = _fdopen( hConHandle, "r" );
 262     *stdin = *fp;
 263     setvbuf( stdin, NULL, _IONBF, 0 );
 264
 265     // redirect unbuffered STDERR to the console
 266     lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
 267     hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
 268     fp = _fdopen( hConHandle, "w" );
 269     *stderr = *fp;
 270     setvbuf( stderr, NULL, _IONBF, 0 );
 271
 272     // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog
 273     // point to console as well
 274     ios::sync_with_stdio();
 275 }
 276
 277
 278
 279 void test()
 280 {
 281     int iVar;
 282     RedirectIOToConsole();
 283
 284     // test stdio
 285     fprintf(stdout, "Test output to stdout\n");
 286     fprintf(stderr, "Test output to stderr\n");
 287     fprintf(stdout, "Enter an integer to test stdin: ");
 288     scanf("%d", &iVar);
 289     printf("You entered %d\n", iVar);
 290
 291     //test iostreams
 292     cout << "Test output to cout" << endl;
 293     cerr << "Test output to cerr" << endl;
 294     clog << "Test output to clog" << endl;
 295     cout << "Enter an integer to test cin: ";
 296     cin >> iVar;
 297     cout << "You entered " << iVar << endl;
 298 }
 299
 300
 301
 302
 303
 304 int APIENTRY WinMain(HINSTANCE hInstance,
 305
 306                      HINSTANCE hPrevInstance,
 307
 308                      LPSTR lpCmdLine,
 309
 310                      int nCmdShow)
 311
 312 {
 313
 314     // TODO: Place code here.
 315
 316
 317
 318     //test();
 319
 320     RedirectIOToConsole();
 321
 322     (void)FileOpt();
 323
 324     return 0;
 325
 326 }
 327
 328
 329
 330
 331
 332
 333
 334 //-----------------------------------------------------------
 335
 336 // main函数
 337
 338 //-----------------------------------------------------------
 339
 340 #if 1
 341
 342 int FileOpt()
 343
 344 {
 345
 346 #if 1
 347
 348     InitGlobalVar();
 349
 350     FileOptions();
 351
 352
 353
 354 #ifdef FLAG_DEBUG
 355
 356     DebugAndPrintAllGlobalVar();
 357
 358 #endif
 359
 360     CloseOrFreeGlobalVar();
 361
 362
 363
 364 #endif
 365
 366     return 0;
 367
 368 }
 369
 370 #endif // 0
 371
 372
 373
 374
 375
 376
 377
 378 //-----------------------------------------------------------
 379
 380 // 函数实现
 381
 382 //-----------------------------------------------------------
 383
 384
 385
 386 /************************************************************************/
 387
 388 /*说明: 同时打印输出到文件及屏幕上 */
 389
 390 /* stream: 文件句柄,即文件描述符 */
 391
 392 /* format: 格式字符串 */
 393
 394 /* Build by zkf39219@2012.3 */
 395
 396 /************************************************************************/
 397
 398 void WriteFormatted (FILE * stream, char * format, ...)
 399
 400 {
 401
 402     /*BEGIN: Modify by zkf39219 @ 2012.511 for pointer NULL error */
 403
 404     if (NULL == stream)
 405
 406     {
 407
 408         printf("FILE pointer init error:IS NULL!!!\n");
 409
 410         return;
 411
 412     }
 413
 414     /*END: Modify by zkf39219 @ 2012.5.11 for pointer NULL error */
 415
 416
 417
 418     //获取时间信息
 419
 420     GetCurSysTime(g_pStrTime);
 421
 422
 423
 424     va_list args;
 425
 426     va_start (args, format);
 427
 428     vfprintf (stream, format, args);
 429
 430     vprintf (format, args);
 431
 432     va_end (args);
 433
 434 }
 435
 436
 437
 438
 439 /////
 440
 441 //功能:
 442
 443 //1.打开文件句柄
 444
 445 //2.标志不同输出文件不同:
 446
 447 //3.标志flag为FLAG_FILENAME_TIME:1时:文件名用时间命名
 448
 449 //4.标志flag为FLAG_FILENAME_ZHID:2时:指定命名
 450
 451 void fOpenFileFp(FILE** fp,
 452
 453                  int flag)//标志,1:文件名用时间命名,2:指定命名
 454
 455 {
 456
 457     if (NULL == *fp)
 458
 459     {
 460
 461         *fp = (FILE*)malloc(sizeof(FILE));
 462
 463
 464
 465         if (NULL == *fp)
 466
 467         {
 468
 469             printf("malloc error!!!\n");
 470
 471             return;
 472
 473         }
 474
 475     }
 476
 477
 478
 479     if (FLAG_ONE_LOG == g_flagOnlyOneLog)
 480
 481     {
 482
 483         char strTemFileName[80];
 484
 485         GetCurSysTime(strTemFileName);
 486
 487         strcat(strTemFileName,".log");
 488
 489         strcpy(g_strTemFileName,strTemFileName);
 490
 491     }
 492
 493
 494
 495
 496
 497     if (FLAG_FILENAME_TIME == flag && (*fp = fopen(g_strTemFileName, "a")) == NULL)
 498
 499     {
 500
 501         WriteFormatted(g_fpLog,"Can't open %s \n", g_strTemFileName);
 502
 503         exit(-1);
 504
 505     }
 506
 507     else if(FLAG_FILENAME_ZHID == flag && (*fp = fopen("OutPut.txt", "a")) == NULL)
 508
 509     {
 510
 511         WriteFormatted(g_fpLog,"Can't open %s \n", g_strTemFileName);
 512
 513         exit(-1);
 514
 515     }
 516
 517
 518
 519     g_flagOnlyOneLog++;
 520
 521 }
 522
 523
 524
 525 //调试时打印所有全局变量
 526
 527 void DebugAndPrintAllGlobalVar()
 528
 529 {
 530
 531     WriteFormatted(g_fpLog,
 532     "g_lCheckFileNumber :Ox%08x\n\
 533 g_lCheckOkFileNumber :Ox%08x\n\
 534 g_lClearFileNumber :Ox%08x\n\
 535 g_flagDeleteFile :Ox%08x\n\
 536 g_fpLog :Ox%08x\n\
 537 g_fpCheckOK :Ox%08x\n\
 538 g_pStrTime :Ox%08x\n\
 539 g_strFindFilePath :Ox%08x\n\
 540 g_strFindFileType :Ox%08x\n\
 541 g_iCount :Ox%08x\n\
 542 g_flagInitFuncIsFinish:Ox%08x\n--->%s\n",
 543 g_lCheckFileNumber,
 544 g_lCheckOkFileNumber,
 545 g_lClearFileNumber,
 546 g_flagDeleteFile,
 547 g_fpLog,g_fpCheckOK,g_pStrTime,g_strFindFilePath,
 548 g_strFindFileType,
 549 g_iCount,
 550 g_flagInitFuncIsFinish,
 551 (g_flagInitFuncIsFinish==FLAG_INIT_NO)?"内存已释放":"危险:内存没有释放");
 552
 553
 554
 555     //释放指针g_fpLog的前提是标志g_flagInitFuncIsFinish置FLAG_INIT_NO
 556
 557     //即其它内存已经释放
 558
 559     if (FLAG_INIT_NO == g_flagInitFuncIsFinish)
 560
 561     {
 562
 563         fCloseFileFp(&g_fpLog);
 564
 565         printf("g_fpLog :Ox%08x\n",g_fpLog);
 566
 567     }
 568
 569 }
 570
 571
 572
 573 //
 574
 575 //功能:
 576
 577 // 初始化全局变量
 578
 579 // 为了避免重复初始化,该函数初始化完成后置标志为FLAG_INIT_OK
 580
 581 void InitGlobalVar()
 582
 583 {
 584
 585     //先判断是否已经初始化,已初始化直接返回
 586
 587     if (FLAG_INIT_OK == g_flagInitFuncIsFinish)
 588
 589     {
 590
 591         return;
 592
 593     }
 594
 595
 596
 597     g_lCheckFileNumber = 0;
 598
 599     g_lCheckOkFileNumber = 0;
 600
 601     g_lClearFileNumber = 0;
 602
 603     g_flagDeleteFile = FLAG_VIEW_FILE;
 604
 605
 606
 607     g_fpLog = NULL;
 608
 609     g_fpCheckOK = NULL;
 610
 611
 612
 613     /* 初始化全局变量 g_fpLog */
 614
 615     if (NULL == g_fpLog)
 616
 617     {
 618
 619         fOpenFileFp(&g_fpLog,FLAG_FILENAME_TIME);
 620
 621     }
 622
 623
 624
 625     /* 初始化全局变量 fp */
 626
 627     if (NULL == g_fpCheckOK)
 628
 629     {
 630
 631         fOpenFileFp(&g_fpCheckOK,FLAG_FILENAME_ZHID);
 632
 633     }
 634
 635
 636
 637     /* 初始化全局变量 */
 638
 639     GetCurSysTime( g_pStrTime );
 640
 641
 642
 643     //strcpy(g_strFindFilePath,FIND_FILE_PATH);
 644
 645     //strcpy(g_strFindFileType,FIND_FILE_TYPE);
 646
 647
 648
 649     g_iCount = 0;
 650
 651     g_flagInitFuncIsFinish = FLAG_INIT_OK;
 652
 653 }
 654
 655 void FreePTime(PSYSTEMTIME pTime)
 656
 657 {
 658
 659     /* 一定要进行下面判断,否则会造成重复释放NULL地址而崩溃 */
 660
 661     if (NULL != pTime)
 662
 663     {
 664
 665         free(pTime);
 666
 667         pTime = NULL;
 668
 669     }
 670
 671 }
 672
 673
 674
 675
 676
 677
 678
 679 //打开文件句柄
 680
 681 void fCloseFileFp(FILE** fp)
 682
 683 {
 684
 685     if (*fp)
 686
 687     {
 688
 689         fclose(*fp);
 690
 691         *fp = NULL;
 692
 693     }
 694
 695 }
 696
 697
 698
 699 ///
 700
 701 //函数功能:
 702
 703 //1.对全局的指针进行释放,并指向NULL
 704
 705 //2.对全局变量置默认值
 706
 707 //3.如果发现初始化标志没有OK,返回
 708
 709 void CloseOrFreeGlobalVar()
 710
 711 {
 712
 713     if (FLAG_INIT_NO == g_flagInitFuncIsFinish )
 714
 715     {
 716
 717         return;
 718
 719     }
 720
 721
 722
 723     /* 调试时不在这释放日志内存指针g_fpLog,由调试函数释放 */
 724
 725 #ifndef FLAG_DEBUG
 726
 727     fCloseFileFp(&g_fpLog);
 728
 729 #endif
 730
 731     fCloseFileFp(&g_fpCheckOK);
 732
 733
 734
 735     g_lCheckFileNumber = 0;
 736
 737     g_lCheckOkFileNumber = 0;
 738
 739     g_lClearFileNumber = 0;
 740
 741     g_flagDeleteFile = FLAG_VIEW_FILE;
 742
 743
 744
 745     //下面两行存在重大隐患:指针虽然指向NULL,但其内存空间没有释放,造成内存泄漏
 746
 747     //fCloseFileFp函数里有指向NULL,所以这里不要再指向NULL.
 748
 749     //g_fpLog = NULL;
 750
 751     //g_fpCheckOK = NULL;
 752
 753
 754
 755     g_flagInitFuncIsFinish = FLAG_INIT_NO;
 756
 757
 758
 759 #ifdef FLAG_DEBUG
 760
 761     //上面没有释放的句柄应该在这里面释放
 762
 763     DebugAndPrintAllGlobalVar();
 764
 765 #endif
 766
 767 }
 768
 769
 770
 771 ////
 772
 773 /* Modify by zengwenwu @ 2012-3-19 :增加一标志,提供不同打印要求 */
 774
 775 //1:打印长时间
 776
 777 //0:短时间
 778
 779 //-1:不打印,相当于只对pTime做赋值
 780
 781 void GetCurSysTime(char* str)
 782
 783 {
 784
 785     LPSYSTEMTIME pstTemSysTime = NULL;
 786
 787
 788
 789     pstTemSysTime = (LPSYSTEMTIME)malloc( sizeof(SYSTEMTIME) );
 790
 791
 792
 793     if ( NULL == pstTemSysTime )
 794
 795     {
 796
 797         WriteFormatted(g_fpLog,"SYSTEMTIME malloc is err\n" );
 798
 799         return;
 800
 801     }
 802
 803
 804
 805     memset(pstTemSysTime,0,sizeof(LPSYSTEMTIME));
 806
 807
 808
 809     GetLocalTime( pstTemSysTime );
 810
 811
 812
 813     sprintf((char*)str,"%04d-%02d-%02d %02d_%02d_%02d %03d",
 814
 815         pstTemSysTime->wYear,
 816
 817         pstTemSysTime->wMonth,
 818
 819         pstTemSysTime->wDay,
 820
 821         pstTemSysTime->wHour,
 822
 823         pstTemSysTime->wMinute,
 824
 825         pstTemSysTime->wSecond,
 826
 827         pstTemSysTime->wMilliseconds
 828
 829         );
 830
 831 }
 832
 833
 834 /////
 835
 836 //Function:
 837
 838 // Visit all folders and files
 839
 840 //
 841
 842 //Paremeter:
 843
 844 // char *lpPath -- path of file
 845
 846 //
 847
 848 //Return:
 849
 850 // void
 851
 852 //
 853
 854
 855
 856 void VisitAllFiles(char * lpPath)
 857 {
 858     Sleep(SLEEP_SECOND);
 859     char szFind[MAX_PATH];
 860     WIN32_FIND_DATA FindFileData;
 861     char chTemPath[MAX_FIND_PATH];
 862
 863     strcpy(chTemPath,lpPath);
 864     strcpy(szFind,lpPath);
 865     strcat(szFind,"\\*.*");
 866
 867     /* 对全局时间g_pTime变量赋值 */
 868     if (0 == g_iCount++)
 869     {
 870         WriteFormatted(g_fpLog,"查找%s下%s类型的文件\n",g_strFindFilePath,g_strFindFileType);
 871         WriteFormatted(g_fpLog,"BEGIN-------------------------------------------------%s\n",g_pStrTime);
 872         WriteFormatted(g_fpCheckOK,"%s\n","\n\n");
 873         WriteFormatted(g_fpCheckOK,"查找目录%s下%s类型的文件\n",g_strFindFilePath,g_strFindFileType);
 874         WriteFormatted(g_fpCheckOK,"BEGIN-------------------------------------------------%s\n",g_pStrTime);
 875     }
 876
 877
 878     /*printf("+------进入目录--->\n+[%s]\n\r",chTemPath);*/
 879     WriteFormatted(g_fpLog,"+------进入目录--->\n+[%s]\n",chTemPath);
 880
 881     HANDLE hFind=::FindFirstFile(szFind,&FindFileData);
 882
 883     if(INVALID_HANDLE_VALUE == hFind)
 884     {
 885         WriteFormatted(g_fpLog,"<%s>No %s file found\n",g_pStrTime,g_strFindFileType);
 886         return;
 887     }
 888
 889     while(TRUE)
 890     {
 891         //If director, visit all sub-folders
 892         if((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
 893         {
 894             g_lCheckFileNumber++;
 895             if(FindFileData.cFileName[0]!='.')
 896             {
 897                 char szFile[MAX_PATH];
 898                 strcpy(szFile,lpPath);
 899                 strcat(szFile,"\\");
 900                 /*strcat(chTemPath,szFile);*/
 901                 strcat(szFile,FindFileData.cFileName);
 902                 VisitAllFiles(szFile);
 903             }
 904         }
 905         else
 906         {
 907             //Judge if TYPE file
 908             g_lCheckFileNumber++;
 909             /*printf(" -%s\n",FindFileData.cFileName);*/
 910
 911             int len = strlen(FindFileData.cFileName);
 912             int iStrLen = strlen(g_strFindFileType);
 913
 914             //除去目录,共检查文件数
 915             const char *p = (char *)&FindFileData.cFileName[len-iStrLen];
 916
 917             //case insentive!
 918             if ( (0 == strcmp("*",g_strFindFileType)) || (0 == strcmp("*.*",g_strFindFileType)) )
 919             {
 920                 //查看所有目录文件:
 921                 g_lCheckOkFileNumber++;
 922
 923                 //if exe file, check it
 924                 char strFileName[MAX_PATH];
 925                 strcpy(strFileName,lpPath);
 926                 strcat(strFileName,"\\");
 927                 strcat(strFileName,FindFileData.cFileName);
 928                 WriteFormatted(g_fpLog,"<%s>已检查文件数:%ld,符合条件数:%ld,刚检查到的文件是:\n%s\n",
 929                     g_pStrTime,
 930                     g_lCheckFileNumber,
 931                     g_lCheckOkFileNumber,
 932                     strFileName);
 933
 934                 fprintf(g_fpCheckOK,"<%s> %s\n",
 935                     g_pStrTime,
 936                     strFileName);
 937             }
 938
 939             else if ((_stricmp(p, g_strFindFileType) == 0))
 940             {
 941                 g_lCheckOkFileNumber++;
 942                 //if exe file, check it
 943                 char strFileName[MAX_PATH];
 944                 strcpy(strFileName,lpPath);
 945                 strcat(strFileName,"\\");
 946                 strcat(strFileName,FindFileData.cFileName);
 947
 948                 WriteFormatted(g_fpLog,"<%s>已检查文件数:%ld,符合条件数:%ld,刚检查到的文件是:\n%s\n",
 949                     g_pStrTime,
 950                     g_lCheckFileNumber,
 951                     g_lCheckOkFileNumber,
 952                     strFileName);
 953
 954                 fprintf(g_fpCheckOK,"<%s> %s\n",
 955                     g_pStrTime,
 956                     strFileName);
 957
 958                 /* 置删除标志时,删除文件 */
 959                 /*if (CheckWHVirus(fp))*/
 960                 if (g_flagDeleteFile == FLAG_DELETE)
 961                 {
 962                     /*PrintLocalTime(g_pStrTime);*/
 963                     /*ClearVirus(fp, strFileName);*/ //fp closed in the function
 964                     /*printf("Virus Found! %s and cleared\r\n", strFileName);*/
 965                     /* 去只读 */
 966                     SetFileAttributes(strFileName,FILE_ATTRIBUTE_NORMAL);
 967
 968                     /* Long,非零表示成功,零表示失败。会设置GetLastError */
 969                     //if (DeleteFile(strFileName))
 970                     if (!remove(strFileName))
 971                     {
 972                         g_lClearFileNumber++;
 973                         /*printf("Delete file: %s successfully,已清除文件数:%ld.\n", strFileName,g_lClearFileNumber);*/
 974                         WriteFormatted(g_fpLog,"<%s>---Delete file: %s successfully,已清除文件数:%ld.\n", g_pStrTime,strFileName,g_lClearFileNumber);
 975                     }
 976                     else
 977                     {
 978                         /*printf("Delete file: %s failed!\n", strFileName);*/
 979                         WriteFormatted(g_fpLog,"<%s>---Delete file: %s failed!\n", g_pStrTime,strFileName);
 980                     }
 981                 }
 982
 983                 /*system("cls");*/
 984
 985                 /*Sleep(SLEEP_SECOND);*/
 986             }
 987 }
 988
 989
 990 //Find next file
 991 if(!FindNextFile(hFind,&FindFileData))
 992 {
 993     // printf("【OK,There is not %s type file in PATH %s】\n",TYPE,chTemPath);
 994     // fprintf(g_fpLog,"---------------->【OK,There is not %s type file any more in PATH %s】\n",TYPE,chTemPath);
 995     break;
 996 }
 997 }
 998
 999
1000 /*GetCurSysTime(g_pStrTime);*/
1001 /*fprintf(g_fpLog,"END---------------------------------------------------%s\n",g_pStrTime);*/
1002
1003 FindClose(hFind);
1004 }
1005
1006
1007 //从配置文件中读取配置信息:
1008 //PATH:需要进行查找/删除的路径名
1009 //TYPE:需要进行查找的文件类型名称
1010 //DORV:是删除还是查看:0查看,1删除
1011 void ReadConfigFromFile()
1012 {
1013     FILE * fpConf = NULL;
1014     char strPathTemp[100];
1015     char strTypeTemp[10];
1016     /*char strCurTime[50];*/
1017     int flagDelOrView = 0;
1018     char strCfgFilePath[100];
1019
1020     memset(strPathTemp,0,sizeof(strPathTemp));
1021     memset(strTypeTemp,0,sizeof(strTypeTemp));
1022     memset(strCfgFilePath,0,sizeof(strTypeTemp));
1023
1024
1025     GetCurrentDirectory(MAX_FIND_PATH,strCfgFilePath);
1026
1027     if (NULL == (fpConf = fopen("config.cfg","r")))
1028     {
1029         WriteFormatted(g_fpLog,"Read config file error!!!->%s\n",strCfgFilePath);
1030
1031         WriteFormatted(g_fpLog,"获取当前路径为:%s\n",strCfgFilePath);
1032         strcat(strCfgFilePath,"\\");
1033         strcat(strCfgFilePath,"config.cfg");
1034
1035         if (NULL == (fpConf = fopen(strCfgFilePath,"w")))
1036         {
1037             WriteFormatted(g_fpLog,"Read config file error!!!->%s\n",strCfgFilePath);
1038             exit(0);
1039         }
1040     }
1041
1042     GetCurSysTime(g_pStrTime);
1043
1044     fscanf(fpConf,"PATH=%s\n",strPathTemp);
1045     fscanf(fpConf,"TYPE=%s\n",strTypeTemp);
1046     fscanf(fpConf,"DORV=%d\n",&flagDelOrView);
1047
1048     WriteFormatted(g_fpLog,"\n读取的文件配置信息:\n",g_pStrTime);
1049     WriteFormatted(g_fpLog,"PATH=%s\n",strPathTemp);
1050     WriteFormatted(g_fpLog,"TYPE=%s\n",strTypeTemp);
1051     WriteFormatted(g_fpLog,"DORV=%d\n",flagDelOrView);
1052
1053     if (!(strcmp(strPathTemp,"") && strcmp(strTypeTemp,"")))
1054     {
1055         WriteFormatted(g_fpLog,"\n配置为空,请修改配置文件\n");
1056         return;
1057     }
1058
1059     strcpy(g_strFindFilePath,strPathTemp);
1060     strcpy(g_strFindFileType,strTypeTemp);
1061     g_flagDeleteFile = flagDelOrView;
1062
1063     WriteFormatted(g_fpLog,"\n全局配置信息:\n",g_pStrTime);
1064     WriteFormatted(g_fpLog,"g_strFindFilePath=%s\n",g_strFindFilePath);
1065     WriteFormatted(g_fpLog,"g_strFindFileType=%s\n",g_strFindFileType);
1066     WriteFormatted(g_fpLog,"g_flagDeleteFile=%d\n",g_flagDeleteFile);
1067
1068     fCloseFileFp(&fpConf);
1069     WriteFormatted(g_fpLog,"Read config successfully!!!\n");
1070     //Sleep(3000);
1071 }
1072
1073
1074 //保存配置到配置文件中:
1075 //PATH:需要进行查找/删除的路径名
1076 //TYPE:需要进行查找的文件类型名称
1077 //DORV:是删除还是查看:0查看,1删除
1078 void SaveConfigToFile()
1079 {
1080     FILE * fpConf = NULL;
1081     char strPathTemp[100];
1082     char strTypeTemp[10];
1083     /*char strCurTime[50];*/
1084     int flagDelOrView = 0;
1085
1086     WriteFormatted(g_fpLog,"\n保存配置信息:\n");
1087
1088     memset(strPathTemp,0,sizeof(strPathTemp));
1089     memset(strTypeTemp,0,sizeof(strTypeTemp));
1090
1091     if (NULL == (fpConf = fopen("config.cfg","w")))
1092     {
1093         WriteFormatted(g_fpLog,"Read config file error !!!\n");
1094         return;
1095     }
1096
1097     if (!(strcmp(g_strFindFilePath,"") && strcmp(g_strFindFileType,"")))
1098     {
1099         WriteFormatted(g_fpLog,"配置为空,请重新配置\n");
1100         return;
1101     }
1102
1103     //将配置信息打印出来
1104     //保存到配置文件中
1105     /*fprintf(g_fpLog,"<%s>全局配置信息:\n",g_pStrTime);*/
1106     fprintf(fpConf,"PATH=%s\n",g_strFindFilePath);
1107     fprintf(fpConf,"TYPE=%s\n",g_strFindFileType);
1108     fprintf(fpConf,"DORV=%d\n",g_flagDeleteFile);
1109
1110     //将保存后的信息写到日志及标准输出中
1111     WriteFormatted(g_fpLog,"保存配置后的配置信息:\n",g_pStrTime);
1112     WriteFormatted(g_fpLog,"PATH=%s\n",g_strFindFilePath);
1113     WriteFormatted(g_fpLog,"TYPE=%s\n",g_strFindFileType);
1114     WriteFormatted(g_fpLog,"DORV=%d\n",g_flagDeleteFile);
1115
1116     fCloseFileFp(&fpConf);
1117     WriteFormatted(g_fpLog,"Save config successfully!!!\n");
1118     //Sleep(3000);
1119 }
1120
1121
1122 void ModifyCfg()
1123 {
1124     char ch;
1125
1126     while (1)
1127     {
1128         fflush(stdin);
1129         flushall();
1130
1131         WriteFormatted(g_fpLog,"<%s>[m]",g_pStrTime);
1132
1133         ch = getch();
1134         WriteFormatted(g_fpLog,"%c\n",ch);
1135         if (13 == ch)
1136         {
1137             continue;
1138         }
1139         else if ('1' == ch)
1140         {
1141             WriteFormatted(g_fpLog,"\n请输入路径:");
1142             scanf("%s",g_strFindFilePath);
1143             continue;
1144         }
1145         else if ('2' == ch)
1146         {
1147             WriteFormatted(g_fpLog,"\n请输入需要查找的文件类型:");
1148             scanf("%s",g_strFindFileType);
1149             continue;
1150         }
1151         else if ('3' == ch)
1152         {
1153             WriteFormatted(g_fpLog,"\n请输入是查看还是删除:0:查看,2:删除\n");
1154             scanf("%d",&g_flagDeleteFile);
1155             //如果不是删除或查看的选择,默认为查看
1156             if ((g_flagDeleteFile != FLAG_DELETE) && (g_flagDeleteFile != FLAG_VIEW_FILE))
1157             {
1158                 g_flagDeleteFile = FLAG_VIEW_FILE;
1159             }
1160             continue;
1161         }
1162         else if ('q' == ch || 'e' == ch)
1163         {
1164             //SaveConfigToFile();
1165             break;
1166         }
1167         else if ('s' == ch)
1168         {
1169             //保存配置到配置文件中
1170             SaveConfigToFile();
1171             /*system("dir");*/
1172         }
1173         else if ('r' == ch)
1174         {
1175             //从文件中读取配置
1176             ReadConfigFromFile();
1177             /*system("dir");*/
1178         }
1179         else if ('?' == ch || 'h' == ch)
1180         {
1181             //WriteFormatted(g_fpLog,"\n\n");
1182             WriteFormatted(g_fpLog,"%s\n","\n修改配置文件:\n1.路径 \n2.文件类型 \n3.删除(0-查看,2-删除) \nq,e退出\n");
1183             continue;
1184         }
1185         else
1186         {
1187             WriteFormatted(g_fpLog,"\nInput error,please check it.\n\n");
1188             continue;
1189         }
1190     }
1191
1192
1193
1194     // if(!strcmp("",g_strFindFilePath) || !strcmp("",g_strFindFileType))
1195     // {
1196     // WriteFormatted(g_fpLog,"竟然不输入路径和类型,想搞死我啊!!!\n");
1197     // /*GetCurrentDirectory(MAX_FIND_PATH,g_strFindFilePath);*/
1198     // // memset(g_strFindFileType,0,10);
1199     // // strcpy(g_strFindFileType,"log");
1200     //
1201     // ReadConfigFromFile();
1202     // }
1203
1204
1205
1206
1207
1208 #ifdef FLAG_DEBUG
1209     WriteFormatted(g_fpLog,
1210         "g_strFindFilePath:%s\n,g_strFindFileType:%s\n,g_flagDeleteFile:%d\n",
1211         g_strFindFilePath,
1212         g_strFindFileType,
1213         g_flagDeleteFile);
1214 #endif //FLAG_DEBUG
1215 }
1216
1217
1218 void My_ShellExecute( HWND hwnd,
1219                      LPCTSTR lpOperation,
1220                      LPCTSTR lpFile,
1221                      LPCTSTR lpParameters,
1222                      LPCTSTR lpDirectory,
1223                      INT nShowCmd
1224                      )
1225 {
1226     HINSTANCE hRet = ShellExecute( hwnd,
1227         lpOperation,
1228         lpFile,
1229         lpParameters,
1230         lpDirectory,
1231         nShowCmd
1232         );
1233     int iRet = (int)hRet;
1234     switch(iRet)
1235     {
1236     case 0:
1237         WriteFormatted(g_fpLog,"The operating system is out of memory or resources.");
1238         break;
1239     case ERROR_FILE_NOT_FOUND:
1240         WriteFormatted(g_fpLog,"The specified file was not found.");
1241         break;
1242     case ERROR_PATH_NOT_FOUND:
1243         WriteFormatted(g_fpLog,"The specified file was not found.");
1244         break;
1245     case ERROR_BAD_FORMAT:
1246         WriteFormatted(g_fpLog,"The specified file was not found.");
1247         break;
1248     case SE_ERR_ACCESSDENIED:
1249         WriteFormatted(g_fpLog,"The specified file was not found.");
1250         break;
1251     case SE_ERR_ASSOCINCOMPLETE:
1252         WriteFormatted(g_fpLog,"The file name association is incomplete or invalid.");
1253         break;
1254     case SE_ERR_DDEBUSY:
1255         WriteFormatted(g_fpLog,"The Dynamic Data Exchange (DDE) transaction could not be completed because other DDE transactions were being processed.");
1256         break;
1257     case SE_ERR_DDEFAIL:
1258         WriteFormatted(g_fpLog,"The DDE transaction failed.");
1259         break;
1260     case SE_ERR_DDETIMEOUT:
1261         WriteFormatted(g_fpLog,"The DDE transaction could not be completed because the request timed out.");
1262         break;
1263     case SE_ERR_DLLNOTFOUND:
1264         WriteFormatted(g_fpLog,"The specified DLL was not found.");
1265         break;
1266     default:
1267 #ifdef FLAG_DEBUG
1268         //失败返回值会小于或等于32
1269         if (iRet>32)
1270         {
1271             WriteFormatted(g_fpLog,"\n-----[Function]:My_ShellExecute successfully:%d-----\n",iRet);
1272         }
1273         else
1274         {
1275             WriteFormatted(g_fpLog,"===================Other:%d=======================",iRet);
1276         }
1277 #endif //FLAG_DEBUG
1278
1279         break;
1280     }
1281 }
1282
1283
1284 void FileOptions()
1285 {
1286 #if 0
1287     printf("----------------------------------------------\n");
1288     printf("DeleteFile.exe (argv[1]) (argv[2]) (argv[3]) :\n");
1289     printf("argv[1]:绝对路径名(最后不加\\\n");
1290     printf("argv[2]:要查找or删除的文件类型\n");
1291     printf("argv[3]:标志:2--删除 其它--查看\n");
1292 #endif
1293
1294     char ch;
1295
1296     WriteFormatted(g_fpLog,STR_LOGO);
1297
1298     while(1)
1299     {
1300         InitGlobalVar();
1301 #ifdef FLAG_DEBUG
1302         DebugAndPrintAllGlobalVar();
1303 #endif
1304         //PRINTTOSCR(STR_HELP);
1305         //WriteFormatted(g_fpLog,STR_HELP);
1306         WriteFormatted(g_fpLog,"<%s>",g_pStrTime);
1307         flushall();
1308         fflush(stdin);
1309         ch = getch();
1310         WriteFormatted(g_fpLog,"%c\n",ch);
1311
1312         if ('y' == ch || 'Y' == ch)
1313         {
1314             ModifyCfg();
1315
1316             WriteFormatted(g_fpLog,"即将要查找%s下%s类型的文件了哦\n",g_strFindFilePath,g_strFindFileType);
1317
1318             Sleep(3000);
1319             VisitAllFiles(g_strFindFilePath);
1320
1321             //CloseOrFreeGlobalVar();
1322         }
1323         else if (ch =='q' || ch == 'e' || ch == 'Q' || ch == 'q')
1324         {
1325             int ret = MessageBoxA(NULL,"确定退出","确定退出 ",MB_YESNO);
1326             /*printf("--------------ret=%d-------------\n",ret);*/
1327             if (IDNO == ret)
1328             {
1329                 WriteFormatted(g_fpLog,"<你刚才选择了否,继续>\n");
1330                 continue;
1331             }
1332
1333             WriteFormatted(g_fpLog,"<你刚才选择了是,退出>\n");
1334             flushall();
1335             //CloseOrFreeGlobalVar();
1336             break;
1337         }
1338         else if (ch == 'm')
1339         {
1340             ModifyCfg();
1341         }
1342         else if ('d' == ch)
1343         {
1344             DebugAndPrintAllGlobalVar();
1345         }
1346         else if ('?' == ch)
1347         {
1348             WriteFormatted(g_fpLog,STR_HELP);
1349         }
1350         else if ('s' == ch)
1351         {
1352             SaveConfigToFile();
1353         }
1354         else if ('t' == ch)
1355         {
1356             WriteFormatted(g_fpLog,"\n太懒了,这个是用做测试用的,大哥\n");
1357             Sleep(2000);
1358             ReadConfigFromFile();
1359             VisitAllFiles(g_strFindFilePath);
1360         }
1361         else if ('c' == ch)
1362         {
1363             //清屏
1364             system("cls");
1365         }
1366         else
1367         {
1368             //空格与回车,继续
1369             if (ch == ' ' || ch == 13)
1370             {
1371                 continue;
1372             }
1373
1374             WriteFormatted(g_fpLog,"Input error,please check it.\n");
1375         }
1376
1377         //printf("<Enter to continue>\n");
1378     }
1379
1380     //退出时,打开文件
1381     My_ShellExecute(NULL, "edit","OutPut.txt" , NULL, NULL, SW_SHOWNORMAL|SW_SHOWMAXIMIZED);
1382     CloseOrFreeGlobalVar();
1383     /*getch();*/
1384 }

Windows API 实现查找、删除任意类型的文件_VERSION20120605(vc6.0调试通过)(2012.6.5最新修改)相关推荐

  1. 在SQL Server中保存和输出任意类型的文件

    我们可以把任意类型的文件保存到SQL Server中,在进行例子之前,先建立测试用表格,TestFile.sql: if exists (select * from dbo.sysobjects wh ...

  2. 启动服务器如何删除文件,在服务器启动时用Filter来删除某种类型的文件

    简介这篇文章主要介绍了在服务器启动时用Filter来删除某种类型的文件以及相关的经验技巧,文章约1303字,浏览量409,点赞数3,值得参考! 代码: import java.io.File; imp ...

  3. python的os库的remove可以删除只带有只读属性的_扩展库os中的方法remove()可以删除带有只读属性的文件。(2.0分)_学小易找答案...

    [填空题]已知 x = {1, 2, 3},那么执行语句 x.add(3) 之后,x的值为__.(2.6分) [填空题]假设已从标准库functools导入reduce()函数,那么表达式 reduc ...

  4. mfc mysql 选择删除文件_MFC应用实例:[60]删除指定类型的文件

    双击"删除文件"按钮添加响应函数OnDelete(),在响应函数OnDelete()中添加一下代码: void CDeleteCertainFolderFileDlg::OnDel ...

  5. 【C、C++、Windows API、 boost】多种方式判断文件、目录是否存在

    转载本文是为了实际之需要,方便查阅. 一.判断文件是否存在 #ifdef WIN32 #include <io.h> //C (Windows) access #else #include ...

  6. 生成任意内容任意类型的文件

    任何类型都可以,内容也是,excel虽然也可以生成,但它会把所有内容都放到第一个,所以还是老实用一般方法吧. 这个有点麻烦,我们一步步来(可以先拉到最后看一眼配置文件会比较好理解) 第一个类:调用方法 ...

  7. Windows API——FindFirstFile——查找文件

    1 //查找文件 2 HANDLE WINAPI FindFirstFile( 3 __in LPCTSTR lpFileName,//文件名或路径 4 __out LPWIN32_FIND_DATA ...

  8. java 获取光标_如何在java中使用Windows API获取当前鼠标光标类型?

    这在Win7上对我很有用.该脚本使用Python,但应该很容易翻译成任何其他语言.当然,它仅在相应的应用程序不使用自定义游标时才有效: from win32con import IDC_APPSTAR ...

  9. windows系统下如何删除带有两点的文件夹

    情况说明: 当文件夹末尾带点,或者带多个点的时候,删除文件夹会提示,文件夹不存在. 以下方式,以删除文件夹路径为 d:\osfipin\aaa.. 方式一: 执行以下命令行代码 md d:\osfip ...

最新文章

  1. 好好说一说室内定位技术
  2. int数组,找小于右边所有数,大于左边所有数的数
  3. smartform 打印预览时转PDF查看
  4. 我们都准备好进入数字货币+无现金世界了?
  5. Android集成三方浏览器之Crosswalk
  6. webpack — 概述 (2)
  7. ceph 存储 对比_分布式存储系统 Curve
  8. python爬取2019年计算机就业_2019年最新Python爬取腾讯招聘网信息代码解析
  9. WINDOWS下VIM配置
  10. 蚂蚁式管理(Style of Ant Management)
  11. 集成Android免费语音合成功能(在线、离线、离在线融合),有这一篇文章就够了(离线)
  12. 【转】CString 操作指南
  13. python中plt.hist_关于python中plt.hist参数的使用详解
  14. meterial design 5.0 新增控件介绍及使用方法
  15. matlab去除图片水印_怎么用matlab从含有水印的图像中提取出水印
  16. android热更新机制
  17. 安全测试涉及的测试对象有哪些?
  18. 采购(标准采购、标准收货) 库存(杂收、杂发) U9操作
  19. 深入浅出计算机组成原理20-面向流水线的指令设计(上):一心多用的现代CPU
  20. 小白如何用Python脚本玩转跳一跳

热门文章

  1. Uncaught SyntaxError: Unexpected token E in JSON at position 0 at JSON.parse (anonymous)
  2. Android SDK开发
  3. 蔬菜视觉分拣机器人的设计与实现(RoboWork参赛方案)
  4. SpringCloud微服务架构实战库存管理与分布式文件系统
  5. 一文讲透python类的继承原理,好比调制鸡尾酒
  6. Azure DevOps Server 的连接源(Artifacts):六、发布python程序(Twine pip)
  7. 阿里巴巴DBA成长之路
  8. 关于卫星互联网的最强入门科普
  9. moment typescript报错TS2307
  10. MySQL Navicat Premium 保存的SQL在哪里?