Help with IF in batch files

hakon

Member
Here is the code:

@ echo off
set /p file=File to delete:
if "%file%" == "*.exe" goto kill
if exist "%file%" goto del
echo File does not exist
goto exit
:kill
taskkill /im "%file%" /f /t
:del
attrib -s -r -h -a "%file%"
del "%file%" /f /q
if exist "%file%" goto err
echo File deleted.
goto exit
:err
choice /M "Error, file was not deleted, do you want it deleted on next reboot"
if ERRORLEVEL==2 goto exit
REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Currentversion\RunOnce /v "Del "%file%" OnNextReboot" /d ^"cmd.exe /c DEL /F /Q \"%file%"" /f"
choice /M "Do you want to reboot now"
if ERRORLEVEL=2 goto exit
shutdown -r -t 60
:exit
pause
exit

The thing that dosent work is the 3th line AND i have yet to figure out how to kill that process, since "%file%" can be a path to a exe (C:\Windows\explorer.exe). Obviously taskill /im "C:\Windows\explorer.exe" /f /t does not work

So, i need help with the 3th line and some ideas how to kill "%file%" if it is a path to a exe

- Hakon
 

My Computer

System One

  • Manufacturer/Model
    Packard Bell BV EasyNote SB87
    CPU
    Intel Core 2 Duo 2.2 GHz
    Motherboard
    Intel Crestline-PM PM965
    Memory
    2x 2 GB DDR2-667 DDR2 SDRAM
    Graphics Card(s)
    NVIDIA GeForce 8600M GT (512 MB)
    Sound Card
    Intel 82801HBM ICH8M
    Monitor(s) Displays
    AU Optronics B170PW03 [17" LCD]
    Screen Resolution
    1440x900
    Hard Drives
    2x Seagate Momentus 7200RPM 200GB
Hi Hakon,

Have a look at the following bit of code. Note that I haven't tested it, although it should work. Pay careful attention to the warning given.
Code:
@ echo off
:main
cls
echo WARNING Incorrect use of this routine can render your system
echo inoperable. Use at your own risk.
echo.
echo Type the full name and extension of the file you want to delete.
echo If no path is specified, the file is assumed to be in the same
echo directory as this batch file.
echo Use '>' instead of '\' in path.
echo.
set /p file="Enter Filename "
echo %file%
echo.
choice /M "Executable file?"
echo %ERRORLEVEL%
echo.
pause
if not exist %file% (
echo File does not exist
echo.
goto exit )
if %ERRORLEVEL%==2 goto delete
:kill
echo kill
echo.
taskkill /IM %file% /F /T
pause
:delete
echo delete
echo.
attrib -r -a -s -h -i %file%
del /P /F %file%
if not exist %file% (
echo File deleted
echo.
goto exit )
choice /M "File not deleted. Do you wish to delete on next reboot?"
echo %ERRORLEVEL%
echo.
if %ERRORLEVEL%==2 goto exit
REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Currentversion\RunOnce /v "Del "%file%" OnNextReboot" /d ^"cmd.exe /c DEL /F /Q \"%file%"" /f"
choice /M "Do you want to reboot now?"
echo %ERRORLEVEL%
echo.
if %ERRORLEVEL%==2 goto exit
shutdown -r -t 60
:exit
echo exiting
echo.
pause
exit
 
Last edited:

My Computer

System One

  • Manufacturer/Model
    Dwarf Dwf/11/2012 r09/2013
    CPU
    Intel Core-i5-3570K 4-core @ 3.4GHz (Ivy Bridge) (OC 4.2GHz)
    Motherboard
    ASRock Z77 Extreme4-M
    Memory
    4 x 4GB DDR3-1600 Corsair Vengeance CMZ8GX3M2A1600C9B (16GB)
    Graphics Card(s)
    MSI GeForce GTX770 Gaming OC 2GB
    Sound Card
    Realtek High Definition on board solution (ALC 898)
    Monitor(s) Displays
    ViewSonic VA1912w Widescreen
    Screen Resolution
    1440x900
    Hard Drives
    OCZ Agility 3 120GB SATA III x2 (RAID 0)
    Samsung HD501LJ 500GB SATA II x2
    Hitachi HDS721010CLA332 1TB SATA II
    Iomega 1.5TB Ext USB 2.0
    WD 2.0TB Ext USB 3.0
    PSU
    XFX Pro Series 850W Semi-Modular
    Case
    Gigabyte IF233
    Cooling
    1 x 120mm Front Inlet 1 x 120mm Rear Exhaust
    Keyboard
    Microsoft Comfort Curve Keyboard 3000 (USB)
    Mouse
    Microsoft Comfort Mouse 3000 for Business (USB)
    Internet Speed
    NetGear DG834Gv3 ADSL Modem/Router (Ethernet) ~4.0 Mb/s (O2)
    Other Info
    Optical Drive: HL-DT-ST BD-RE BH10LS30 SATA Bluray
    Lexmark S305 Printer/Scanner/Copier (USB)
    WEI Score: 8.1/8.1/8.5/8.5/8.25
    Asus Eee PC 1011PX Netbook (Windows 7 x86 Starter)
Back
Top