Saved Bookmarks
| 1. |
Solve : Nested ifs and fried brain? |
|
Answer» I think I should know how to do this but I have forgotten too much of the nuances of the command prompt. I'm trying to dream up a condition statement for a host of possibilities, like deleting certain files but only those files. The first thought I had was along the lines of IF EXIST 000?.* DEL 000?.* ELSE - And that is where I am falling down on the job. To KEEP the example simple let's SUPPOSED an ascending list of in numeric order. So after the ELSE I would like to have it CHECK the next higher group as in IF EXIST 001?.* DEL 001?.* ELSE . . . . Basically I am wanting to build a really ugly next or compound IF statement. Now since this is supposed to happen in ascending or descending order it should be simple to just change the numerical condition if but I am stuck on this complex IF statement. I got out of the computer business about the time "scripts" were taking the PLACE of "Batch" files so Robo-powershell-whatever-it-is-called isn't really an option for me. I'd appreciate any suggestions. Some details. How many files? Why do you need to remove them? Would it not be better to send older files to a archive folder? Why sound sending order make any difference? Are you using IF EXIST before DEL filemask purely so that you don't get a 'file not found' error message if nothing matches filemask? Or is it important for some other reason to know if filemask exists? Will this affect what you do (or don't do) next? Because if your logic is like this: If any files matching filemask1 exist, delete them if not... If any files matching filemask2 exist, delete them if not... If any files matching filemask3 exist, delete them (etc) ... then you don't really care about the 'if nots', do you? Basically I'm asking, why do you need IF EXIST or ELSE at all? The folder in question has a few thousand graphic files and a program sequentially goes the folder and displays the next graphic in the list for a few minutes and goes to the next one. Unfortunately the program isn't bright enough to keep track of where it is in the list and after every restart, it starts with the first file. The files are all copies of files in a folder on a different drive so it is easy to replenish the folder "feeding" the program. For right now I attempt to remember to check that folder on a restart and delete out what seems like would be a reasonable number from the beginning. That way I don't have the same hundred files pictured over and over again. So I am looking for a way to delete some number of files, maybe a hundred, on a restart but without having to remember to do so. For example if file 0100 exists then I could delete 01?? but then I want it to stop. I don't want it to check for 0200 because I would have just deleted files 0100 through 0199. If after running the program for a while, deleting 100 files might seem too high or too low, then I could adjust the batch file IF statements accordingly. On the first running of the batch file it should FIND and delete the first hundred file and break out of the if statement. On the second running of the program it would find no matching files of the first hundred and skip to the next conditional statement; which would prompt about the next hundred and so on. By the time you have a thousand files deleted, the program would need to past through ten if statements which would all be not true but the eleventh one would be true. This is just an exercise in mental gymnastics to keep the brain from freezing up, it is not serious. But it has turned out to be tougher than I anticipated. I gave some consideration to reversing the logic to not exist but that didn't seem to make the task any easier. Quote from: rmmccoy On the first running of the batch file it should find and delete the first hundred file and break out of the if statement. On the second running of the program it would find no matching files of the first hundred and skip to the next conditional statement; which would prompt about the next hundred and so on. By the time you have a thousand files deleted, the program would need to past through ten if statements which would all be not true but the eleventh one would be true.This is what I did: 1. Create 1000 test files (I made .txt files but the extension is immaterial, you can change this) in blocks of 100 named 0000.txt to 0099.txt, 0100.txt to 0199.txt ... ending 0900.txt to 0999.txt 2. Each time it is run it deletes the first block in the sequence that exists, then quits. 3. If, on running, there are no files to delete, it informs the user and quits. If you run batches by double clicking in Explorer, (i.e. not in a console window) you may want to put a PAUSE command as the last line. @echo off IF EXIST 00??.txt DEL 00??.txt & goto done IF EXIST 01??.txt DEL 01??.txt & goto done IF EXIST 02??.txt DEL 02??.txt & goto done IF EXIST 03??.txt DEL 03??.txt & goto done IF EXIST 04??.txt DEL 04??.txt & goto done IF EXIST 05??.txt DEL 05??.txt & goto done IF EXIST 06??.txt DEL 06??.txt & goto done IF EXIST 07??.txt DEL 07??.txt & goto done IF EXIST 07??.txt DEL 07??.txt & goto done IF EXIST 08??.txt DEL 08??.txt & goto done IF EXIST 09??.txt DEL 09??.txt & goto done echo No files left! :done echo Finished The same effect is produced by this: @echo off FOR /L %%N IN (0,1,9) DO IF EXIST 0%%N??.txt DEL 0%%N??.txt & GOTO done ECHO No files left! :done ECHO Finished You can have multline IF statements using brackets (parentheses) so that this: IF EXIST 00??.txt DEL 00??.txt & goto done is equivalent to this: IF EXIST 00??.txt ( DEL 00??.txt goto done ) So you could do stuff like this IF EXIST 00??.txt ( ECHO Deleting files: 00??.txt DEL 00??.txt goto done ) Does the program take command line options at all? If so then keeping track of where a script is up to in the list of files should be fairly easy. Del can be tricky because when deleting using simple filemask it can delete files you don't want to delete, because del parses the short filenames too and not just the long filenames.Deleter4.bat: @echo off for /l %%N in (0,1,9) do if exist 0%%N??.txt echo Deleting files starting 0%%N00 & del 0%%N??.txt & goto done echo No files left! :done echo Finished Output of 11 runs, starting with a folder of 1000 files: D:\testfolder\del100>deleter4 Deleting files starting 0000 Finished D:\testfolder\del100>deleter4 Deleting files starting 0100 Finished D:\testfolder\del100>deleter4 Deleting files starting 0200 Finished D:\testfolder\del100>deleter4 Deleting files starting 0300 Finished D:\testfolder\del100>deleter4 Deleting files starting 0400 Finished D:\testfolder\del100>deleter4 Deleting files starting 0500 Finished D:\testfolder\del100>deleter4 Deleting files starting 0600 Finished D:\testfolder\del100>deleter4 Deleting files starting 0700 Finished D:\testfolder\del100>deleter4 Deleting files starting 0800 Finished D:\testfolder\del100>deleter4 Deleting files starting 0900 Finished D:\testfolder\del100>deleter4 No files left! Finished |
|