Subversion Repositories DevTools

Rev

Rev 2313 | Rev 2449 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2313 Rev 2354
Line 22... Line 22...
22
**                          (c) CopyFile
22
**                          (c) CopyFile
23
**                          (d) DeleteFile
23
**                          (d) DeleteFile
24
**                          (r) Remove Files (wildcard)
24
**                          (r) Remove Files (wildcard)
25
**                          (D) DeleteDir after deleting specified files (wildcard)
25
**                          (D) DeleteDir after deleting specified files (wildcard)
26
**                          (T) Delete Directory Trees
26
**                          (T) Delete Directory Trees
27
**                          (R) Delete Empty Directories
27
**                          (R) Remove Files and Empty Directories
28
**
28
**
29
**                      Example Usage
29
**                      Example Usage
30
**
30
**
31
**                          JatsFileUtil c9 'copyFile'    aaa/bbb/ccc/dddd/file build_test.pl +w
31
**                          JatsFileUtil c9 'copyFile'    aaa/bbb/ccc/dddd/file build_test.pl +w
32
**                          JatsFileUtil d9 'unCopyFile'  aaa/bbb/ccc/dddd/file
32
**                          JatsFileUtil d9 'unCopyFile'  aaa/bbb/ccc/dddd/file
33
**                          JatsFileUtil r9 'deleteFile'  a1 b2 c3
33
**                          JatsFileUtil r9 'deleteFile'  a1 b2 c3
34
**                          JatsFileUtil D9 'DeleteFiles' src/WIN32P.OBJ *.err *.pch '*'
34
**                          JatsFileUtil D9 'DeleteFiles' src/WIN32P.OBJ *.err *.pch '*'
35
**                          JatsFileUtil T9 'DeleteTree'  interface
35
**                          JatsFileUtil T9 'DeleteTree'  interface
36
**                          JatsFileUtil R9 'RmDir'       build
36
**                          JatsFileUtil R9 'RmItem'       build
37
**
37
**
38
**                      First two arguments are common to all
38
**                      First two arguments are common to all
39
**                          argv[1]     - Mode specifier, Debug specifier
39
**                          argv[1]     - Mode specifier, Debug specifier
40
**                          argv[2]     - Display Text
40
**                          argv[2]     - Display Text
41
**
41
**
Line 52... Line 52...
52
#define INVALID_FILE_ATTIBUTES ((DWORD) -1)
52
#define INVALID_FILE_ATTIBUTES ((DWORD) -1)
53
 
53
 
54
VOID ErrorExit (char *lpszMessage, LPTSTR lpszMessage2);
54
VOID ErrorExit (char *lpszMessage, LPTSTR lpszMessage2);
55
void createPaths ( _TCHAR *path );
55
void createPaths ( _TCHAR *path );
56
void DeleteDir( int argc, _TCHAR* argv[] );
56
void DeleteDir( int argc, _TCHAR* argv[] );
57
void DeleteDirTree( int argc, _TCHAR* argv[] );
-
 
58
_TCHAR * makePath( _TCHAR *base, _TCHAR *path);
57
_TCHAR * makePath( _TCHAR *base, _TCHAR *path);
59
int DeleteOneFile (_TCHAR * dst );
-
 
60
void DeleteOneDirectoryTree( _TCHAR* baseDir );
58
void DeleteOneDirectoryTree( _TCHAR* baseDir );
61
void copyOneFile( int argc, _TCHAR* argv[] );
59
void copyOneFile( int argc, _TCHAR* argv[] );
62
void RmDir( int argc, _TCHAR* argv[] );
60
void RmItem( int argc, _TCHAR* argv[] );
-
 
61
int DeleteOneFile (_TCHAR * dst );
-
 
62
void DeleteOneDirectory (_TCHAR *path );
-
 
63
void stdCheck( char *name, int argBad, _TCHAR *txt );
63
 
64
 
64
/*
65
/*
65
**  Global
66
**  Global
66
*/
67
*/
67
char  verbose = 1;                          /* Debugging aid */
68
char  verbose = 1;                          /* Debugging aid */
-
 
