Anyone know how to delete all files in a folder that does NOT contain a specific string? For example, if I have a bunch of files that have _YM in the file name, and other files that have _HO, _MM, or _OT. I only want to keep _YM and get rid of everything else in DOS command. Thanks.
Move the files that contain the string you want, delete the rest, move the kept files back. The DOS shell is very limited when it comes to scripting. I've used cygwin before, which is the GNU tools ported to Windows, and had much better luck. In most UNIX shells, what you want could be easily accomplished with a find script.
There are many ways to do this, but you need to think OUTSIDE the box. IT and scripting is all about being lazy and saving time... but showing your boss you're doing an extensive job. Don't think of "deleting everything but something else," think of it as "only keeping something else." Therefore: output the listing of the directory containing "_YM" to a file. Using this file, do a for look that moves or copies those out to another directory temporarily, then delete everything in the directory where you had them first. You've deleted everything and salvaged the "YM" ones. Now bring back everything from the temporary directory back into the original directory. You're done.
Change the file attribute of the files you want to keep to read only (+R), then issue a command to delete everything. The delete will fail on files with the read-only attribute set. So, for example, if you want to keep a_YM.txt and b_YM.txt, but want to delete a_MM.txt and b_OT.txt : attrib +R *_YM* del * You don't really need a batch file. I'm doing this off the top of my head, and am not sure which Windows you're using or how it behaves differently in various versions of it, but it's worked for me in the past. Try it in a dummy directory first.
I'd do a variation of this. First turn off the archive attribute: ATTRIB -A *.* Then turn on the archive attribute for the _YM files: ATTRIB +A *_YM* Then delete all files with the archive attribute off: DEL *.* /A:-A
Thanks guys. I am using DOS commands within SAS. I will give it a try tomorrow with both of the suggestions you guys had. It's easy to remove files with specific text, but wasn't as easy to remove everything but. BTW, I was just using player initials as examples.:grin: