Wednesday, December 2, 2009

Batch File Bug

This is the code I have:

cd %PROJECTROOTLOCAL%\Installation\AmazonS3\AmazonS3\
del  /s /f /q *.*

What I want to do is delete all files in the Installation\AmazonS3\AmazonS3 directory.  However what if the directory doesn’t exist?  Well I get this error on the first line:

The system cannot find the path specified.

And the current directory isn’t changed.  Which leaves me at the root of the my project.  Which means that the second command deletes all the files in my project.  Not good.  The fix is this:

mkdir %PROJECTROOTLOCAL%\Installation\AmazonS3\AmazonS3\
cd %PROJECTROOTLOCAL%\Installation\AmazonS3\AmazonS3\
del  /s /f /q *.*

Or is there something better?

{6230289B-5BEE-409e-932A-2F01FA407A92}

1 comment:

1. I would always specify a full path in a batch file, when deleting files. Use the for command to do this.

for /r c:\temp %a in (*.*) do del /s /f /q %a

remember to make the varible %%a in the batch file.

One could also use the:
FOR /F ["options"] %%parameter IN('command_to_process') DO command

Then you could use dir to get a more targeted list of files, especally if they are hidden, system ect...

See ss64.com for great references