1

This seems like it should be simple, but I'm stuck. I'm looping over a list of photo file paths from a text file and deleting each one. The problem is that if the del command fails (this could happen if the file didn't exist or was in use by some other program), I don't get any notification in the log.

BTW- This is windows server 2008 R1 CMD.

Here is my code:

@echo off for /F %%A in (f:\deletePhotos\deletelist.txt) do ( echo del %%A /f >> f:\deletePhotos\log.txt 2>&1 ) 

Here is a sample of the log:

del f:\photos\11\48\ALX10-9322-48.jpg /f del f:\photos\11\48\ALX10-9692-48.jpg /f del f:\photos\11\48\LKSR20-5910-48.jpg /f del f:\photos\11\48\LKSR20-8639-48.jpg /f del f:\photos\11\48\SEMN4030836-48.jpg /f del f:\photos\11\48\SEMN4036515-48.jpg /f 

The files from the log (above) do NOT exist (I checked). Obviously, I am trying to get the log to show some error like:

Could Not Find f:\photos\11\48\ALX10-9322-48.jpg 

But instead I just get an echo of the command itself. Please help!

*UPDATE* Figured it out: Good old trial and error: had to move the log output and the redirection outside of the bat file execution.

Like this:

f:\deletephotos>del.bat f:\deletePhotos\delete_11.txt >> f:\deletePhotos\log.txt 2>&1 

That did the trick.

6
  • Sorry, I just don't get it. The script you've posted does echo the DEL commands instead of executing them. So, naturally, you couldn't possibly get any "File Not Found" errors. Commented Jan 11, 2013 at 7:26
  • Yes- it echos the delete commands, NOT the results of the delete commands. If you look at my log sample above you will see that even if the delete command fails, it still just echos the original command. I needed it to echo the error. The trick was to add the logging to the bat file request, not IN the bat file itself. Now it is working perfectly. Commented Jan 11, 2013 at 8:15
  • Perhaps you forgot to add something then, because from what I can see, a DEL command couldn't fail in your script, because there's no DEL command in your script. There's an ECHO that prints strings which look like DEL commands, but they are never executed, so there can be no talk about something failing. I mean, it's great that you've managed to resolve the issue and I'm perfectly happy for you, really. I'm merely pointing out that there was probably not enough information in your question for anyone else to figure out the issue. Commented Jan 11, 2013 at 8:35
  • Ok- I'm learning... So- how would you echo the output of the delete command inside of the loop? I tried so many different ways, that I don't even remember what I did. I'd love to understand this better if you have more info. Commented Jan 11, 2013 at 8:39
  • For clarity sake, could you specify "one" what in "delete each one"? For those with ADD it will help quicken understand and maintain retention. ;) Commented Jan 11, 2013 at 19:04

1 Answer 1

1

In answer to comments, here's my attempt at a script that deletes files listed in a text file and logs the results. If the file is deleted successfully, it logs filename deleted, otherwise it logs the error message produced by the DEL command:

@ECHO OFF > logfile ( FOR /F "tokens=*" %%A IN (filelist) DO ( DEL "%%A" 2>&1 | FINDSTR "." || (ECHO %%A deleted) ) ) 

I must admit that I had to use a small trick to make my script more or less compact. My original idea was based on checking the direct result (the exit code) of DEL's execution. It appears, however, that DEL doesn't set the exit code based on the target file's existence. More specifically, it returns "success" regardless of whether the file was found or not. Yes, it does print the error message if it could not find the file, but the exit code is 0 in either case.

So I was forced to reduce myself to tricks and stratagems and eventually found this page, whence my little trick comes. The idea is, basically, to check whether the DEL's stderr output contains anything. If it doesn't, consider that an actual success and print filename deleted. Otherwise, the discovered message eventually makes its way to the log file.

You can see that the redirection of the output is placed inside the script. If you prefer to specify the redirection on the command line when calling the batch script, remove the logfile ( line and the corresponding closing bracket.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the additional info. It's 3:30 am here so I'm going to bed. But I will be sure to check out the info in more detail tomorrow. Thanks again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.