69
char  *helpText =
-
 
70
         "Usage: JatsFileUtil Mode Text Arguments\n"
-
 
71
         "\n"
-
 
72
         "  Where 'Mode' is two characters:\n"
-
 
73
         "      1 - Operation Specifier\n"
-
 
74
         "      2 - Debug Mode. 0..9\n"
-
 
75
         "  Where 'Text' is a, possibly empty, display string\n"
-
 
76
         "\n"
-
 
77
         "  By Example:\n"
-
 
78
         "      c9 copyFile     dstPath srcPath chmod\n"
-
 
79
         "      d9 unCopyFile   dstPath\n"
-
 
80
         "      r9 RmFile       file+\n"
-
 
81
         "      D9 DeleteFiles  dstDir file+ - supports (?*)\n"
-
 
82
         "      T9 DeleteTree   dstDir+\n"
-
 
83
         "      R9 RmItem       (dir|file)+\n";
68
 
84
 
69
/*----------------------------------------------------------------------------
85
/*----------------------------------------------------------------------------
70
** FUNCTION           : main
86
** FUNCTION           : main
71
**
87
**
72
** DESCRIPTION        : Main entry points
88
** DESCRIPTION        : Main entry points
Line 79... Line 95...
79
**
95
**
80
----------------------------------------------------------------------------*/
96
----------------------------------------------------------------------------*/
81
 
97
 
