Batch: Kill Zombie Tasks

@echo off

rem TMstatus.bat set zombie vars to full or partial task name to test remove from Task Manager
Demonstrates (a) Timestamps (b) FOR parsing of delimited fields and (c) IF logical comparisons
NOTE: Replace 'Zombie1' and 'Zombie2', with your choice from command line TASKLIST

setlocal
set zombie1=firef
set zombie2=SoftwareUpdate
set hasargument=no_argument
if not "%1"=="" set hasargument=%1
:maketimestamp

set xtime=%time::=%
set xtime=%xtime:.=_%
set xdate=%date: =_%
set xdate=%xdate:/=_%
rem set xdate=%xdate:~4%
set xtime=%xtime:~0,-3%
rem set timestamp=%xdate%_%xtime%
set timestamp=%xdate%
rem set newdir=_hold_%timestamp%
set outputfile=taskman%xdate%.log

:randomfile
set randfile=delete%random%.txt
TASKLIST /NH /FI "IMAGENAME eq %zombie1%*"
TASKLIST /NH /FI "IMAGENAME eq %zombie2%*"

TASKLIST /NH /FI "IMAGENAME eq %zombie1%*" >> %randfile%
TASKLIST /NH /FI "IMAGENAME eq %zombie2%*" >> %randfile%

for /f "tokens=3" %%f in ('find /i /c "%zombie1%" %randfile%') do set whatcount1=%%f
for /f "tokens=3" %%g in ('find /i /c "%zombie2%" %randfile%') do set whatcount2=%%g
echo %timestamp% %0 count of %zombie1% is: %whatcount1%
echo %timestamp% %0 count of %zombie1% is: %whatcount2%

echo %timestamp% %0 %hasargument% count of %zombie1% is: %whatcount1% >> %outputfile%
echo - %timestamp% %0 %hasargument% count of %zombie2% is: %whatcount2% >> %outputfile%
rem if %whatcount% GTE 2 TASKKILL /S system /U username /P password /FI "IMAGENAME eq note*"
if %whatcount1% GEQ 1 TASKKILL /FI "IMAGENAME eq %zombie1%*" >> %outputfile%
if %whatcount2% GEQ 1 TASKKILL /FI "IMAGENAME eq %zombie2%*" >> %outputfile%
DEL %randfile%
:skip

ECHO FOR MORE DETAILS try command line: TASKLIST /M /FI "IMAGENAME eq SubSys*"

if "%1"=="" echo end of %0
if "%1"=="" pause
endlocal

Batch: Clean Desktop

@echo off

REM moves graphic files to "Screenshots", other files to folder "_Desktop<timestamp>
REM demonstrates current date/time stamps AND "for (each) loops

setlocal
rem set debug=anythingbutblank
pushd %userprofile%\Desktop
if not exist Screenshots md Screenshots
:makedir with timestamp
set xtime=%time::=%
set xtime=%xtime:.=_%
set xdate=%date: =_%
set xdate=%xdate:/=_%
rem set xdate=%xdate:~4%
set xtime=%xtime:~0,-3%
rem set timestamp=%xdate%_%xtime%
set timestamp=%xdate%
set newdir=_Desktop_%timestamp%
if not "%debug%"=="" @echo on
if not exist %newdir% md %newdir%
rem excluded LNK URL
if not "%debug%"=="" echo newdir is: %newdir%
rem for %%x in (*.url *.lnk *.jpg *.pdf *.txt *.bak *.ahk *.doc* *.sql *.xls* *.png *.xml *.bmp *.gif *.log *.rtf *.htm? *.msg *.zip) do if exist "%%x" move "%%x" %newdir%
for %%x in ( *.csv *.txt *.vsd *.css *.cs *.cpp *.bak *.php *.js *.json *.doc* *.sql *.xls* *.xml *.java *.py *.log *.rtf *.htm? *.msg *.eml *.gz *.zip) do if exist "%%x" move /-Y "%%x" %newdir%
for %%x in ( *.jpg *.pdf *.png *.bmp *.gif ) do if exist "%%x" move /-Y "%%x" Screenshotsif not exist Shortcuts md Shortcutsfor %%x in ( *.lnk *.URL ) do if exist "%%x" move /-Y "%%x" Shortcuts

:set n
:set x
echo end batch file %0
pause

endlocal

vB Regex Error Detection

This is a sample of "stub" code using REGEX (regular expressions) to find predefined Error words expecially when Internet Explorer unexpectedly asks for a Certificate.

    1. detect and skip  Windows Certificate Errors or similar Error page that IE may surprise the user with
    2. Fill in a Login Form
  • To WORK: it needs you to identify the (login or other) form elements
    1. identify a unique STRING in the unexpected  Error (Certificate) page
'________________________
Function checkForError(strErr,strTag)' returns 1 of 3 strings FULL LARGE INNERTEXT STRING IF ERR FOUND, ELSE "PASS"
Dim names,ele,innerText,outerText,temp,bFlag' used as string to debug
temp=""
bFlag=False' flag True if ERR FOUND, false if ERR NOT FOUND
Set names=objIE.Document.getElementsByTagName(strTag)

' sample: needs IE page object to be defined and navigated to as current page, having login form elements

'√' CHECKING for and bypassing MICROSOFT CERTIFICATE WARNING

isUnsafe=findClickOkIfUnsafe("not recommended",testLinks)
'bad string is first param signalling certificate error

If isUnsafe>=0then testLinks(isUnsafe-1).Click
' (1) works if HARD CODED FROM OBSERVATION - code returns
' (2) ??
' testLinks(isUnsafe).Click ' using index as boolean????  

call waitForReady(objIE)    ' do this after each click on menu
end If   
   ' self documenting comments by doing "find" of H4 tag or any desired flag/tag

' LEAVING MICROSOFT CERTIFICATE WARNING INTO LOG IN PAGE
'_________ functions / subroutines _________

Function findClickOkIfUnsafe(strInnerText,someArray) ' returns index if any member of someArray has "bad string"
Dim k, foundAt, strtmp

foundAt=-1
strtmp=""
'msgbox "array length: " & someArray.length , , "from Function findClickOkIfUnsafe"          ' for debugging, tells you where you are
for k = 0 to someArray.length-1    'LBound(someArray) to UBound(someArray)
If (boolRegExMatch ( someArray( k ).innerText, trim(strInnerText)) > 0)then
        'booltemp = True '( True OR booltemp)
foundAt= k
exit for
end if
next 'k

findClickOkIfUnsafe=foundAt
' if for/next never entered then returns -1 from second line

End Function

'________________________
Function boolRegExMatch(regx,strng) ' returns COUNT no it's NOT boolean!!!!
Dim regEx, Match,Matches, n, tmp ' Create variable.
Set regEx=NewRegExp ' Create a regular expression.
regEx.Pattern=regx ' Set pattern.
regEx.IgnoreCase=True ' Set case insensitivity.
regEx.Global=True ' Set global applicability.
Set Matches=regEx.Execute(strng) ' Execute search.
boolRegExMatch=Matches.count
Set Matches=Nothing
Set RegEx=Nothing
End Function

'________________________

Function checkForError(strErr,strTag)
' returns 1 of 3 strings full INNERTEXT STRING IF error found, ELSE "PASS"
Dim names,ele,innerText,outerText,temp,bFlag' used as string to debug
temp=""

bFlag=False ' flag True if ERR FOUND, false if ERR NOT FOUND
Set names=objIE.Document.getElementsByTagName(strTag)

For each ele in names
innerText=ele.innerText
If (boolRegExMatch(strErr,innerText)>0)Then
 
temp = "Fail: "&innerText' innerText

exit For   ' only reports first error found!?
Else
temp="PASS"
End If
Next
checkForError=temp
End Function
'________________________
Function fnLogin(uid,pwd)
Dim inputs,strPageStatus
Set inputs=objIE.Document.getElementsByTagName("INPUT")

inputs(2).Value=uid
inputs(3).Value=pwd
inputs(4).Click

' on Error GoTo 0
' If Err.Number then strPageStatus = "FAILERROR: CLOSE DUPLICATE BROWSER SESSION" & vbCrlf & Err.Description else
waitForReady(objIE)
strPageStatus=checkForError(".*password.*incorrect.*|Prohibite.*","DIV")'prefixes "Failed:" to InnerText
' End If
fnLogin=strPageStatus
End Function

'________________________

Function checkForError(strErr,strTag)' returns 1 of 3 strings full INNERTEXT STRING IF ERR FOUND, ELSE "PASS"
Dim names, ele, innerText, outerText, temp, bFlag  ' used as string to debug
temp=""

