如何从文本文件中读取不同的内容

时间:2019-04-26 20:17:04

标签: 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}
      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}

6 个答案:

答案 0 :(得分:1)

如果您不愿意为此使用另一种内置脚本语言,并且您要读取的文件使用Windows标准行尾,那么我有另一种想法。

您可以搜索ID: {…,而不是直接搜索<Anything><Carriage Return><Line Feed><Space(s)><Password:>的行。 您可以根据需要进一步完善<Anything>,但是在这种情况下,我认为这不是必需的

@Echo Off
SetLocal DisableDelayedExpansion
Set "Src=input.txt"
Set "Str=Password:"
Set "ID="
(Set LF=^
% 0x0A %
)
For /F %%A In ('Copy /Z "%~f0" Nul')Do Set "CR=%%A"
SetLocal EnableDelayedExpansion
FindStr /RC:".*!CR!*!LF! *%Str%" "%Src%">"%TEMP%\_$.tmp"
EndLocal
For /F "UseBackTokens=2Delims={}" %%A In ("%TEMP%\_$.tmp")Do Set "ID={%%A}"
Del "%TEMP%\_$.tmp"
If Not Defined ID GoTo :EOF
manage-bde -protectors -adbackup c: -id %ID%

在上面的示例中,我使用input.txt作为您正在阅读的文本文件的名称,请根据需要进行修改。

答案 1 :(得分:1)

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,然后显示所需的命令行,重新应用{{1} }

答案 2 :(得分:1)

利用这一事实的纯批处理脚本,您在“触发行”(Numerical Password:)之后立即寻找密钥

@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%

答案 3 :(得分:0)

也许使用windows script来处理任务?可以使用if (.not. present(optionalArg)) then optionalArg_local = 2 end if 直接执行此操作,也可以使用import { Component, ViewChild } from '@angular/core'; import { Platform, NavController } from 'ionic-angular'; import { StatusBar } from '@ionic-native/status-bar'; import { SplashScreen } from '@ionic-native/splash-screen'; import { Storage } from '@ionic/Storage'; import { HomePage } from '../pages/home/home'; import { LoginPage } from '../pages/login/login'; import { RegisterPage } from '../pages/register/register'; @Component({ templateUrl: 'app.html' }) export class MyApp { _platform: Platform; public get platform(): Platform { return this._platform; } public set platform(value: Platform) { this._platform = value; } @ViewChild('content') nav: NavController; rootPage: any; initialization: any; initializationApp: any; Platform: any; statusBar: any; splashScreen: any; Storage: any; constructor(public platform1: Platform, public StatusBar: StatusBar, public SplashScreen: SplashScreen, private storage: Storage) { this.initializeApp(); } initializeApp() { this.platform.ready().then(() => { this.statusBar.styleDefault(); this.splashScreen.hide(); }); this.storage.get('session_storage').then((res) => { if (res == null) { this.rootPage = LoginPage; } else { this.rootPage = HomePage; } }); } } 从批处理文件中执行:

wscript

答案 4 :(得分:0)

使用正则表达式和FindStr,您也许可以得到想要的东西。部分解决方案,因为我比您更懒。 ;)

c:\projects>findstr {[0-9A-F/-]*}  data.txt
  ID: {31116007-D1FB-43CC-A89C-927487BD5F00}
  ID: {C7948F32-F2F0-4E55-AD2E-06E982DFDB4F}
  ID: {6497FF93-F678-4317-B5D7-423E9D682BF0}

答案 5 :(得分:0)

编辑:我忽略了all IDs并根据示例行创建了一个解决方案

以下PowerShell一号班轮将从文本文件.\BitLocker.txt中提取ID

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%