1

I have a folder test which has some other files and folders. I also have a bat file delete.bat

test test\delete.bat

The bat file delete the contents of test using the next command: rmdir C:\test /s /q

I also want to delete the folder test. If i copy the delet.bat file and paste it in another directory the test folder will be deleted.

However, if i run the delete.bat file inside the test folder, all the contents of the test folder(included the delete.bat file) are deleted, but the test folder not.

I consider that this is because the test folder is opened.

Any suggestion? Is there any command that i can add at the beginning if delete.bat in order to close the folder first and then run the command rmdir C:\test /s /q?

xarlap
  • 157
  • 1
  • 2
  • 14

2 Answers2

2

The following delete.bat works fine is you run it using win+R in Windows.

cd c:\
rmdir C:\test /s /q
sasha_gud
  • 1,635
  • 13
  • 18
1

Ok,

Sasha_gud helps a lot.

Now i find a way to add code in bat file in order to run as administrator (so the run as administrator will be run automatically).

For more information look at How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?

Here is the code:

:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
@echo off
CLS 
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================

:checkPrivileges 
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ) 

:getPrivileges 
if '%1'=='ELEV' (shift & goto gotPrivileges)  
ECHO. 
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation 
ECHO **************************************

setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs" 
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >>     "%temp%\OEgetPrivileges.vbs" 
"%temp%\OEgetPrivileges.vbs" 
exit /B 

:gotPrivileges 
::::::::::::::::::::::::::::
:START
::::::::::::::::::::::::::::
setlocal & pushd .

REM Run shell as admin (example) - put here code as you like

cd c:\
rmdir C:\Users\haris\Desktop\test /s /q

Only the 2 last lines are my code.

Community
  • 1
  • 1
xarlap
  • 157
  • 1
  • 2
  • 14