Windows Batch CMD-将文本提取到最后一个特殊字符
我正在尝试提取没有文件名的路径位置。例如,程序生成的日志文件将具有类似于以下内容的字符串:Windows Batch CMD-将文本提取到最后一个特殊字符,windows,for-loop,batch-file,cmd,command-line,Windows,For Loop,Batch File,Cmd,Command Line,我正在尝试提取没有文件名的路径位置。例如,程序生成的日志文件将具有类似于以下内容的字符串: 2021-03-24T13:34:15 - Processing: C:\Source\SubFolder1\SubFolder2\SubFolder3\File1-???????????.txt 我需要从日志文件中提取以下内容: 2021-03-24T13:34:15 - Processing: C:\Source\SubFolder1\SubFolder2\SubFolder3\File1-????
2021-03-24T13:34:15 - Processing: C:\Source\SubFolder1\SubFolder2\SubFolder3\File1-???????????.txt
我需要从日志文件中提取以下内容:
2021-03-24T13:34:15 - Processing: C:\Source\SubFolder1\SubFolder2\SubFolder3\File1-???????????.txt
C:\Source\SubFolder1\SubFolder2\SubFolder3
到目前为止,我能够获得:
C:\Source\SubFolder1\SubFolder2\SubFolder3\File1-??.txt”
使用以下代码:
for /f "delims=" %%a in ('^<"C:\Source0\sample.txt" find "Processing:"') do set _path="%%a"
set _path_=%_path:~35%
echo %_path_%
for/f“delims=“%%a in(”^也许类似的东西适合您:
在以下情况下:
对于/F“Tokens=3,*”%G在(“%SystemRoot%\System32\find.exe”处理中:“0^我很高兴您有了可以工作的东西。另一种方法是使用受支持的Windows系统上已经存在的PowerShell。这看起来很长,但如果不仔细检查日期和时间,可能会减少
SET "THESTRING=2021-03-24T13:34:15 - Processing: C:\Source\SubFolder1\SubFolder2\SubFolder3\File1-???????????.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}T\d{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}T\d{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:\Source\SubFolder1\SubFolder2\SubFolder3\File1-???????????.txt"
$Part = if ('%TheString%' -match '\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2} - Processing: .*\\(.*)\.txt.*') { "$Matches[1]" }
顺便说一句,我只计算了34个字符,从字符串的开头省略,尽管我下面的答案没有进行任何计算。