文件A 的内容:
TaskName: \Microsoft\Windows\Work Folders\Work Folders Maintenance Work
TaskName: \Microsoft\Windows\Workplace Join\Automatic-Device-Join
TaskName: \Microsoft\XblGameSave\XblGameSaveTask
TaskName: \Microsoft\XblGameSave\XblGameSaveTaskLogon
文件B的内容:
Scheduled Task State: Enabled
Scheduled Task State: Disabled
Scheduled Task State: Enabled
Scheduled Task State: Enabled
在文件B中我想替换"已启用"使用"启用"和#34;已禁用"使用"禁用,"如下:
Scheduled Task State: Enable
Scheduled Task State: Disable
Scheduled Task State: Enable
Scheduled Task State: Enable
最后,我想获取文件A和文件B的值,并将以下字符串导出到文件C :
:: String format: schtasks /change /TN "FILE-A_VALUE in quotes" /FILE-B_VALUE
schtasks /change /TN "\Microsoft\Windows\Work Folders\Work Folders Maintenance Work" /Enable
schtasks /change /TN "\Microsoft\Windows\Workplace Join\Automatic-Device-Join" /Disable
schtasks /change /TN "\Microsoft\XblGameSave\XblGameSaveTask" /Enable
schtasks /change /TN "\Microsoft\XblGameSave\XblGameSaveTaskLogon" /Enable
这可以从批处理文件中获得吗?感谢您的帮助。
更新:
以下是用于在此过程中创建文件的命令的完整列表,其中包含用户Stephan的答案:
:: Complete commands to back up the current enabled/disabled state of all Windows scheduled tasks to a batch file; the outputted batch file will be in the form of schtasks commands to restore the enabled/disabled states. Note: all scheduled task values are language-dependent; as such the processes below are for English only.
:: Get list and parameters of all tasks; find values for TaskName, which includes full path and name of each scheduled tasks; save values to temp File A.
SCHTASKS /Query /FO LIST /v| findstr /r /C:"TaskName: " >fileA.txt
:: Re-get list and parameters of all tasks; find values for Scheduled Task State; save values to temp File B
SCHTASKS /Query /FO LIST /v| findstr /r /C:"Scheduled Task State: " >fileB.txt
:: Read both files in parallel using delayed expansion to set variables from both files
@echo off
setlocal enabledelayedexpansion
<fileB.txt (
:: Set variable for task name from fileA
for /f "delims=" %%a in (fileA.txt) do (
set "task=%%a"
set "task=!task:~38!
set /p "line="
:: Set variable for task state from fileB, and truncate line so values are changed from enabled/disabled to enable/disable
for %%A in ("!line:~38,-1!") do set "state=%%~A"
:: output task name and truncated task state in the local language needed for schtasks command to enable/disable tasks
:: Example: schtasks /change /TN "\Microsoft\Windows\Active Directory Rights Management Services Client\AD RMS Rights Policy Template Management (Automated)" /Disable
echo schtasks /change /TN "!task!" /!state!
)
) >fileC.bat
答案 0 :(得分:0)
有两个并行读取文件的技巧:
@echo off
setlocal enabledelayedexpansion
<fileB.txt (
for /f "delims=" %%a in (fileA.txt) do (
set "task=%%a"
set "task=!task:~38!
set /p "line="
for %%A in ("!line:~38,-1!") do set "state=%%~A"
echo schtasks /change /TN "!task!" /!state!
)
) >fileC.bat
注意:通过两个步骤读取任务名称和任务状态存在风险:任务可能会在这些步骤之间被删除或添加,这将导致非同步列表。
schtasks
输出也取决于语言
如果:~38
在所有系统上都可靠,我也怀疑
上面的代码适用于您的示例文件。