Batch file to show or delete a numbered task names

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Batch file to show or delete a numbered task names

#1 Post by Hackoo » 09 Nov 2017 17:58

Hi ;)
I need a little help to improve my new batch to show or delete a numbered task names
The code below works fine if the task name does not contains a space :D
So i'm looking how can i modify it to read a task name with a space :?: :roll:
Thank you for any help :!:

Code: Select all

@echo off
Title Delete Tasks with their time tasks execution by Hackoo 2017
Mode 90,30 & color 0A
:Menu
cls & echo(
echo Type the time with this format "hh:mm" to show or delete for scheduled Tasks
set /a "count=0"
set /p "strTime="
cls & echo( 
Setlocal EnableDelayedExpansion
@for /f "tokens=1 delims= " %%a in ('schtasks /query ^| find /I "%strTime%"') do (
	set /a "Count+=1"
	set "TaskName[!Count!]=%%a"
)
Rem Display numbered Task Names
@for /L %%i in (1,1,%Count%) do (
	set "msg=[%%i] - !TaskName[%%i]!"
	echo !msg!
)
echo(
Rem Asking the user if he wants to delete or not the numbered task names
echo Type the number of the task to delete ! 
set /p "Input="
@for /L %%i in (1,1,%Count%) Do (
    If "%INPUT%" EQU "%%i" (
		schtasks /delete /tn "!TaskName[%%i]!"
	)
)
echo(
echo Type any key to show and delete another task !
Pause>nul
Goto Menu

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Batch file to show or delete a numbered task names

#2 Post by Squashman » 09 Nov 2017 18:05

Your problem is this line because you are using a space as a delimiter.

Code: Select all

@for /f "tokens=1 delims= " %%a in ('schtasks /query ^| find /I "%strTime%"') do (
Look at using the /FO csv option with the query. Then use the comma as the delimiter.

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Re: Batch file to show or delete a numbered task names

#3 Post by Hackoo » 09 Nov 2017 20:15

Thank you Squashman :wink: :lol:
You have put me on the right direction :idea:
And here is my modification :

Code: Select all

@echo off
Mode 100,3 & color 0A
Title Delete Tasks with their time tasks execution by Hackoo 2017
:Menu
Mode 100,3 & color 0A
cls & echo(
echo    Type the time with this format "hh:mm" or a task name to show or delete for scheduled Tasks
set /a "count=0"
set /p "strTime="
cls & echo( 
echo              Please wait a while ... looking for a scheduled Tasks for "%strTime%"
Setlocal EnableDelayedExpansion
@for /f "tokens=1 delims=," %%a in ('schtasks /query /fo csv ^| find /I "%strTime%"') do (
	set /a "Count+=1"
	set "TaskName[!Count!]=%%~a"
)
Rem Display numbered Task Names
Mode 90,30 & cls & echo( 
@for /L %%i in (1,1,%Count%) do (
	set "Task=[%%i] - !TaskName[%%i]:~1!"
	echo !Task!
)

If not defined Task (
	Mode 90,3 & Color 0C
	echo(
	echo                     No Scheduled Tasks found with this criteria
	Timeout /T 3 /nobreak >nul & goto Menu
) 

echo(
Rem Asking user if he wants to delete or not the numbered task names
echo Type the number of the task to delete ! 
set /p "Input="
@for /L %%i in (1,1,%Count%) Do (
    If "%INPUT%" EQU "%%i" (
		schtasks /delete /tn "!TaskName[%%i]:~1!"
	)
)
echo(
echo Type any key to show and delete another task !
Pause>nul
Goto Menu

Post Reply