Import all schtasks .xml files inside the current directory via batch

核能气质少年 提交于 2019-12-25 09:12:11

问题


I know it's possible to import multiple tasks like this:

schtasks.exe /create /TN "Task 1 Name" /XML "Full_Path_of_Backup_XML_File"
schtasks.exe /create /TN "Task 2 Name" /XML "Full_Path_of_Backup_XML_File"
schtasks.exe /create /TN "Task 3 Name" /XML "Full_Path_of_Backup_XML_File"

But I'd prefer a more dynamic way to import all schtasks .xml files inside the current directory. Much like this batch here (.xml instead of .reg):

rem @echo off
cls
SET folder=%~dp0
for %%i in ("%folder%*.reg") do (regedit /s "%%i")
echo done
pause
EXIT

Can someone guide me to the right direction? :)


回答1:


Do you mean that you want to iterate through every XML file that is located in the same directory as the batch-file script? And then create a scheduled task with every XML file that was found? If that is the case you could try something like this (assuming the XML files are valid to create scheduled tasks with):

@echo off

setlocal enabledelayedexpansion

for %%e in ("%~dp0*.xml") do (
  set /a "count+=1"
  schtasks /create /tn "Task !count! Name" /xml "%%~e"
)

If you need to be able to pass XML file names that contain exclamation marks to the schtasks command-line tool, use the following code instead:

@echo off

setlocal disabledelayedexpansion
set "count=1"

for %%e in ("%~dp0*.xml") do (
  set "file=%%~e"
  setlocal enabledelayedexpansion
  schtasks /create /tn "Task !count! Name" /xml "!file!"
  endlocal & set /a "count+=1"
)

Still one question, how to name the tasks like their .xml filenames?

This will actually simplify things because there is no need to expand a separate variable within the for-loop body during execution time:

@echo off

setlocal disabledelayedexpansion

for %%e in ("%~dp0*.xml") do (
  schtasks /create /tn "%%~ne" /xml "%%~e"
)


来源:https://stackoverflow.com/questions/45079375/import-all-schtasks-xml-files-inside-the-current-directory-via-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!