bFlag=False ' flag True if ERR FOUND, false if ERR NOT FOUND
Set names=objIE.Document.getElementsByTagName(strTag)

For each ele innames
innerText=ele.innerText
If (boolRegExMatch(strErr,innerText)>0)Then
'If Instr(1, innerText, strErr) Then temp="Fail: "&innerText' innerText

exit For  ' only reports first error found!?
Else
temp="PASS"
End If
Next
checkForError=temp
End Function
'________________________

How to TRIM() cr/lf?

What about TRIM() for removing white space from strings!
Although it is a really handy BUG-ELIMINATOR for *IF* ever a user pastes rich text into your (MS-Access) application, STILL this attachment demonstrates that TRIM only works on outer spaces, not other white space characters like ASCII characters 9, 10, 12, 13. (vbTab, vbLf, Formfeed, vbCr)

' Save this in a VBS file on your desktop for a VISUAL DEMO comparing three string cleaning functions below
Dim someOtherString, someOtherString1, someOtherString2, someOtherString3
someOtherString1 = vbCrLf & vbTab & "Hi Mom!" & int(rnd(100)*5) & vbTab & vbCrLf
someOtherString2 = vbCrLf & vbTab & "Hi Mom!" & int(rnd(100)*5) & vbTab & vbCrLf
someOtherString3 = vbCrLf & vbTab & "Hi Mom!" & int(rnd(100)*5) & vbTab & vbCrLf
msgbox chr(34) & trim(someOtherString1) & chr(34),,"Trim"
msgbox chr(34) & nibble(someOtherString2) & chr(34),,"Nibble"
msgbox chr(34) & chomp(someOtherString3) & chr(34),,"Chomp"
' --------------------
Function chomp(somestring)
if len(somestring) >= 1 then
while (asc(right(somestring,1)) <= 13 )
somestring = left(somestring, len(somestring)-1)
wend
end if
chomp = somestring
End Function
' --------------------
Function nibble(somestring)
if len(somestring) >= 1 then
while (asc(left(somestring,1)) <= 13 )
somestring = right(somestring, len(somestring)-1)
wend
end if
nibble = somestring
End Function

See https://excel-on-demand.com/2017/01/25/vbcommon-chomp-nibble/

VBS – time stamp: options demo

VBS will run in Windows as a stand alone script just like JS (JavaScript) and BATCH files.

'save as ANSI TXT file with extenstion ".vbs" 
'saving as UTF and Unicode will show "Error in Line 1"
nowis = Now
msgbox minuteStamp, vbOKOnly,"MinuteStamp"
msgbox timeStamp, vbOKOnly, "TimeStamp"
msgbox timestamphhmmss(nowis), vbOKOnly, "Time Stamp hh mm ss"

' ==== 'right' forces two digit values ===
Function minuteStamp() 
 minuteStamp = Year(Now) & "_" & _
 Right("0" & Month(Now),2) & "_" & _
 Right("0" & Day(Now),2) & "_" & _ 
 Right("0" & Hour(Now),2) & _
 Right("0" & Minute(Now),2) 
 '& _ Right("0" & Second(Now),2) 
End Function

' ============
Function timeStamp()
 timeStamp = Year(Now) & "_" & _
 Right("0" & Month(Now),2) & "_" & _
 Right("0" & Day(Now),2) & "_" & _ 
 Right("0" & Hour(Now),2) & _
 Right("0" & Minute(Now),2) & _
 Right("0" & Second(Now),2) 
End Function
' ======


Function timestamphhmmss(someTime)
 timestamphhmmss = FormatDateTime(someTime,vbshorttime) & ":" & right("0" & Second(someTime), 2)
End Function
 
'Constant Value Description from Microsoft® HELP
'vbGeneralDate 0 Display a date and/or time. If there is a date part, display it as a short date. If there is a time part, display it as a long time. If present, both parts are displayed.
' vbLongDate 1 Display a date using the long date format specified in your computer's regional settings.
' vbShortDate 2 Display a date using the short date format specified in your computer's regional settings.
' vbLongTime 3 Display a time using the time format specified in your computer's regional settings.
' vbShortTime 4 Display a time using the 24-hour format (hh:mm).
'_______________________________