Freigeben über


Two quick file-manipulation scripts from the vault

There are a couple of scripts that have been with me since the beginning of time. I don't use them that much anymore, but they occasionally come in handy. I'm always pleasantly surprised to find that I still have them when I need them. One makes files and the other deletes files.

The first is called 'makefiles.cmd'. It just creates any files that you specify on the command line. This is handy for making some little scratch files you might need to test another script.

    makefiles.cmd

    @for %%x in (%*) do @echo %%x > %%x

This line loops over every argument passed in on the command line, say I run this as: makefiles foo.txt bar.txt donotdelete.txt. This line, then uses foo.txt as the first argument, bar.txt as the second, etc. It then echos the name of that file and redirects that into a file of that same name. That is, 'foo.txt' will just have the text 'foo.txt' in it.

The second is called 'delallbut.cmd'. This deletes all files in the current directory except for the one you specify on the command line.

    delallbut.cmd

    @echo off
if defined ECHO (echo %ECHO%)
for %%x in (%*) do attrib +r %%x >nul 2>&1
del /q . >nul 2>&1
for %%x in (%*) do attrib -r %%x >nul 2>&1

This deserves a little bit of explanation. I often start scripts with those first two lines. If I want to see what a given script is doing (usually when I'm testing it or something unexpected is happening when I run it), I set an environment variable, ECHO, to on: set ECHO=on. So the first line here turns echo off (the default), then checks to see if I have ECHO set to on, and if so, turns it echo on. Note that I can also turn echo back off by setting ECHO to off. There's either a bug or an undocumented feature here, wherein if I set ECHO to, say, "Hi, how are you?", that text will be echoed at the beginning of this script. Sure, I could explicitly check to see whether ECHO is set to on or off, but that's more work than it's worth, in my opinion.

The next three lines are the meat of the script. The first line loops over all of the files you've passed in (like we did in makefiles.cmd). The action it takes for each of these files is to set the Read-only file attribute. The >nul 2>&1 bit basically says "throw away the output of attrib (if any)". I'll go into more detail about that little bit in a later posting.

So now every file we want to save is marked read-only. The next line is a shortcut for del *.*, or 'delete all files in this directory'. And again, we throw away any text that del might output. The following line turns the read-only flag back off, and we're done. All the files in this directory have been blown away except the ones we've specified. If you used the example for makefiles.cmd, then run delallbut foo.txt donotdelete.txt, you'll notice that bar.txt has disappeared, but foo.txt and donotdelete.txt are still intact.

Now, for bonus points, what problems can you find in these scripts? There are some assumptions made in each of these scripts. Leave your answers in the comments.

Comments

  • Anonymous
    June 03, 2005
    delallbut will not delete a file that already has the read only flag set, also if the file you dont delete had the ro flag, this is lost

    I really like "if defined ECHO (echo %ECHO%)", will use this in my batch files from now on =)

    Looking forward to why ">nul 2>&1" is better that ">nul"
  • Anonymous
    June 08, 2005
    BTW, my favorite part about "if defined ECHO (echo %ECHO%)" is saying it out loud. ECHO...Echo...echo...