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}

Comments

  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

    ReplyDelete

Post a Comment

Popular posts from this blog

Yet once more into the breech (of altered programming logic)

How to convert SVG data to a Png Image file Using InkScape

Simple WP7 Mango App for Background Tasks, Toast, and Tiles: Code Explanation