82
int _tmain(int argc, _TCHAR* argv[])
98
int _tmain(int argc, _TCHAR* argv[])
83
{
99
{
-
 
100
    _TCHAR * dst;
-
 
101
    int ii;
-
 
102
    
84
    /*
103
    /*
85
    **  User must provide some thing
104
    **  User must provide some thing
86
    */
105
    */
87
    if ( argc < 2 )
106
    if ( argc < 2 )
88
    {
107
    {
89
        ErrorExit("Insufficient arguments",L"");
108
       fprintf(stderr, helpText );
-
 
109
       return 1;
90
    }
110
    }
91
 
111
 
92
    /*
112
    /*
93
    **  Examine the first argument
113
    **  Examine the first argument
94
    **  Must be a character string of
114
    **  Must be a character string of
Line 102... Line 122...
102
        /*
122
        /*
103
        **  If Verbose, then display arguments
123
        **  If Verbose, then display arguments
104
        */
124
        */
105
        if ( verbose > 2 )
125
        if ( verbose > 2 )
106
        {
126
        {
107
            int ii;
-
 
108
            for ( ii = 0; ii < argc ; ii++ )
127
            for ( ii = 0; ii < argc ; ii++ )
109
            {
128
            {
110
                fprintf(stderr, "Arg%d: %ls:\n", ii, argv[ii] );
129
                fprintf(stderr, "Arg%d: %ls:\n", ii, argv[ii] );
111
            }
130
            }
112
            fflush(stderr) ;
131
            fflush(stderr) ;
Line 124... Line 143...
124
        **      argv[3] - target path
143
        **      argv[3] - target path
125
        **      argv[4] - Source path
144
        **      argv[4] - Source path
126
        **      argv[5] - File attributes
145
        **      argv[5] - File attributes
127
        */
146
        */
128
        case 'c':
147
        case 'c':
-
 
148
            stdCheck( "CopyFile", argc != 6, NULL );
-
 
149
            fprintf(stderr, "---- %ls %ls\n", argv[2], argv[3]);
-
 
150
            fflush(stderr) ;
129
            copyOneFile(argc, argv);
151
            copyOneFile(argc, argv);
130
            break;
152
            break;
131
 
153
 
132
        /*
154
        /*
133
        **  UnCopy a file
155
        **  UnCopy a file
134
        **      argv[2] - Text
156
        **      argv[2] - Text
135
        **      argv[3] - target path
157
        **      argv[3] - target path
136
        */
158
        */
137
        case 'd':
159
        case 'd':
138
            {
-
 
139
                _TCHAR * dst;
-
 
140
        
-
 
141
                if ( argc != 4 )
160
            stdCheck( "UnCopy", argc != 4, NULL );
142
                    ErrorExit("Incorrect argument count for mode",L"");
-
 
143
 
-
 
144
                /*
-
 
145
                **  Display user text
-
 
146
                */
-
 
147
                fprintf(stderr, "---- %ls %ls\n", argv[2], argv[3]);
161
            fprintf(stderr, "---- %ls %ls\n", argv[2], argv[3]);
148
                fflush(stderr) ;
162
            fflush(stderr) ;
149
 
163
 
150
                dst = makePath(argv[3], NULL);
164
            dst = makePath(argv[3], NULL);
151
                DeleteOneFile(dst);
165
            DeleteOneFile(dst);
152
                free (dst);
166
            free (dst);
153
            }
-
 
154
            break;
167
            break;
155
 
168
 
156
        /*
169
        /*
157
        **  Remove named files
170
        **  Remove named files
158
        **      argv[2]     - Text
171
        **      argv[2]     - Text
159
        **      argv[3]+    - target path
172
        **      argv[3]+    - target path
160
        */
173
        */
161
        case 'r':
174
        case 'r':
162
                {
-
 
163
                    int ii;
-
 
164
 
-
 
165
                    if ( argc < 4 )
-
 
166
                        ErrorExit("Incorrect argument count for mode",L"");
-
 
167
 
-
 
168
                    /*
-
 
169
                    **  Display user text
-
 
170
                    */
-
 
171
                    if ( argv[2][0] )
-
 
172
                    {
-
 
173
                        fprintf(stderr, "%s\n", argv[2]);
175
            stdCheck( "RmFile", argc <= 3, argv[2] );
174
                        fflush(stderr) ;
-
 
175
                    }
-
 
176
 
-
 
177
                    for ( ii = 3; ii < argc ; ii++ )
176
            for ( ii = 3; ii < argc ; ii++ )
178
                    {
177
            {
179
                        _TCHAR * dst = makePath(argv[ii], NULL);
178
                _TCHAR * dst = makePath(argv[ii], NULL);
180
                        DeleteOneFile(dst);
179
                DeleteOneFile(dst);
181
                        free (dst);
180
                free (dst);
182
                    }
-
 
183
                }
181
            }
184
            break;
182
            break;
185
 
183
 
186
        /*
184
        /*
187
        **  Delete files in directory - with wildcards
185
        **  Delete files in directory - with wildcards
188
        **      argv[2]     - Text
186
        **      argv[2]     - Text
189
        **      argv[3]     - Base directory
187
        **      argv[3]     - Base directory
190
        **      argv[4]+    - Files in dir to delete.
188
        **      argv[4]+    - Files in dir to delete.
191
        */
189
        */
192
        case 'D':
190
        case 'D':
-
 
191
            stdCheck( "DeleteDir", argc < 4, argv[2] );
193
            DeleteDir(argc - 2, argv + 2 );
192
            DeleteDir(argc, argv );
194
            break;
193
            break;
195
 
194
 
196
        /*
195
        /*
197
        **  Delete files recursively
196
        **  Delete files recursively
198
        **      argv[2] - Text
197
        **      argv[2] - Text
199
        **      argv[3]+  Base directory
198
        **      argv[3]+  Base directory
200
        */
199
        */
201
        case 'T':
200
        case 'T':
202
            DeleteDirTree(argc - 2, argv + 2 );
201
            stdCheck( "DeleteDirTree", argc < 3, argv[2] );
-
 
202
            for ( ii = 3; ii < argc ; ii++)
-
 
203
            {
-
 
204
                DeleteOneDirectoryTree( argv[ii] );
-
 
205
            }
203
            break;
206
            break;
204
 
207
 
205
        /*
208
        /*
206
        **  Delete Empty Directories
209
        **  Delete Empty Directories
207
        **      argv[2] - Text
210
        **      argv[2] - Text
208
        **      argv[3]+  Base directory
211
        **      argv[3]+  Base directory
209
        */
212
        */
210
        case 'R':
213
        case 'R':
-
 
214
            stdCheck( "RmItem", argc < 3, argv[2] );
211
            RmDir(argc - 2, argv + 2 );
215
            RmItem(argc, argv );
212
            break;
216
            break;
213
            
217
            
214
            
218
            
215
        default :
219
        default :
216
            ErrorExit("Unknown mode: ",argv[1]);
220
            ErrorExit("Unknown mode: ",argv[1]);
Line 218... Line 222...
218
    }
222
    }
219
    return 0;
223
    return 0;
220
}
224
}
221
 
