"bat" file keeps closing after executing

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
b_rianv
Posts: 2
Joined: 25 Aug 2019 12:33

"bat" file keeps closing after executing

#1 Post by b_rianv » 25 Aug 2019 13:56

Good afternoon everyone,
Hi my name is Brian and I have rewritten a bat file I have and trying to combined my other ones so that I have one Master bat file to run. I will use it for deep cleaning computers I work with at work (win7/win10). The first part of the bat is for killing task/processes and I believe that is where my problem starts. I have remove that part and was able to get the bat to work. So I just need to get the first part "killing task/processes" to work and then I believe the rest of the bat file will run. For some reason after I "right click" and "run as Administrator" the first few line will run and then the bat file just closes. Can someone please help me out as I would like to use it at work?
Thank you,
Brian

Code: Select all

@echo off
Color 0C
echo Kill Task.....EXECUTING!!!
TIMEOUT /T 10
SETLOCAL EnableDelayedExpansion
cd c:\Windows\System32
FOR /F “DELIMS=: TOKENS=2” %%i IN (‘TASKLIST /FI “USERNAME EQ %userdomain%\%username%” /FO LIST ^| FIND /I “Image name: “‘) DO (
:: Remove space
SET var=%%~i
SET MyVar=!var!
SET MyVar=!MyVar: =!
:: Process with space in the name
if “!MyVar!”==”AutomationAnywhere.exe” (
SET “MyVar=Automation Anywhere.exe”
)
:: Applications
if not “!MyVar!”==”Automation Anywhere.exe” (
if not “!MyVar!”==”always-on-top.exe” (
:: Windows Services
if not “!MyVar!”==”svchost.exe” (
if not “!MyVar!”==”explorer.exe” (
if not “!MyVar!”==”cmd.exe” (
if not “!MyVar!”==”conhost.exe” (
if not “!MyVar!”==”dllhost.exe” (
if not “!MyVar!”==”tasklist.exe” (
if not “!MyVar!”==”vmtoolsd.exe” (
if not “!MyVar!”==”dwm.exe” (
if not “!MyVar!”==”taskhostex.exe” (
if not “!MyVar!”==”rdpclip.exe” (
if not “!MyVar!”==”find.exe” (
taskkill /f /im “!MyVar!” /FI “USERNAME EQ %userdomain%\%username%”
) ) ) ) ) ) ) ) ) ) ) ) )
)
echo.
echo Waiting 5 seconds…

echo.
TIMEOUT / 5
echo CleanUp.....INVOKING!!!
TIMEOUT /T 5
Color 0A

ECHO -----
ECHO STATUS: CHKDSK scheduling in progress.
ECHO Y | CHKDSK C: /F

ECHO STATUS: CHKDSK scheduling completed, defragmentation in progress.
defrag.exe c: -f /h /x

REM Checking System File Integrity
ECHO Checking System File Integritu
SFC /SCANNOW

echo Cleaning system junk files, please wait…
REM displays a line of text

del /f /s /q %systemdrive%\*.tmp
del /f /s /q %systemdrive%\*._mp
del /f /s /q %systemdrive%\*.log
del /f /s /q %systemdrive%\*.gid
del /f /s /q %systemdrive%\*.chk
del /f /s /q %systemdrive%\*.old
del /f /s /q %systemdrive%\recycled\*.*
del /f /s /q %windir%\*.bak
del /f /s /q %windir%\prefetch\*.*
rd /s /q %windir%\temp & md %windir%\temp
del /f /q %userprofile%\cookies\*.*
del /f /q %userprofile%\recent\*.*
del /f /s /q “%userprofile%\Local Settings\Temporary Internet Files\*.*”
del /f /s /q “%userprofile%\Local Settings\Temp\*.*”
del /f /s /q “%userprofile%\recent\*.*”

REM /f: force deleting of read-only files
REM /s: Delete specified files from all subdirectories.
REM /q: Quiet mode, do not ask if ok to delete on global wildcard
REM %systemdrive%: drive upon which the system folder was placed
REM %windir%: a regular variable and is defined in the variable store as %SystemRoot%. 
REM %userprofile%: variable to find the directory structure owned by the user running the process

del /s /f /q c:\windows\temp\*.*
rd /s /q c:\windows\temp
md c:\windows\temp
del /s /f /q C:\WINDOWS\Prefetch
del /s /f /q %temp%\*.*
rd /s /q %temp%
md %temp%
deltree /y c:\windows\tempor~1
deltree /y c:\windows\temp
deltree /y c:\windows\tmp
deltree /y c:\windows\ff*.tmp
deltree /y c:\windows\history
deltree /y c:\windows\cookies
deltree /y c:\windows\recent
deltree /y c:\windows\spool\printers
deltree c:\windows\temp\*.* /y
del c:\WIN386.SWP

Del /F /Q %APPDATA%\Microsoft\Windows\Recent\*

Del /F /Q %APPDATA%\Microsoft\Windows\Recent\AutomaticDestinations\*

Del /F /Q %APPDATA%\Microsoft\Windows\Recent\CustomDestinations\*

reg delete HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\RunMRU /VA /F
reg delete HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\TypedPaths /VA /F 
reg delete "HKEY_CURRENT_USER\Software\Microsoft\Terminal Server Client\Default" /va /f
reg delete "HKEY_CURRENT_USER\Software\Microsoft\Terminal Server Client\Servers" /f
reg add "HKEY_CURRENT_USER\Software\Microsoft\Terminal Server Client\Servers"
cd %userprofile%\documents\
attrib Default.rdp -s -h
del Default.rdp

echo off

set DataDir=C:\Users\%USERNAME%\AppData\Local\Microsoft\Intern~1\

del /q /s /f "%DataDir%"
rd /s /q "%DataDir%"

set History=C:\Users\%USERNAME%\AppData\Local\Microsoft\Windows\History

del /q /s /f "%History%"
rd /s /q "%History%"

set IETemp=C:\Users\%USERNAME%\AppData\Local\Microsoft\Windows\Tempor~1

del /q /s /f "%IETemp%"
rd /s /q "%IETemp%"

set Cookies=C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Cookies

del /q /s /f "%Cookies%"
rd /s /q "%Cookies%"

C:\bin\regdelete.exe HKEY_CURRENT_USER "Software\Microsoft\Internet Explorer\TypedURLs"

echo off | clip
echo y | del %1\*.*

echo Network Connetion.....PURGING!!!
TIMEOUT /T 10
ipconfig /release 
ipconfig /renew 
ipconfig /flushdns
ipconfig /registerdns 

echo Network Connection.....TESTING!!!
TIMEOUT /T 15
@echo off
Color 0E

cd\ 
ping google.com -n 10

echo Cleaning of junk files is.....FINISHED!!!
TIMEOUT /T 10
shutdown -r -f -t 30 /C "Applying Changes" 

ECHO.
ECHO Removing CleanUp utility...
REM Delete self on Exit
cmd /k DEL "%~f0"

Erzesel
Posts: 5
Joined: 25 Aug 2019 02:28
Location: Leipzig /DE

Re: "bat" file keeps closing after executing

#2 Post by Erzesel » 25 Aug 2019 17:40

