@echo offsetlocal enabledelayedexpansion set "search=^(\d+)\.(\d+)\.(\d+)$" set "replace=$1-0-$2-$3" set "file=version.cfg" for /f "delims=" %%i in ('type "%file%" ^& break ^> "%file%"') do ( set "line=%%i" echo !line! | findstr /r /c:"%search%" >nul && set "line=!line:%search%=%replace%!" >>"%file%" echo(!line!) )这个代码不能实现替换
时间: 2023-07-05 14:11:07 浏览: 41
根据你提供的代码,我发现可能是因为你在循环中对同一个文件进行读取和写入操作,导致出现了问题。可以尝试将读取和写入操作分开,例如:
```
@echo off
setlocal enabledelayedexpansion
set "search=^(\d+)\.(\d+)\.(\d+)$"
set "replace=$1-0-$2-$3"
set "file=version.cfg"
rem 读取文件并替换文本
for /f "delims=" %%i in ('type "%file%"') do (
set "line=%%i"
echo !line! | findstr /r /c:"%search%" >nul && set "line=!line:%search%=%replace%!"
echo(!line!
) > temp.txt
rem 将替换后的文本写入文件
move /y temp.txt "%file%"
```
这段代码将读取文件并替换文本后,将替换后的文本输出到临时文件 `temp.txt` 中,最后再将临时文件中的文本替换原文件。请注意,这里使用了 `move` 命令,而不是 `echo` 命令,以确保在替换文件时不会出现问题。
如果你仍然无法正确实现替换,请提供更多的上下文或错误信息,我将尽力帮助你解决问题。
相关问题
@echo off setlocal enabledelayedexpansion set input_file=test.txt set output_file=output.txt for /f "tokens=* delims=" %%a in ('type "%input_file%"') do ( set "line=%%a" set "line=!line:^ =!" if not "!line!"=="" ( if defined first_line ( set "output=!output!,!line!" ) else ( set "output=!line!" set "first_line=1" ) ) ) echo %output% > %output_file% 加上每行结尾添加逗号
可以使用以下修改后的代码,在每行结尾添加逗号:
```
@echo off
setlocal enabledelayedexpansion
set input_file=test.txt
set output_file=output.txt
for /f "tokens=* delims=" %%a in ('type "%input_file%"') do (
set "line=%%a"
set "line=!line:^
=!"
if not "!line!"=="" (
if defined first_line (
set "output=!output!,!line!,"
) else (
set "output=!line!,"
set "first_line=1"
)
)
)
echo %output:~0,-1% > %output_file%
```
在上面的代码中,`output` 变量中每行的结尾都添加了逗号。最后一行的逗号会被输出到输出文件中,因此使用 `%output:~0,-1%` 去除最后一个逗号,再将结果输出到输出文件中。
同样地,需要将 `input_file` 和 `output_file` 变量替换为实际使用的文件路径。
@echo off setlocal EnableDelayedExpansion set "file=path/to/file.txt" set "tempfile=%file%.temp" set "linenumber=10" rem read the file and write the modified content to a temporary file set "count=0" (for /f "usebackq delims=" %%i in ("%file%") do ( set /a "count+=1" if !count! neq %linenumber% echo %%i )) > "%tempfile%" rem overwrite the original file with the modified content move /y "%tempfile%" "%file%" > nul 添加一个替换指定多少行文本内容功能
好的,以下是一个示例批处理脚本,用于替换指定路径下的notepad文本文件中的指定行的内容,并将修改后的内容保存到原文件中。请将其中的"path/to/file.txt"替换为您需要处理的文件路径,将"10"替换为您需要替换内容的行数,将"old text"替换为需要替换的旧文本内容,将"new text"替换为需要替换成的新文本内容。
```
@echo off
setlocal EnableDelayedExpansion
set "file=path/to/file.txt"
set "tempfile=%file%.temp"
set "linenumber=10"
set "oldtext=old text"
set "newtext=new text"
rem read the file and write the modified content to a temporary file
set "count=0"
(for /f "usebackq delims=" %%i in ("%file%") do (
set /a "count+=1"
if !count! neq %linenumber% (
echo %%i
) else (
echo %%i | findstr /v /c:"%oldtext%" || (
set "line=%%i"
set "line=!line:%oldtext%=%newtext%!"
echo !line!
)
)
)) > "%tempfile%"
rem overwrite the original file with the modified content
move /y "%tempfile%" "%file%" > nul
```
注意:请谨慎使用此脚本,尤其是对于重要的文件。建议先备份原文件,以免数据丢失。