1

I'm trying to create a batch script to import all tasks to Windows Task Scheduler including their folders (like OfficeSoftwareProtectionPlatform\SvcRestartTask, Microsoft\Microsoft Antimalware\Microsoft Antimalware Scheduled Scan). The last parts in the examples are file names. The task files do not have extensions.

I found this script to import all files from the current directory:

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

But I need something like this:

for /r %%e in (*) do (
schtasks /create /tn "%%~ne" /xml "%%[subfolder_tree]\%%~ne"
)

The first problem is that /r doesn't exclude the parent folder from the loop. Secondly, I have no idea how to make the name of the task start with the chain of subfolders starting from the first level in the current folder. I've beein googling for the whole day to no avail. The batch tutorials are above my comprehension level.

The desired output for OfficeSoftwareProtectionPlatform\SvcRestartTask is:

  1. SvcRestartTask
  2. OfficeSoftwareProtectionPlatform

OR just OfficeSoftwareProtectionPlatform\SvcRestartTask

The desired output for Microsoft\Microsoft Antimalware\Microsoft Antimalware Scheduled Scan is:

  1. Microsoft Antimalware Scheduled Scan
  2. Microsoft\Microsoft Antimalware

OR just Microsoft\Microsoft Antimalware\Microsoft Antimalware Scheduled Scan

But the parent folder with the batch file also contains files and those must be ignored.

DIR:

