循环遍历除父文件夹外各级子文件夹中的文件,获取它们的部分路径和名称

循环遍历除父文件夹外各级子文件夹中的文件,获取它们的部分路径和名称

我正在尝试创建一个批处理脚本,将所有任务(包括其文件夹)导入 Windows 任务计划程序(例如OfficeSoftwareProtectionPlatform\SvcRestartTaskMicrosoft\Microsoft Antimalware\Microsoft Antimalware Scheduled Scan)。示例中的最后部分是文件名。任务文件没有扩展名。

我发现这个脚本可以从当前目录导入所有文件:

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

但我需要这样的东西:

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

第一个问题是它/r没有将父文件夹从循环中排除。其次,我不知道如何让任务名称以当前文件夹中从第一级开始的子文件夹链开头。我在谷歌上搜索了一整天,但无济于事。批处理教程超出了我的理解水平。

所需的输出OfficeSoftwareProtectionPlatform\SvcRestartTask是:

  1. SvcRestartTask
  2. OfficeSoftwareProtectionPlatform

要不就OfficeSoftwareProtectionPlatform\SvcRestartTask

所需的输出Microsoft\Microsoft Antimalware\Microsoft Antimalware Scheduled Scan是:

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

要不就 Microsoft\Microsoft Antimalware\Microsoft Antimalware Scheduled Scan

但是批处理文件的父文件夹也包含文件,必须忽略这些文件。

总监:

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

一定不能包含(基本上是来自父目录的文件):

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}

请注意,在所有情况下,路径的最后一部分都是文件名,因为它们没有扩展名。

答案1

我正在尝试创建一个批处理脚本,将所有任务导入到 Windows 任务计划程序

使用以下批处理文件(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

笔记:

  • 排除批处理文件所在目录中的所有文件。
  • 当您对将要执行的命令echo满意时,请删除。schtasks

示例输出:

> 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"
>

相关内容