225
 
222
/*----------------------------------------------------------------------------
226
/*----------------------------------------------------------------------------
-
 
227
** FUNCTION           : stdCheck
-
 
228
**
-
 
229
** DESCRIPTION        : Check arg count
-
 
230
**                      Print standard header
-
 
231
**
-
 
232
**
-
 
233
** INPUTS             : name        - Name of the operation
-
 
234
**                      argBad      - Arg count Not Ok
-
 
235
**                      text        - Text to print
-
 
236
**
-
 
237
** RETURNS            : Will not return on error
-
 
238
**
-
 
239
----------------------------------------------------------------------------*/
-
 
240
 
-
 
241
void stdCheck( char *name, int argBad, _TCHAR *txt )
-
 
242
{
-
 
243
    if ( argBad  )
-
 
244
    {
-
 
245
       fprintf(stderr, "JatsFileUtil:Incorrect argument count for %s\n", name);
-
 
246
       ErrorExit(NULL, NULL);
-
 
247
    }
-
 
248
 
-
 
249
    /*
-
 
250
    **  Display user text
-
 
251
    **      Suppress display if the message is empty
-
 
252
    */
-
 
253
    if ( txt && *txt )
-
 
254
    {
-
 
255
        fprintf(stderr, "%ls\n",txt);
-
 
256
        fflush(stderr) ;
-
 
257
    }
-
 
258
}
-
 
259
 
-
 
260
/*----------------------------------------------------------------------------
223
** FUNCTION           : createPaths
261
** FUNCTION           : createPaths
224
**
262
**
225
** DESCRIPTION        : Create the path to the target
263
** DESCRIPTION        : Create the path to the target
226
**
264
**
227
**
265
**
Line 276... Line 314...
276
{
314
{
277
	DWORD rv;
315
	DWORD rv;
278
    _TCHAR * src;
316
    _TCHAR * src;
279
    _TCHAR * dst;
317
    _TCHAR * dst;
280
    
318
    
281
    if ( argc != 6 )
-
 
282
        ErrorExit("Incorrect argument count for file copy",L"");
-
 
283
    
-
 
284
    /*
-
 
285
    **  Display user text
-
 
286
    */
-
 
287
    fprintf(stderr, "---- %ls %ls\n", argv[2], argv[3]);
-
 
288
    fflush(stderr) ;
-
 
289
 
-
 
290
    dst = makePath(argv[3], NULL);
319
    dst = makePath(argv[3], NULL);
291
    src = makePath(argv[4], NULL);
320
    src = makePath(argv[4], NULL);
292
 
321
 
293
    /*
322
    /*
294
    **   Check that the source is a file
323
    **   Check that the source is a file
295
    */
324
    */
296
    if ( verbose )
325
    if ( verbose > 2)
297
        fprintf(stderr, "Validate Source File: %ls\n", src);
326
        fprintf(stderr, "Validate Source File: %ls\n", src);
298
        
327
        
299
    rv = GetFileAttributesW( src );
328
    rv = GetFileAttributesW( src );
300
    if ( rv == INVALID_FILE_ATTIBUTES )
329
    if ( rv == INVALID_FILE_ATTIBUTES )
301
    {
330
    {
Line 303... Line 332...
303
        fprintf(stderr, "Source: %ls\n", src);
332
        fprintf(stderr, "Source: %ls\n", src);
304
        ErrorExit("Source File not found: ", argv[4]);
333
        ErrorExit("Source File not found: ", argv[4]);
305
    }
334
    }
306
 
335
 
307
    /*
336
    /*
308
    **  Remove the ReadOnly attribute on the dest file
337
    **  Delete the destination file before the copy
-
 
338
    **  Will force it to be writable
309
    */