J:\Tasks\AAct
J:\Tasks\Adobe Acrobat Update Task
J:\Tasks\Adobe Flash Player NPAPI Notifier
J:\Tasks\Adobe Flash Player Updater
J:\Tasks\AdobeGCInvoker-1.0-Erik-PC-Erik
J:\Tasks\Anvirlauncher
J:\Tasks\AutoVPN
J:\Tasks\CCleanerSkipUAC
J:\Tasks\dir.txt
J:\Tasks\GoogleUpdateTaskMachineCore
J:\Tasks\GoogleUpdateTaskMachineUA
J:\Tasks\GoogleUpdateTaskUserS-1-5-21-2007260307-1439470342-1577740330-1000Core1d4973d1c6a7ff0
J:\Tasks\GoogleUpdateTaskUserS-1-5-21-2007260307-1439470342-1577740330-1000UA1d4973d1c7282fb
J:\Tasks\import.bat
J:\Tasks\RegOrganizerQuickLaunch
J:\Tasks\RunAsStdUser Task
J:\Tasks\SidebarExecute
J:\Tasks\Treexy Driver Fusion
J:\Tasks\Tweaking
J:\Tasks\ViewPower
J:\Tasks\{119181EB-20B0-4C08-9EE4-ED35854713D9}
J:\Tasks\{1AFFD6B4-18F3-484B-9EF8-01421B4EC8D3}
J:\Tasks\{1E586D66-286C-4FEC-8CA7-04A6AB259139}
J:\Tasks\{3EFC349B-A5D5-4CDB-805C-92E38EDB3AED}
J:\Tasks\{66B60342-E559-4843-AD66-B978A2ED7AF0}
J:\Tasks\{6CA70EC5-CCFC-4C88-B411-83DC7977EB47}
J:\Tasks\{A4DC2987-F94D-4442-B1AB-8CF48FEB860D}
J:\Tasks\{A841007C-2525-49F1-A1E8-F1ECB191852B}
J:\Tasks\{A8EB447F-6D62-418B-9265-DE3DD343BE36}
J:\Tasks\{BB2AECA2-3DAC-4B26-864A-81044AA655D2}
J:\Tasks\{BBA84D6B-AD68-4B06-9599-2DC0EB3A4976}
J:\Tasks\{C348D38C-7969-4453-8DBA-ACE7300EFCC1}
J:\Tasks\{D95F1270-1461-4AA0-B477-F86D533923D4}
J:\Tasks\{E0EFB471-92C7-47BA-991F-AC55ABCB36EF}
J:\Tasks\{EE62020D-3B06-4FC1-A582-B347C00C0C58}
J:\Tasks\{F2AEA727-A62E-4FEF-92B5-604A7AA117D4}
J:\Tasks\{F6F404A4-CA9E-4E2D-A556-F8CE07D3FA23}
J:\Tasks\{FD420BB9-0F4A-4218-ACA2-7AC1CB2B5ECE}
J:\Tasks\DelayedItemsByChemtableSoftware\AdobeAAMUpdater-1.0
J:\Tasks\DelayedItemsByChemtableSoftware\Google Update
J:\Tasks\DelayedItemsByChemtableSoftware\QuickTime Task
J:\Tasks\Games\UpdateCheck_S-1-5-21-2007260307-1439470342-1577740330-1000
J:\Tasks\Microsoft\Microsoft Antimalware\Microsoft Antimalware Scheduled Scan
J:\Tasks\Microsoft\Microsoft Antimalware\MpIdleTask
J:\Tasks\Microsoft\Office\Office 15 Subscription Heartbeat
J:\Tasks\Microsoft\Office\OfficeTelemetryAgentFallBack
J:\Tasks\Microsoft\Office\OfficeTelemetryAgentLogOn
J:\Tasks\Microsoft\Windows\Active Directory Rights Management Services Client\AD RMS Rights Policy Template Management (Automated)
J:\Tasks\Microsoft\Windows\Active Directory Rights Management Services Client\AD RMS Rights Policy Template Management (Manual)
J:\Tasks\Microsoft\Windows\AppID\PolicyConverter
J:\Tasks\Microsoft\Windows\AppID\VerifiedPublisherCertStoreCheck
J:\Tasks\Microsoft\Windows\Application Experience\Microsoft Compatibility Appraiser
J:\Tasks\Microsoft\Windows\Application Experience\ProgramDataUpdater
J:\Tasks\Microsoft\Windows\Bluetooth\UninstallDeviceTask
J:\Tasks\Microsoft\Windows\CertificateServicesClient\SystemTask
J:\Tasks\Microsoft\Windows\CertificateServicesClient\UserTask
J:\Tasks\Microsoft\Windows\CertificateServicesClient\UserTask-Roam
J:\Tasks\Microsoft\Windows\Diagnosis\Scheduled
J:\Tasks\Microsoft\Windows\DiskDiagnostic\Microsoft-Windows-DiskDiagnosticResolver
J:\Tasks\Microsoft\Windows\Location\Notifications
J:\Tasks\Microsoft\Windows\MemoryDiagnostic\CorruptionDetector
J:\Tasks\Microsoft\Windows\MemoryDiagnostic\DecompressionFailureDetector
J:\Tasks\Microsoft\Windows\MobilePC\HotStart
J:\Tasks\Microsoft\Windows\MUI\LPRemove
J:\Tasks\Microsoft\Windows\Multimedia\SystemSoundsService
J:\Tasks\Microsoft\Windows\NetTrace\GatherNetworkInfo
J:\Tasks\Microsoft\Windows\Offline Files\Background Synchronization
J:\Tasks\Microsoft\Windows\Offline Files\Logon Synchronization
J:\Tasks\Microsoft\Windows\PerfTrack\BackgroundConfigSurveyor
J:\Tasks\Microsoft\Windows\Power Efficiency Diagnostics\AnalyzeSystem
J:\Tasks\Microsoft\Windows\RAC\RacTask
J:\Tasks\Microsoft\Windows\Ras\MobilityManager
J:\Tasks\Microsoft\Windows\Registry\RegIdleBackup
J:\Tasks\Microsoft\Windows\RemoteAssistance\RemoteAssistanceTask
J:\Tasks\Microsoft\Windows\Shell\WindowsParentalControls
J:\Tasks\Microsoft\Windows\Shell\WindowsParentalControlsMigration
J:\Tasks\Microsoft\Windows\SideShow\AutoWake
J:\Tasks\Microsoft\Windows\SideShow\GadgetManager
J:\Tasks\Microsoft\Windows\SideShow\SessionAgent
J:\Tasks\Microsoft\Windows\SideShow\SystemDataProviders
J:\Tasks\Microsoft\Windows\SoftwareProtectionPlatform\SvcRestartTask
J:\Tasks\Microsoft\Windows\SystemRestore\SR
J:\Tasks\Microsoft\Windows\Task Manager\Interactive
J:\Tasks\Microsoft\Windows\Tcpip\IpAddressConflict1
J:\Tasks\Microsoft\Windows\Tcpip\IpAddressConflict2
J:\Tasks\Microsoft\Windows\TextServicesFramework\MsCtfMonitor
J:\Tasks\Microsoft\Windows\Time Synchronization\SynchronizeTime
J:\Tasks\Microsoft\Windows\UPnP\UPnPHostConfig
J:\Tasks\Microsoft\Windows\User Profile Service\HiveUploadTask
J:\Tasks\Microsoft\Windows\WDI\ResolutionHost
J:\Tasks\Microsoft\Windows\Windows Activation Technologies\ValidationTask
J:\Tasks\Microsoft\Windows\Windows Activation Technologies\ValidationTaskDeadline
J:\Tasks\Microsoft\Windows\Windows Error Reporting\QueueReporting
J:\Tasks\Microsoft\Windows\Windows Filtering Platform\BfeOnServiceStartTypeChange
J:\Tasks\Microsoft\Windows\WindowsBackup\ConfigNotification
J:\Tasks\Microsoft\Windows\WindowsColorSystem\Calibration Loader
J:\Tasks\Microsoft\Windows\Wininet\CacheTask
J:\Tasks\OfficeSoftwareProtectionPlatform\SvcRestartTask

MUST NOT BE INCLUDED (basically files from the parent dir):

