SQL_8119
SQL_8119

Reputation: 31

How do I delete files only if the parent directory exists in the Windows command prompt?

I'm using this batch script to delete files

FORFILES /P "X:\test" /S /M *.bak /C "CMD /C DEL @path"

However, the X drive is a resource on an active/passive cluster. I have to run the batch file on both nodes. Two questions...

  1. Is this the best method?
  2. I want the batch to look for the X drive before deleting the files - do you know of a way? I don't want it to run on the passive cluster because the X drive won't be on it.

Upvotes: 3

Views: 6742

Answers (2)

dbenham
dbenham

Reputation: 130919

It would be much faster to simply use DEL instead of FORFILES. You can test if the root path exists using IF EXIST.

if exist "x:\test\*.bak" del /q /s "x:\test\*.bak"

Upvotes: 8

Nathan Rice
Nathan Rice

Reputation: 3111

I just trying copying a file to the folder. Then check to see if the file exists afterward. If it does, I delete the file, and then I can proceed.

Upvotes: 0

Related Questions