339
    */
310
    DeleteOneFile(dst);
340
    DeleteOneFile(dst);
311
 
341
 
312
    /*
342
    /*
313
    **  Create directories
343
    **  Create directories
Line 327... Line 357...
327
    }
357
    }
328
 
358
 
329
    /*
359
    /*
330
    **  Test for files existence
360
    **  Test for files existence
331
    */
361
    */
332
    if ( verbose )
362
    if ( verbose > 1 )
333
        fprintf(stderr, "Test target was copied: %ls\n", dst);
363
        fprintf(stderr, "Test target was copied: %ls\n", dst);
334
 
364
 
335
    rv = GetFileAttributesW( dst );
365
    rv = GetFileAttributesW( dst );
336
    if ( rv == INVALID_FILE_ATTIBUTES )
366
    if ( rv == INVALID_FILE_ATTIBUTES )
337
    {
367
    {
Line 368... Line 398...
368
**                      Then delete the directory - if its empty
398
**                      Then delete the directory - if its empty
369
**
399
**
370
**
400
**
371
** INPUTS             : argc    - count of args
401
** INPUTS             : argc    - count of args
372
**                      argv    - list of files to delete
402
**                      argv    - list of files to delete
373
**                                [0]:  Text to display
-
 
374
**                                [1]:  Base directory
403
**                                [3]:  Base directory
375
**                                [2]+  Files in dir to delete
404
**                                [4]+  Files in dir to delete
376
**
405
**
377
** RETURNS            : Will not return on error
406
** RETURNS            : Will not return on error
378
**
407
**
379
----------------------------------------------------------------------------*/
408
----------------------------------------------------------------------------*/
380
 
409
 
Line 384... Line 413...
384
    _TCHAR* baseDir;
413
    _TCHAR* baseDir;
385
    WIN32_FIND_DATA FindFileData;
414
    WIN32_FIND_DATA FindFileData;
386
    HANDLE hFind;
415
    HANDLE hFind;
387
    _TCHAR *target;
416
    _TCHAR *target;
388
    _TCHAR *dirPath;
417
    _TCHAR *dirPath;
389
 
-
 
390
    if ( argc < 3 )
-
 
391
        ErrorExit("Insufficient arguments for DeleteDir",L"");
-
 
392
        
418
    int ii;
393
    /*
-
 
394
    **  Display the user message
-
 
395
    **      Suppress display if the message is empty
-
 
396
    */
-
 
397
    if ( argv[0][0] )
-
 
398
    {
-
 
399
        fprintf(stderr, "%ls\n", argv[0]);
-
 
400
        fflush(stderr) ;
-
 
401
    }
-
 
402
 
419
 
403
    /*
420
    /*
404
    **  Extract the base directory from the argument list
421
    **  Extract the base directory from the argument list
405
    **  This must be a directory
422
    **  This must be a directory
406
    */
423
    */
407
    baseDir = argv[1];
424
    baseDir = argv[3];
408
    argc -= 2;
-
 
409
    argv+=2;
-
 
410
 
425
 
411
    /*
426
    /*
412
    **  Ensure that the base directory exists
427
    **  Ensure that the base directory exists
413
    */
428
    */
414
    dirPath = makePath(baseDir, NULL);
429
    dirPath = makePath(baseDir, NULL);
Line 439... Line 454...
439
 
454
 
440
    /*
455
    /*
441
    **  Process all the suffixes
456
    **  Process all the suffixes
442
    **  They may contain a wild card
457
    **  They may contain a wild card
443
    */
458
    */
444
    for ( ; argc ; argc--, argv++ )
459
    for ( ii = 4; ii < argc ; ii++)
445
    {
460
    {
446
        _TCHAR * thisDir = makePath( baseDir, *argv);
461
        _TCHAR * thisDir = makePath( baseDir, argv[ii]);
447
        hFind = FindFirstFile(thisDir, &FindFileData);
462
        hFind = FindFirstFile(thisDir, &FindFileData);
448
        free(thisDir);
463
        free(thisDir);
449
        
464
        
450
        if (hFind == INVALID_HANDLE_VALUE)
465
        if (hFind == INVALID_HANDLE_VALUE)
451
        {
466
        {
452
            /*
467
            /*
453
            **  No match
468
            **  No match
454
            */
469
            */
455
            if ( verbose > 1 )
470
            if ( verbose > 1 )
456
                fprintf(stderr, "No Match: %ls, %ls\n", baseDir, *argv);
471
                fprintf(stderr, "No Match: %ls, %ls\n", baseDir, argv[ii]);
457
            continue;
472
            continue;
458
        }
473
        }
459
 
474
 
460
        /*
475
        /*
461
        **  Iterate over all the files
476
        **  Iterate over all the files
Line 465... Line 480...
465
                continue;
480
                continue;
466
 
481
 
467
            if ( _tcscmp( TEXT(".."), FindFileData.cFileName ) == 0 )
482
            if ( _tcscmp( TEXT(".."), FindFileData.cFileName ) == 0 )
468
                continue;
483
                continue;
469
 
484
 
470
            if ( verbose )
485
            if ( verbose > 1)
471
                fprintf(stderr, "Match: %ls\n", FindFileData.cFileName);
486
                fprintf(stderr, "Match: %ls\n", FindFileData.cFileName);
472
 
487
 
473
            /*
488
            /*
474
            **  Matching file found
489
            **  Matching file found
475
            */
490
            */
Line 485... Line 500...
485
    **  Finally delete the directory
500
    **  Finally delete the directory
486
    **      Unless its '.'
501
    **      Unless its '.'
487
    */
502
    */
488
    if ( _tcscmp( TEXT("."),baseDir) != 0 )
503
    if ( _tcscmp( TEXT("."),baseDir) != 0 )
489
    {
504
    {
490
        if ( verbose > 1 )
-
 
491
            fprintf(stderr, "Delete Directory: %ls\n", baseDir);
-
 
492
 
-
 
493
        if ( ! RemoveDirectory( dirPath ) )
505
        DeleteOneDirectory(baseDir);
494
        {
-
 
495
            if ( verbose )
-
 
496
                fprintf(stderr, "Directory not deleted: %ls\n", baseDir);
-
 
497
        }
-
 
498
    }
506
    }
499
    free(dirPath);
507
    free(dirPath);
500
}
508
}
501
 
509
 
502
/*----------------------------------------------------------------------------
510
/*----------------------------------------------------------------------------
503
** FUNCTION           : DeleteDirTree
-
 
504
**
-
 
505
** DESCRIPTION        : Delete an entire directory tree(s)
-
 
506
**
-
 
507
** INPUTS             : argc    - count of args
-
 
508
**                      argv    - list of files to delete
-
 
509
**                                [0]:  Text to display
-
 
510
**                                [1]+  Base directory
-
 
511
**
-
 
512
** RETURNS            : Will not return on error
-
 
513
**
-
 
514
----------------------------------------------------------------------------*/
-
 
515
 
-
 
516
void DeleteDirTree( int argc, _TCHAR* argv[] )
-
 
517
{
-
 
518
    int ii;
-
 
519
 
-
 
520
    if ( argc < 2 )
-
 
521
        ErrorExit("Insufficient arguments for DeleteDirTree",L"");
-
 
522
        
-
 
523
    /*
-
 
524
    **  Display the user message
-
 
525
    **      Suppress display if the message is empty
-
 
526
    */
-
 
527
    if ( argv[0][0] )
-
 
528
    {
-
 
529
        fprintf(stderr, "%ls\n", argv[0]);
-
 
530
        fflush(stderr) ;
-
 
531
    }
-
 
532
 
-
 
533
    for ( ii = 1; ii < argc ; ii++)
-
 
534
    {
-
 
535
        DeleteOneDirectoryTree( argv[ii] );
-
 
536
    }
-
 
537
}
-
 
538
 
-
 
539
/*----------------------------------------------------------------------------
-
 
540
** FUNCTION           : DeleteOneDirectoryTree
511
** FUNCTION           : DeleteOneDirectoryTree
541
**
512
**
542
** DESCRIPTION        : Delete an entire directory tree(s)
513
** DESCRIPTION        : Delete an entire directory tree(s)
-
 
514
**                      Force it writable and searchable before deletion
-
 
515
**                      Don't follow symbolic links - just delete them
543
**
516
**
544
** INPUTS             : path    - Dir to delete
517
** INPUTS             : path    - Dir to delete
545
**
518
**
546
** RETURNS            : Will not return on error
519
** RETURNS            : Will not return on error
547
**
520
**
Line 635... Line 608...
635
        FindClose(hFind);
608
        FindClose(hFind);
636
    }
609
    }
637
 
610
 
638
    /*
611
    /*
639
    **  Finally delete the directory
612
    **  Finally delete the directory
-
 
613
    **  It should now be empty
640
    */
614
    */
641
    if ( verbose > 1 )
-
 
642
        fprintf(stderr, "Delete Directory: %ls\n", baseDir);
615
    DeleteOneDirectory(baseDir);
643
 
616
 
-
 
617
    free(dirPath);
-
 
618
}
-
 
619
 
-
 
620
/*----------------------------------------------------------------------------
-
 
621
** FUNCTION           : RmItem
-
 
622
**
-
 
623
** DESCRIPTION        : Remove Empty directories and files
-
 
624
**
-
 
625
** INPUTS             : argc    - count of args
-
 
626
**                      argv    - list of files to delete
-
 
627
**                                [3]+  Base directory
-
 
628
**
-
 
629
** RETURNS            : Will not return on error
-
 
630
**
-
 
631
----------------------------------------------------------------------------*/
-
 
632
 
-
 
633
 
-
 
634
void RmItem( int argc, _TCHAR* argv[] )
-
 
635
{
-
 
636
    int ii;
-
 
637
    _TCHAR *target;
-
 
638
    DWORD rv;
-
 
639
 
644
    if ( ! RemoveDirectory( dirPath ) )
640
    for ( ii = 3; ii < argc ; ii++)
645
    {
641
    {
-
 
642
 
646
        if ( verbose )
643
        if ( verbose > 2)
647
            fprintf(stderr, "Directory not deleted: %ls\n", baseDir);
644
            fprintf(stderr, "RmItem: %s\n", argv[ii]);
-
 
645
 
-
 
646
        target = makePath( argv[ii],NULL);
-
 
647
        rv = GetFileAttributesW( target );
-
 
648
        if ( rv != INVALID_FILE_ATTIBUTES )
-
 
649
        {
-
 
650
            if ( rv  & FILE_ATTRIBUTE_DIRECTORY)
-
 
651
            {
-
 
652
                DeleteOneDirectory( target );
-
 
653
            }
-
 
654
            else
-
 
655
            {
-
 
656
                DeleteOneFile (target);
-
 
657
            }
-
 
658
        }
-
 
659
        free(target);
648
    }
660
    }
649
    free(dirPath);
-
 
650
}
661
}
651
 
