home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / OS2DEL.ZIP / DD.C next >
C/C++ Source or Header  |  1989-11-26  |  8KB  |  264 lines

  1.  
  2. /* ATKINSON - HOME COMPUTER - 414-543-8929 */
  3.  
  4. /* CL /AC /G2 KD.C */
  5.  
  6. #define  INCL_DOS
  7. #define  INCL_VIO
  8. #include <os2.h>
  9. #include <ctype.h>
  10.  
  11.         typedef char file_names[(100)+1];
  12.         typedef file_names inter_arr1[50];
  13.         static  inter_arr1 dir_array; 
  14.         typedef file_names inter_arr2[600];
  15.         static  inter_arr2 file_array;
  16.     PSZ     pszPath;
  17.     PSZ     start_dir;
  18.     PSZ     pszCommandLine;
  19.     PSZ     change_path;
  20.     USHORT  cbPath;
  21.     USHORT  usDisk;
  22.     USHORT  cbWritten;
  23.     USHORT  offCommand;
  24.     USHORT  i;
  25.     USHORT  cch;
  26.     USHORT  drive;
  27.     USHORT  status;
  28.     USHORT  dir_index;
  29.     USHORT  dir_maximum;
  30.     USHORT  file_index;
  31.     USHORT  file_maximum;
  32.     ULONG   ulDrives;
  33.     SEL     selPath;
  34.     SEL     sel_start_dir;
  35.     SEL     selEnvironment;
  36.         SEL     sel_change_path;
  37.           
  38. main()
  39. {
  40.     VioWrtTTY("\r\nDD - Delete Directory - ver 1.00 - OS/2\r\n",43,0);
  41.     get_environment();
  42.     get_start_dir();
  43.     if ((change_path[0] != start_dir[0]) || 
  44.             (change_path[1] != start_dir[1]) ||
  45.             (change_path[2] != start_dir[2]))
  46.           {
  47.           usage();
  48.           DosExit(EXIT_THREAD,0);
  49.           }          
  50.         status = DosChDir(change_path,0L);
  51.         if (status != 0)
  52.           {
  53.           VioWrtTTY("\r\nDirectory inaccessible\r\n",26,0);
  54.           DosExit(EXIT_THREAD,0);
  55.           }    
  56.         delete_dir(change_path);  
  57.         status = DosChDir(start_dir,0L);
  58.     DosExit(EXIT_THREAD,0);
  59. }
  60.  
  61. delete_dir(dir_to_delete)
  62. PSZ dir_to_delete[100];
  63. {
  64.         PSZ delete_dir3;
  65.         SEL sel_delete_dir3;
  66.         PSZ delete_dir4;
  67.         SEL sel_delete_dir4;
  68.         USHORT find_result;
  69.         HDIR hdir = 0xFFFF;
  70.         FILEFINDBUF findbuf;
  71.         USHORT usSearchCount = 1;
  72.     strcpy(dir_array[(1)], dir_to_delete);
  73.     dir_index = 2;
  74.         find_result = DosFindFirst("*.*",&hdir,FILE_DIRECTORY,&findbuf,
  75.                       sizeof(findbuf), &usSearchCount,0L);   
  76.         while (find_result == 0)
  77.           {
  78.           if ((findbuf.attrFile & 0x10) == 0x10)
  79.            {
  80.            if (findbuf.achName[0] == '.')
  81.              {
  82.              }
  83.            else   
  84.              {
  85.              strcpy(dir_array[(dir_index)],dir_to_delete);
  86.              strcat(dir_array[(dir_index)],"\\");
  87.              strcat(dir_array[(dir_index)],findbuf.achName);             
  88.              dir_index++;
  89.              }
  90.            }   
  91.           find_result = DosFindNext(hdir,&findbuf,sizeof(findbuf),&usSearchCount);
  92.           }
  93.         dir_maximum = dir_index - 1;
  94.         dir_index = 1;    
  95.         do
  96.           {
  97.           delete_files(dir_array[(dir_index)]);
  98.           dir_index++;
  99.           }
  100.         while (!(dir_index >= dir_maximum));
  101.       status = DosChDir(start_dir, 0L);  
  102.       for (dir_index = dir_maximum; dir_index >= 1; --dir_index)
  103.         {
  104.         VioWrtTTY("\r\n",2,0);            
  105.         VioWrtTTY(dir_array[(dir_index)],strlen(dir_array[(dir_index)]),0);
  106.         cbPath = 0;
  107.         DosQCurDisk(&usDisk, &ulDrives);
  108.         DosQCurDir(usDisk, NULL, &cbPath);
  109.         DosAllocSeg(cbPath, &sel_delete_dir3, SEG_NONSHARED);
  110.         delete_dir3 = MAKEP(sel_delete_dir3, 0);
  111.         DosAllocSeg(100, &sel_delete_dir4, SEG_NONSHARED);
  112.         delete_dir4 = MAKEP(sel_delete_dir4, 0);
  113.         DosQCurDir(usDisk, delete_dir3, &cbPath);
  114.         usDisk =  toascii(usDisk+64);
  115.         delete_dir4[0] = usDisk;
  116.         delete_dir4[1] = '\0';
  117.         strcat(delete_dir4,":\\");
  118.         strcat(delete_dir4, delete_dir3);
  119.         status = DosRmDir(dir_array[(dir_index)],0L);
  120.         }
  121.       VioWrtTTY("\r\n",2,0);  
  122.       dir_index++;
  123. }
  124.  
  125. delete_files(file_name)
  126. file_names file_name; 
  127. {
  128.     HDIR hdir = 0xFFFF;
  129.     FILEFINDBUF findbuf;
  130.     USHORT usSearchCount = 1;
  131.     USHORT find_result;
  132.     PSZ delete_dir1;
  133.     SEL sel_delete_dir1;
  134.     PSZ delete_dir2;
  135.     SEL sel_delete_dir2;
  136.  
  137.     status = DosChDir(start_dir,0L);
  138.     status = DosChDir(file_name,0L);
  139.     cbPath = 0;
  140.     DosQCurDisk(&usDisk, &ulDrives);
  141.     DosQCurDir(usDisk, NULL, &cbPath);
  142.     DosAllocSeg(cbPath, &sel_delete_dir1, SEG_NONSHARED);
  143.     delete_dir1 = MAKEP(sel_delete_dir1, 0);
  144.     DosAllocSeg(100, &sel_delete_dir2, SEG_NONSHARED);
  145.     delete_dir2 = MAKEP(sel_delete_dir2, 0);
  146.     DosQCurDir(usDisk, delete_dir1, &cbPath);
  147.     usDisk =  toascii(usDisk+64);
  148.     delete_dir2[0] = usDisk;
  149.     delete_dir2[1] = '\0';
  150.     strcat(delete_dir2,":\\");
  151.     strcat(delete_dir2, delete_dir1);
  152.  
  153.     file_maximum = 0;
  154.     file_index = 1;
  155.     find_result = DosFindFirst("*.*",&hdir,FILE_ARCHIVED | FILE_DIRECTORY,
  156.                  &findbuf, sizeof(findbuf), &usSearchCount,0L);
  157.     while (find_result == 0)
  158.           {
  159.           if (findbuf.achName[0] != '.')
  160.             {
  161.             strcpy(file_array[(file_index)],delete_dir2);
  162.             strcat(file_array[(file_index)],"\\");
  163.             strcat(file_array[(file_index)],findbuf.achName);
  164.             file_index++;
  165.             }
  166.           if (file_index > 599)
  167.             {
  168.             VioWrtTTY("\r\nDelete file limit exceeded...",31,0);
  169.             DosExit(EXIT_THREAD,0);
  170.             }    
  171.        find_result = DosFindNext(hdir,&findbuf,sizeof(findbuf),&usSearchCount);
  172.           }  
  173.     file_maximum = file_index - 1;
  174.     if (file_maximum > 0)
  175.       {
  176.           for (file_index = 1; file_index <= file_maximum; ++file_index)
  177.             {
  178.             delete_file(file_array[(file_index)]);
  179.             }
  180.           file_index--;   
  181.           }
  182. }
  183.  
  184. delete_file(file_name)
  185. file_names file_name; 
  186. {
  187.     if (strlen(file_name) > 100)
  188.           {
  189.           VioWrtTTY("\r\nPath too long error",21,0);
  190.           DosExit(EXIT_THREAD,0);
  191.           }    
  192.         if ((file_name == ".") || (file_name == ".."))
  193.           {
  194.           }
  195.         else         
  196.           {
  197.           status = DosSetFileMode(file_name,FILE_ARCHIVED,0L);
  198.           status = DosDelete(file_name,0L);
  199.           if (status != 0)
  200.             {
  201.             dir_maximum++;
  202.             strcpy(dir_array[(dir_maximum)],file_name);
  203.             if (dir_maximum > 49)
  204.               {
  205.               VioWrtTTY("\r\nDelete dir limit exceeded...",30,0);
  206.               DosExit(EXIT_THREAD,0);
  207.               }
  208.             return;
  209.             }
  210.         }   
  211.     VioWrtTTY("\r\n",2,0);
  212.     VioWrtTTY(file_name,strlen(file_name),0);
  213. }
  214.     
  215. get_start_dir()
  216. {
  217.     cbPath = 0;
  218.     DosQCurDisk(&usDisk, &ulDrives);
  219.     DosQCurDir(usDisk, NULL, &cbPath);
  220.     DosAllocSeg(cbPath, &selPath, SEG_NONSHARED);
  221.     pszPath = MAKEP(selPath, 0);
  222.     DosAllocSeg(100, &sel_start_dir, SEG_NONSHARED);
  223.     start_dir = MAKEP(sel_start_dir, 0);
  224.     DosQCurDir(usDisk, pszPath, &cbPath);
  225.         usDisk =  toascii(usDisk+64);
  226.         start_dir[0] = usDisk;
  227.         start_dir[1] = '\0';
  228.     strcat(start_dir,":\\");
  229.     strcat(start_dir, pszPath);
  230. }
  231.  
  232. get_environment()
  233. {
  234.     USHORT i2;
  235.     DosGetEnv(&selEnvironment, &offCommand);
  236.     pszCommandLine = MAKEP(selEnvironment, offCommand);
  237.     for (i = 0; pszCommandLine[i]; i++);
  238.         DosAllocSeg(100, &sel_change_path, SEG_NONSHARED);
  239.         change_path = MAKEP(sel_change_path, 0);
  240.     for (i++, cch = 0; pszCommandLine[cch + i]; cch++);
  241.     i++;
  242.     for (i2 = 0; i2 < cch; i2++)
  243.       {
  244.       change_path[i2] = pszCommandLine[i + i2];
  245.       }
  246.     strupr(change_path);  
  247. }    
  248.  
  249. usage()
  250. {
  251.     VioWrtTTY("\r\nUsage:",8,0);
  252.     VioWrtTTY("\r\n",2,0);
  253.     VioWrtTTY("\r\nDD drive:\\path",16,0);
  254.     VioWrtTTY("\r\n",2,0);
  255.     VioWrtTTY("\r\nEx: DD C:\\SOURCE\\FOO",24,0);
  256.     VioWrtTTY("\r\n",2,0);
  257.     VioWrtTTY("\r\nEx: DD C:\\FOO",16,0);
  258.     VioWrtTTY("\r\n",2,0);
  259.     VioWrtTTY("\r\nDesired DIR to delete *must* be on current DISK",49,0);
  260.     VioWrtTTY("\r\n",2,0);
  261.     VioWrtTTY("\r\nFull DRIVE:\\PATH *must* be given",35,0);
  262.     VioWrtTTY("\r\n",2,0);
  263. }
  264.