问题
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