Batch CMD-提取文本直到最后一个特殊字符



我正在尝试提取一个没有文件名的路径位置。例如,程序生成的日志文件将具有类似以下的字符串:

2021-03-24T13:34:15 - Processing: C:SourceSubFolder1SubFolder2SubFolder3File1-???????????.txt

我需要从日志文件中提取以下内容:

C:\Source\SubFolder1\SubFolder2\SubFolder3

到目前为止,我能够获得:

C:\Source\SubFolder1\SubFolder2\SubFolder3\File1-???????????????????????。txt";

使用以下代码:

for /f "delims=" %%a in ('^<"C:Source0sample.txt" find "Processing:"') do set _path="%%a"
set _path_=%_path:~35%
echo %_path_%

请帮助我展示如何省略文件1*?????字符串的一部分

编辑:文件1-?????该部分的字符长度是不固定的,可以在级别上进一步下降的子导演也是不固定的。唯一固定的部分是字符串的前35个字符,我使用%_path省略了这些字符:~35%。

也许这样的东西对你有用:

在cmd中作为命令行:

For /F "Tokens=3,*" %G In ('%SystemRoot%System32find.exe "Processing:" 0^<"C:Source0sample.txt"') Do @For %I In ("%~dpH.") Do @Echo %~dpnxI

或者作为批处理文件中的命令行:

@For /F "Tokens=3,*" %%G In ('%SystemRoot%System32find.exe "Processing:" 0^<"C:Source0sample.txt"') Do @For %%I In ("%%~dpH.") Do @Echo %%~dpnxI

我很高兴你有一些可以工作的东西。另一种方法是使用已经在您支持的Windows系统上的PowerShell。这看起来很长,但如果不仔细检查日期和时间,可能会减少。

SET "THESTRING=2021-03-24T13:34:15 - Processing: C:SourceSubFolder1SubFolder2SubFolder3File1-???????????.txt"
REM === Find the full file name
SET "PART=no match"
FOR /F "delims=" %%A IN ('powershell -NoLogo -NoProfile -Command ^
"if ('%THESTRING%' -match 'd{4}-d{2}-d{2}Td{2}:d{2}:d{2} - Processing: .*\(.*).txt.*') {" ^
"$Matches[1]"}') DO (SET "PART=%%~A")
ECHO PART IS ===%PART%===
REM === Find the part of the name after the HYPHEN character
SET "PART=no match"
FOR /F "delims=" %%A IN ('powershell -NoLogo -NoProfile -Command ^
"if ('%THESTRING%' -match 'd{4}-d{2}-d{2}Td{2}:d{2}:d{2} - Processing: .*\.*-(.*).txt.*') {" ^
"$Matches[1]"}') DO (SET "PART=%%~A")
ECHO PART IS ===%PART%===

如果不在CMD.EXE.中运行它会更容易

$TheSting=2021-03-24T13:34:15 - Processing: C:SourceSubFolder1SubFolder2SubFolder3File1-???????????.txt"
$Part = if ('%TheString%' -match 'd{4}-d{2}-d{2}Td{2}:d{2}:d{2} - Processing: .*\(.*).txt.*') { "$Matches[1]" }

最新更新