right at the beginning:
You're using a lot of quota characters that are not standard Batch. Only " and ' are valid.
In for-loops you can still use a BackQuote `. Other quotes are not allowed.

This should work:

Code: Select all

setlocal enabledelayedexpansion
FOR /F "TOKENS=2* DELIMS=:" %%i IN ('TASKLIST /FI "USERNAME EQ %userdomain%\%username%" /FO LIST ^| FIND /I "Image name:"') DO (
:: Remove space
SET var=%%~i
SET MyVar=!var!
SET MyVar=!MyVar: =!
:: Process with space in the name
if "!MyVar!"=="AutomationAnywhere.exe" (
SET "MyVar=Automation Anywhere.exe"
)
:: Applications
if not "!MyVar!"=="Automation Anywhere.exe" (
if not "!MyVar!"=="always-on-top.exe" (
:: Windows Services
if not "!MyVar!"=="svchost.exe" (
if not "!MyVar!"=="explorer.exe" (
if not "!MyVar!"=="cmd.exe" (
if not "!MyVar!"=="conhost.exe" (
if not "!MyVar!"=="dllhost.exe" (
if not "!MyVar!"=="tasklist.exe" (
if not "!MyVar!"=="vmtoolsd.exe" (
if not "!MyVar!"=="dwm.exe" (
if not "!MyVar!"=="taskhostex.exe" (
if not "!MyVar!"=="rdpclip.exe" (
if not "!MyVar!"=="find.exe" (
echo taskkill /f /im "!MyVar!" /FI "USERNAME EQ %userdomain%\%username%"
) ) ) ) ) ) ) ) ) ) ) ) )
)
My solution for Selectiv Taskkill edit: the use of findstr /v to exclude processes was a bad and slow idea :P

Code: Select all

@echo off
setlocal enabledelayedexpansion
 rem look for running processes in scope of the current user
    rem exlusions=====================================================================================
  rem set good processes  to exlusions.
  rem here add all processes they known as not bad for your purpose
  rem ...Systemprocesses... You  can change  this  list  as you want....
set "exclusions=cmd.exe conhost.exe dllhost.exe dwm.exe explorer.exe find.exe findstr.exe rdpclip.exe svchost.exe taskeng.exe taskhost.exe taskhostex.exe taskmgr.exe tasklist.exe userinit.exe vmtoolsd.exe
  rem filenames with Spaces  in Quotes!
set "exclusions=%exclusions% "my   test.exe" "Automation Anywhere.exe" "

for /f " tokens=1,2 delims=," %%a in ('"tasklist /nh /fo csv /fi "username eq %userdomain%\%username%" 2>nul |sort"') do (
    set "excluded_Flag="
    for %%e in (%exclusions%) do (
        if /i "%%~a"=="%%~e" set "excluded_Flag=1"
    )
    if !excluded_Flag! neq 1 (
        set "PID_list=!PID_list! /PID %%~b"
        echo PID for kill %%a %%b saved 
    ) else (echo excluded %%a %%b )
)

  rem  let's make a Masskill
  rem Demo remove  echo to make Taskkill sharp
echo taskkill /f %PID_list%
pause 
exit /b
  • first pass create the PID-List for Taskkill
  • second pass Execute Taskkill with the List of PIDs
Some error messages from Taskkill are normal because removing tree-based applications also stops their child processes
At this point, I also interrupt the work on your batch. Your cleanup is certainly not good for my system ....

b_rianv
Posts: 2
Joined: 25 Aug 2019 12:33

Re: "bat" file keeps closing after executing

#3 Post by b_rianv » 26 Aug 2019 13:35

Thank you for your help and I will look at your code. If I may ask, why wouldn't my bat file work for your system? Did I do something wrong in my code, if so please show me.
Thank you for your time

ShadowThief
Expert
Posts: 908
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: "bat" file keeps closing after executing

#4 Post by ShadowThief » 27 Aug 2019 03:05

Personally, I'm not going to run your script because
  • it kills any process except for the ones listed in your script and you're missing some super important system processes
  • you're blowing away and restoring %TEMP%, which won't work if anything is using the directory at the time
  • you're deleting WIN386.SWP which hasn't shipped with Windows since XP, so you've very clearly found a cleanup script online somewhere and haven't actually tested this yet

Erzesel
Posts: 5
Joined: 25 Aug 2019 02:28
Location: Leipzig /DE

Re: "bat" file keeps closing after executing

#5 Post by Erzesel » 27 Aug 2019 04:26

b_rianv wrote:
26 Aug 2019 13:35
... why wouldn't my bat file work for your system? Did I do something wrong in my code,
Many of the commands inserted into your batch may be syntactically correct. But my system is running stable for years as I keep it clean in my way.
Your batch deletes a lot of possibly system-relevant files and folders (I have not checked each of youre deletes :roll: ). Although many "would-be-administrators" on the net are of the opinion that some of them are completely obsolete and useless, there may be reasons why they have not yet been removed by M$. Standardcleaning of some Temp-files is done by some clicks, without harmful commands.

For example:
You apply defrag.exe to the C: drive. In the present time, the system is usually on an SSD, for which no defragmentation is necessary and even harmful.

In conclusion, your batch is of dubious benefit to a lot of individual operations.
For not having enough knowledge of Batch syntax to spot obvious errors yourself, your batch simply goes too deep into systemic structures.
I will use it for deep cleaning computers I work with at work
Please do not do that if you care about your job. Your boss and your colleagues will be infinitely grateful to you if you do not kill their computer. :shock:
There are a lot of professional programs to clean up the system. These are improved for many years and remove garbage quite reliably

Try less problematic exercises. In the area of ​​this forum, there are impressive experiments on some strange possibilities of batch commands
For me, the golden rule has been in effect for 35 years: "Do not touch a runnig sytem". Some 100MB of garbage or useless Files is not a serious problem for TeraByte volumes.

Eureka!
Posts: 29
Joined: 25 Jul 2019 18:25

Re: "bat" file keeps closing after executing

#6 Post by Eureka! » 27 Aug 2019 14:13

b_rianv wrote:
25 Aug 2019 13:56
For some reason after I "right click" and "run as Administrator"
Be aware that if you start your script like that, you will end up with a completely different %USERNAME% if you weren't logged in as a local administrator.
My advice: Split your script in a SYTEM part and a USER part.
(although there are batch techniques to "un-elevate" a script ..)

As @Erzesel already pointed out: be *very* careful when deleting stuff. Know what you are doing and know wha to do when things go wrong. Otherwise things can get messy (don't be the proverbial "child with a chainsaw")
And I suspect you are not yet "in control" (DELTREE is no longer part of Windows since Windows 98 (IIRC))

That out of the way:
Try this for killing your processes:

Code: Select all

@echo off
setlocal
set WHITELIST= 'Automation Anywhere.exe' 'always-on-top.exe' 'svchost.exe' 'explorer.exe' 'cmd.exe' 'conhost.exe' 'dllhost.exe' 'tasklist.exe' 'vmtoolsd.exe' 'dwm.exe' 'taskhostex.exe' 'rdpclip.exe'

FOR /F "usebackq tokens=1,2 delims=," %%i IN (`TASKLIST.exe /FI "USERNAME EQ %userdomain%\%username%" /NH /FO CSV`) do (
	echo KEEP '%%~i'|findstr /i "%WHITELIST%" || echo KILL %%i with PID %%~j
)
	pause
Output will be something similar to:

Code: Select all

KILL "RuntimeBroker.exe" with PID 5088
KILL "Q-Dir_x64.exe" with PID 3464
KILL "notepad++.exe" with PID 8140
KEEP 'svchost.exe'
KILL "Everything.exe" with PID 4184
KEEP 'explorer.exe'
KILL "firefox.exe" with PID 1304
KILL "firefox.exe" with PID 1600
This doesn't actually kill your processes, but shows you what will be executed.
If this is what you expected, you can replace echo KILL %%i with PID %%~j with:
taskkill.exe /PID %%~j /t
(I didn't test this last part; the rest is tested)

Post Reply