Batch file 如何从文本文件中读取不同的内容
我需要从如下文件中读取ID:Batch file 如何从文本文件中读取不同的内容,batch-file,Batch File,我需要从如下文件中读取ID: BitLocker Drive Encryption: Configuration Tool version 10.0.16299 Copyright (C) 2013 Microsoft Corporation. All rights reserved. Volume C: [OSDisk] All Key Protectors External Key: ID: {31116007-D1FB-43CC-A89C-927487BD5F00}
BitLocker Drive Encryption: Configuration Tool version 10.0.16299
Copyright (C) 2013 Microsoft Corporation. All rights reserved.
Volume C: [OSDisk]
All Key Protectors
External Key:
ID: {31116007-D1FB-43CC-A89C-927487BD5F00}
External Key File Name:
31116007-D1FB-43CC-A89C-927487BD5F00.BEK
Numerical Password:
ID: {C7948F32-F2F0-4E55-AD2E-06E982DFDB4F}
Password:
xxxx-xxxxx-xxxxx-xxxxxx-xxxxx-xxxx-xxxxx-xxx
TPM:
ID: {6497FF93-F678-4317-B5D7-423E9D682BF0}
PCR Validation Profile:
0, 2, 4, 8, 9, 10, 11
然后运行以下命令将所有ID的密钥导出到AD
manage-bde -protectors -adbackup c: -id {C7948F32-F2F0-4E55-AD2E-06E982DFDB4F}
也许用一个新的方法来处理这个任务?这可以使用wscript
直接执行,也可以使用cscript
从批处理文件执行:
cscript //nologo myscript.js
var fso = new ActiveXObject("Scripting.FileSystemObject");
var file = fso.OpenTextFile("C:\\myfile.txt", 1);
var content = file.readAll();
// Do something with file content
file.close();
通过正则表达式和FindStr,您可能会得到想要的结果。部分解决方案,因为我比你懒。;)
编辑:我忽略了
所有ID
,并基于示例行创建了一个解决方案
下面的PowerShell one liner将从文本文件\BitLocker.txt
PoSh> Select-String -Path .\BitLocker.txt -Pattern 'Numerical Password' -Context 0,1|ForEach-Object {($_.Context.Postcontext.Trim() -split ' ')[1]}
{C7948F32-F2F0-4E55-AD2E-06E982DFDB4F}
要批量包装主题,请执行以下操作:
@Echo off
For /f "usebackq delims=" %%A in (`
powershell -NoP -C "Select-String -Path .\BitLocker.txt -Pattern 'Numerical Password' -Context 0,1|% {($_.Context.Postcontext.Trim() -split ' ')[1]}"
`) Do Set "ID=%%A"
manage-bde -protectors -adbackup c: -id %ID%
如果您不愿意为此使用其他内置脚本语言,并且您要读取的文件使用Windows标准行结尾,我有另一个想法 您可以搜索
,而不是直接搜索ID为:{…的行。如果您觉得需要,您可以进一步优化
,但在这种情况下,我认为没有必要:
@Echo关闭
SetLocal DisableDelayedExpansion
设置“Src=input.txt”
设置“Str=密码:”
设置“ID=”
(左前)=^
%0x0A%
)
对于('Copy/Z'%~f0“Nul')中的/F%%A,请设置“CR=%%A”
SetLocal EnableDelayedExpansion
FindStr/RC:“.*!CR!*!LF!*%Str%”“%Src%”>%TEMP%\\ u$.tmp”
端部
对于(“%TEMP%\\ u$.tmp”)中的/F“UseBackTokens=2Delims={}”%%A,请设置“ID={%%A}”
删除“%TEMP%\\ u$.tmp”
如果未定义ID转到:EOF
管理bde-保护器-ADC备份:-id%id%
在上面的示例中,我使用了input.txt
作为您正在读取的文本文件的名称,请根据需要修改该名称
FOR /f "tokens=2delims={}" %%a IN ('findstr /x /r /c:" *ID: {.*}" "%filename1%"') DO ECHO manage-bde -protectors -adbackup c: -id {%%a}
其中filename1
包含您正在检查的文件名,并且命令正在执行echo
编辑。删除echo
关键字以执行manage bde
findstr
查找与模式[spaces]ID:{string}
完全匹配的行,并将{}
之间的字符串分配给%%a,然后显示所需的命令行,重新应用{}
纯批处理脚本,它利用了以下事实,即在“触发行”(数字密码:
)
你以前尝试过什么?这里的目标是什么?请回顾一下这个好问题。我们在
alt.msdos.batch
中有一个名为Todd
的通讯员,他最初在qbasic
中编写了每个解决方案,然后将qbasic
程序包装在几个批处理行中。他最终安顿下来并制作了一些有用的batch,但人们普遍认为,将批处理包装器应用于以另一种语言编写的解决方案并不是批处理解决方案。@Magoo,我理解您试图表达的观点,但在这种情况下,我建议LotPings只是利用默认的内置CLI可执行文件,就像他使用attrib
,choice,find
,findstr
,forfiles
,ipconfig
,mode
,more
,net
,ping
,robocopy
,runas
,sc
,setx
,taskkill
,任务列表
,超时树,where
,wscript
,xcopy
等等。唯一真正的区别是它不在%\uuuuuuuuu AppDir\uuuuuuuuu%
@Magoo中,这是一个多么简洁的故事;-)几年前我在amb/ambnt中很活跃(使用另一个ID)。你不是指Todd Vargo吗?我把OPs示例行看得太字面了,因为PowerShellsSelect String
有一个方便的-Context
参数,所以围绕这个参数构建了一个解决方案。虽然我的注意力分散在批处理和PwSh之间,但当涉及到完整的正则表达式处理时,我肯定偏向于PwSh。@LotPings:我做了大多数的验证我只是提一下。
FOR /f "tokens=2delims={}" %%a IN ('findstr /x /r /c:" *ID: {.*}" "%filename1%"') DO ECHO manage-bde -protectors -adbackup c: -id {%%a}
@echo off
setlocal
set "flag="
for /f "tokens=*" %%a in (t.txt) do (
if defined flag (for %%b in (%%a) do set "key=%%b") & goto :found
if "%%a" == "Numerical Password:" set "flag=yes"
)
echo not found & goto :eof
:found
echo %key%