662
 
652
/*----------------------------------------------------------------------------
663
/*----------------------------------------------------------------------------
653
** FUNCTION           : DeleteOneFile
664
** FUNCTION           : DeleteOneFile
654
**
665
**
655
** DESCRIPTION        : Delete a file
666
** DESCRIPTION        : Delete a file
-
 
667
**                      Low level deletion operation to be used by other functions
656
**                      Force it writable before deletion
668
**                      Force it writable before deletion
-
 
669
**                      Don't follow symbolic links - just delete them
657
**
670
**
658
** INPUTS             : path        - path to the file
671
** INPUTS             : path        - path to the file
659
**
672
**
660
** RETURNS            : 0           - OK (file deleted or not present)
673
** RETURNS            : 0           - OK (file deleted or not present)
661
**
674
**
Line 693... Line 706...
693
 
706
 
694
    return result;
707
    return result;
695
}
708
}
696
 
709
 
697
/*----------------------------------------------------------------------------
710
/*----------------------------------------------------------------------------
698
** FUNCTION           : RmDir
711
** FUNCTION           : DeleteOneDirectory
699
**
712
**
700
** DESCRIPTION        : Remove Empty directories
713
** DESCRIPTION        : Low level function to delete a directory
-
 
714
**                      Assumes that checks have been performed
-
 
715
**                          It is a directory
-
 
716
**                          It does exist
701
**
717
**
702
** INPUTS             : argc    - count of args
-
 
703
**                      argv    - list of files to delete
-
 
704
**                                [0]:  Text to display
-
 
705
**                                [1]+  Base directory
-
 
706
**
718
**
-
 
719
** INPUTS             : path            - Target Path
-
 
720
**
707
** RETURNS            : Will not return on error
721
** RETURNS            : Nothing
708
**
722
**
709
----------------------------------------------------------------------------*/
723
----------------------------------------------------------------------------*/
710
 
