Windows批处理文件,在标签之间找到一个字符串

标签 windows batch-file cmd

我需要在文件中搜索第一次出现的字符串。

我有这个 php 文件:

<?php

#---------------------------------------------------------------------------------------
#
#
#  Gpl V2 and further license updates applies to this code
#
#  2015/05/21
#
#-------------------------------------------------------------------------------

error_reporting(0);

$version = '1.0 Alpha';

//some code
echo "\r\n===== ".$argv[0]." ".$version.": Select a channel =====: ";

我需要找到这个值 1.0 Alpha 。我在一个 bat 文件中写了这段代码。

for /f "tokens=*" %%x in ('findstr "$version" %~1') do (
set str=%%x
)

但他找到了所有第二个字符串: echo "\r\n===== ".$argv[0]."".$version.": Select a channel =====: ";

解决方案?

最后一件事,脚本可以从 $version = '1.0 Alpha'; 更改为 $version = "1.0 Alpha";

报价可能会改变。

最佳答案

您需要在找到第一次出现的字符串后退出循环。试试这个:

for /f "tokens=*" %%x in ('findstr "$version" %~1') do (
   set str=%%x
   goto :EndFor
)
:EndFor

set str=%str:"='% 
for /f "tokens=2 delims='" %%x in ('echo(%str%') do set str=%%x
echo(str=%str%
pause

关于Windows批处理文件,在标签之间找到一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31322020/

相关文章:

java - 将字符串从 EBCDIC 转换为 Unicode/UTF8

c - 如何通过管理代码使用 Windows native dll?

windows - 解压所有子文件夹中的所有 zip 文件

c++ - SDL程序可以自己运行,不能通过命令行运行

windows - 如何在 windows 批处理文件或 linux shell 脚本中传递超过 10 个参数

windows - 批处理文件 : assign decimal value to variable

c++ - 从 dll 中获取 DLL 的名称

windows - 使用可变数量的参数调用批处理文件

batch-file - 批处理文件错误: "cant be processed syntactically at this point"

windows - 从 Windows 命令 (CMD) 运行 SPSS