For deleting the files, you can first try to ren
(rename) the file. ren
will set ERRORLEVEL to 1 if the file is locked.
@echo OFF
:: Delete all files, but exit if a file is locked.
for %%F in (*.*) do (
@echo Deleting %%F
ren %%F tmp 2> nul
if ERRORLEVEL 1 (
@echo Cannot delete %%F, it is locked.
exit /b 1
)
del tmp
)
I suspect you may be able to do the same thing for directories, but I can't seem to figure out how to get a directory locked so I can test. The following may work:
:: Remove all directories, but exit if one is locked.
FOR /D %%G in (*) DO (
@echo Removing %%G
ren %%G tmpdir 2> nul
if ERRORLEVEL 1 (
@echo Cannot remove %%G, it is locked
exit /b 1
)
RD /s /q tmpdir
)