J:\Tasks\AAct
J:\Tasks\Adobe Acrobat Update Task
J:\Tasks\Adobe Flash Player NPAPI Notifier
J:\Tasks\Adobe Flash Player Updater
J:\Tasks\AdobeGCInvoker-1.0-Erik-PC-Erik
J:\Tasks\Anvirlauncher
J:\Tasks\AutoVPN
J:\Tasks\CCleanerSkipUAC
J:\Tasks\dir.txt
J:\Tasks\GoogleUpdateTaskMachineCore
J:\Tasks\GoogleUpdateTaskMachineUA
J:\Tasks\GoogleUpdateTaskUserS-1-5-21-2007260307-1439470342-1577740330-1000Core1d4973d1c6a7ff0
J:\Tasks\GoogleUpdateTaskUserS-1-5-21-2007260307-1439470342-1577740330-1000UA1d4973d1c7282fb
J:\Tasks\import.bat
J:\Tasks\RegOrganizerQuickLaunch
J:\Tasks\RunAsStdUser Task
J:\Tasks\SidebarExecute
J:\Tasks\Treexy Driver Fusion
J:\Tasks\Tweaking
J:\Tasks\ViewPower
J:\Tasks\{119181EB-20B0-4C08-9EE4-ED35854713D9}
J:\Tasks\{1AFFD6B4-18F3-484B-9EF8-01421B4EC8D3}
J:\Tasks\{1E586D66-286C-4FEC-8CA7-04A6AB259139}
J:\Tasks\{3EFC349B-A5D5-4CDB-805C-92E38EDB3AED}
J:\Tasks\{66B60342-E559-4843-AD66-B978A2ED7AF0}
J:\Tasks\{6CA70EC5-CCFC-4C88-B411-83DC7977EB47}
J:\Tasks\{A4DC2987-F94D-4442-B1AB-8CF48FEB860D}
J:\Tasks\{A841007C-2525-49F1-A1E8-F1ECB191852B}
J:\Tasks\{A8EB447F-6D62-418B-9265-DE3DD343BE36}
J:\Tasks\{BB2AECA2-3DAC-4B26-864A-81044AA655D2}
J:\Tasks\{BBA84D6B-AD68-4B06-9599-2DC0EB3A4976}
J:\Tasks\{C348D38C-7969-4453-8DBA-ACE7300EFCC1}
J:\Tasks\{D95F1270-1461-4AA0-B477-F86D533923D4}
J:\Tasks\{E0EFB471-92C7-47BA-991F-AC55ABCB36EF}
J:\Tasks\{EE62020D-3B06-4FC1-A582-B347C00C0C58}
J:\Tasks\{F2AEA727-A62E-4FEF-92B5-604A7AA117D4}
J:\Tasks\{F6F404A4-CA9E-4E2D-A556-F8CE07D3FA23}
J:\Tasks\{FD420BB9-0F4A-4218-ACA2-7AC1CB2B5ECE}

Please note that the last part of the path in all cases is the file name, as they are extensionless.

1

I'm trying to create a batch script to import all tasks to Windows Task Scheduler

Use the following batch file (test.cmd):

@echo off
setlocal EnableDelayedExpansion
rem process all files
for /f "tokens=*" %%f in ('dir /b /s /a:-d') do (
  set "_dir=%%~dpf"
  rem exclude files in the directory containing the batch file (%cd%)
  if not [%cd%\] equ [!_dir!] (
    rem remove remove drive and parent directories
    set "_dir=!_dir:%cd%=!"
    rem remove leading and trailing \
    set "_dir=!_dir:~1,-1!"
    set "_file=%%~nxf"
    echo schtasks /create /tn "!cd!\!_dir!\!_file!" /xml "!_dir!\!_file!"
    )
  )
endlocal

Notes:

  • Excludes all files in the directory where the batch file is located.
  • Remove echo when you are happy with the schtasks command that will be executed.

Example output:

> dir /b /s
F:\test\test\DelayedItemsByChemtableSoftware
F:\test\test\Microsoft
F:\test\test\OfficeSoftwareProtectionPlatform
F:\test\test\test
F:\test\test\test.cmd
F:\test\test\DelayedItemsByChemtableSoftware\AdobeAAMUpdater-1.0
F:\test\test\Microsoft\Microsoft Antimalware
F:\test\test\Microsoft\Microsoft Antimalware\Microsoft Antimalware Scheduled Scan
F:\test\test\OfficeSoftwareProtectionPlatform\SvcRestartTask

> test
schtasks /create /tn "F:\test\test\DelayedItemsByChemtableSoftware\AdobeAAMUpdater-1.0" /xml "DelayedItemsByChemtableSoftware\AdobeAAMUpdater-1.0"
schtasks /create /tn "F:\test\test\Microsoft\Microsoft Antimalware\Microsoft Antimalware Scheduled Scan" /xml "Microsoft\Microsoft Antimalware\Microsoft Antimalware Scheduled Scan"
schtasks /create /tn "F:\test\test\OfficeSoftwareProtectionPlatform\SvcRestartTask" /xml "OfficeSoftwareProtectionPlatform\SvcRestartTask"
>

  • Thank you very much for your time and help. I accepted the answer, but it doesn't work on files containing . in the name. It cuts out the part after . If it's not too much to ask, could you provide a fix for that too? – COOLak Mar 13 at 15:29
  • @COOLak That's because you said "The task files do not have extensions.". Anyway it's fixed. – DavidPostill Mar 13 at 15:32
  • Thanks. They indeed don't have extensions, but they are named after tasks from Task Scheduler, and some tasks can happen to contain a dot in their names. Thank you again. – COOLak Mar 13 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.