Batch file 如何查找和替换包含保留字符和空格的行

Batch file 如何查找和替换包含保留字符和空格的行,batch-file,passwords,complexity-theory,Batch File,Passwords,Complexity Theory,我是新来的。 我正在尝试使用批处理编辑文件中的一行。 但是,该行包含空格和保留字符(符号=) 基本上,我想将PasswordComplexity=1更改为PasswordComplexity=0,它位于名为export.cfg 感谢所有想法,以下是我现在拥有的: setlocal enabledelayedexpansion FOR /F "usebackq delims=" %%a IN ("export.cfg") DO ( set "line=%%a" & echo !line:

我是新来的。 我正在尝试使用批处理编辑文件中的一行。 但是,该行包含空格和保留字符(符号
=
) 基本上,我想将
PasswordComplexity=1
更改为
PasswordComplexity=0
,它位于名为
export.cfg
感谢所有想法,以下是我现在拥有的:

setlocal enabledelayedexpansion

FOR /F "usebackq delims=" %%a IN ("export.cfg") DO (
set "line=%%a" & echo !line:(PasswordComplexity = 1)=(PasswordComplexity = 0)!
)>>"import.cfg"

不幸的是,等号
=
不能仅用本机Windows解决方案取代。 看

如果包含PasswordComplexity的行只有一位数,则可以执行以下操作:

setlocal EnableExtensions EnableDelayedExpansion

for /f "usebackq delims=" %%A in ("export.cfg") do (
    set "line=%%A"
    set "found=false"
    for /f "delims=" %%B in (echo %%A^|find "PasswordComplexity = 1") do set "found=true"
    if !found!==true (
        echo !line:1=0!
    ) else (
        echo !line!
    )
)>>"import.cfg"

endlocal

还请注意,无论何时使用引号外的特殊字符,都必须对其进行转义。请参见

不幸的是,等号
=
不能仅用本机Windows解决方案取代。 看

如果包含PasswordComplexity的行只有一位数,则可以执行以下操作:

setlocal EnableExtensions EnableDelayedExpansion

for /f "usebackq delims=" %%A in ("export.cfg") do (
    set "line=%%A"
    set "found=false"
    for /f "delims=" %%B in (echo %%A^|find "PasswordComplexity = 1") do set "found=true"
    if !found!==true (
        echo !line:1=0!
    ) else (
        echo !line!
    )
)>>"import.cfg"

endlocal

还请注意,无论何时使用引号外的特殊字符,都必须对其进行转义。请参见

不错的尝试,但错了!开括号不需要转义,闭括号需要转义,但在repalce表达式中不能转义等号。等号不能用这种方法代替。很好的尝试,但错了!开括号不需要转义,闭括号需要转义,但在repalce表达式中不能转义等号。等号不能用这种方法代替