looping thru schedule task

K

Kok Yong Lee

Hi there,

I am writting a bat file to loop through all the scheduled tasks on my pc, so that I can change its properties, but I am having some problem in retrieving existing schedule task name using "for /f"

c:\junk>schtasks /query /nh /fo CSV

"check","13:00:00, 08/07/2006",""
"CopyInv","23:15:00, 04/07/2006","Could not start"
"daily_test","17:55:00, 03/07/2006",""
"job","Never",""
"mybackup","06:59:00, 04/07/2006","Could not start"
"start_test","Never",""
"start_version","17:40:00, 09/07/2006","Could not start"
"weekend_test","16:30:00, 07/07/2006","Could not start"


in my batch file I have


call :TASK_NAME
:TASK_NAME
for /f "tokens=1-3 delims=," %%f in ('schtasks /query /nh /fo CSV') do (
set name_=%%~f
set next_run_time_=%%~g
set status=%%~h
echo %name_%
)
goto :END
:END

when I ran the batch file, I get the following out put

c:\junk>for /F "tokens=1-3 delims=," %f in ('schtasks /query /nh /fo CSV') do (
set name_=%~f
set next_run_time_=%~g
set status=%~h
echo weekend_test
)

c:\junk>(
set name_=check
set next_run_time_=13:00:00
set status= 08/07/2006"
echo weekend_test
)
weekend_test

c:\junk>(
set name_=CopyInv
set next_run_time_=23:15:00
set status= 04/07/2006"
echo weekend_test
)
weekend_test

c:\junk>(
set name_=daily_test
set next_run_time_=17:55:00
set status= 03/07/2006"
echo weekend_test
)
weekend_test

c:\junk>(
set name_=job
set next_run_time_=Never
set status=
echo weekend_test
)
weekend_test
.....

The problem is the line "echo %name_%" always has the value of "weekend_test". what was I missing here?

thanks.
 
J

Jerold Schulman

I have revised your script using EnableDelayedExpansion:

@echo off
setlocal EnableDelayedExpansion
call :TASK_NAME
endlocal
goto :EOF
:TASK_NAME
for /f "tokens=1-3 delims=," %%f in ('schtasks /query /nh /fo CSV') do (
set name_=%%~f
set next_run_time_=%%~g
set status=%%~h
echo !name_! !next_run_time_! !status!
)



Hi there,

I am writting a bat file to loop through all the scheduled tasks on my pc, so that I can change its properties, but I am having some problem in retrieving existing schedule task name using "for /f"

c:\junk>schtasks /query /nh /fo CSV

"check","13:00:00, 08/07/2006",""
"CopyInv","23:15:00, 04/07/2006","Could not start"
"daily_test","17:55:00, 03/07/2006",""
"job","Never",""
"mybackup","06:59:00, 04/07/2006","Could not start"
"start_test","Never",""
"start_version","17:40:00, 09/07/2006","Could not start"
"weekend_test","16:30:00, 07/07/2006","Could not start"


in my batch file I have


call :TASK_NAME
:TASK_NAME
for /f "tokens=1-3 delims=," %%f in ('schtasks /query /nh /fo CSV') do (
set name_=%%~f
set next_run_time_=%%~g
set status=%%~h
echo %name_%
)
goto :END
:END

when I ran the batch file, I get the following out put

c:\junk>for /F "tokens=1-3 delims=," %f in ('schtasks /query /nh /fo CSV') do (
set name_=%~f
set next_run_time_=%~g
set status=%~h
echo weekend_test
)

c:\junk>(
set name_=check
set next_run_time_=13:00:00
set status= 08/07/2006"
echo weekend_test
)
weekend_test

c:\junk>(
set name_=CopyInv
set next_run_time_=23:15:00
set status= 04/07/2006"
echo weekend_test
)
weekend_test

c:\junk>(
set name_=daily_test
set next_run_time_=17:55:00
set status= 03/07/2006"
echo weekend_test
)
weekend_test

c:\junk>(
set name_=job
set next_run_time_=Never
set status=
echo weekend_test
)
weekend_test
....

The problem is the line "echo %name_%" always has the value of "weekend_test". what was I missing here?

thanks.

Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
http://www.jsifaq.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top