724
 
711
 
-
 
712
void RmDir( int argc, _TCHAR* argv[] )
725
void DeleteOneDirectory (_TCHAR *path )
713
{
726
{
714
    int ii;
-
 
715
    _TCHAR *target;
-
 
716
 
-
 
717
    if ( argc < 2 )
727
    if ( verbose )
718
        ErrorExit("Insufficient arguments for RmDir",L"");
-
 
719
        
-
 
720
    /*
-
 
721
    **  Display the user message
-
 
722
    **      Suppress display if the message is empty
-
 
723
    */
-
 
724
    if ( argv[0][0] )
-
 
725
    {
-
 
726
        fprintf(stderr, "%ls\n", argv[0]);
728
        fprintf(stderr, "Delete Directory: %ls\n", path);
727
        fflush(stderr) ;
-
 
728
    }
-
 
729
 
729
 
730
    for ( ii = 1; ii < argc ; ii++)
730
    if ( ! RemoveDirectory(path))
731
    {
731
    {
732
        target = makePath( argv[ii],NULL);
-
 
733
        if ( verbose > 2)
732
        if ( verbose )
734
            fprintf(stderr, "RmDir: %ls\n", target);
-
 
735
        
-
 
736
        if ( ! RemoveDirectory (target) )
-
 
737
        {
-
 
738
            if ( verbose )
-
 
739
                fprintf(stderr, "Directory not deleted: %ls\n", argv[ii]);
733
            fprintf(stderr, "Directory not deleted: %ls\n", path);
740
        }
-
 
741
        free(target);
-
 
742
    }
734
    }
743
}
735
}
744
 
736
 
745
/*----------------------------------------------------------------------------
737
/*----------------------------------------------------------------------------
746
** FUNCTION           : makePath
738
** FUNCTION           : makePath
Line 876... Line 868...
876
        {
868
        {
877
            *udst++;
869
            *udst++;
878
        }
870
        }
879
    }
871
    }
880
 
872
 
881
    if ( verbose > 2)
873
    if ( verbose > 5)
882
        fprintf(stderr, "makePath: %ls\n", data);
874
        fprintf(stderr, "makePath: %ls\n", data);
883
 
875
 
884
    return data;
876
    return data;
885
}
877
}
886
 
878
 
Line 897... Line 889...
897
**                      Will exit with bad code
889
**                      Will exit with bad code
898
**
890
**
899
----------------------------------------------------------------------------*/
891
----------------------------------------------------------------------------*/
900
 
892
 
901
VOID ErrorExit (char *lpszMessage, LPTSTR lpszMessage2)
893
VOID ErrorExit (char *lpszMessage, LPTSTR lpszMessage2)
902
{ 
894
{
-
 
895
    if ( lpszMessage  )
-
 
896
    {
903
   fprintf(stderr, "JatsFileUtil:Error:%s%ls\n", lpszMessage,lpszMessage2);
897
        fprintf(stderr, "JatsFileUtil:Error:%s%ls\n", lpszMessage,lpszMessage2);
-
 
898
    }
904
   fflush(stderr) ;
899
   fflush(stderr) ;
905
   ExitProcess(-1);
900
   ExitProcess(-1);
906
} 
901
} 
907
 
902