簡體   English   中英

使用批處理腳本替換文件中的字符串。 我有以下代碼,但在每行末尾添加了空格

[英]Replace string in a file using batch script. I have the below code but it's adding blank space at the end of each line

使用批處理腳本替換文件中的字符串。 我有以下代碼,但是在每行末尾添加了空格。 它會在加載時影響作業過程。 有沒有簡單的方法可以替換,例如在記事本中替換?

@ECHO OFF
setlocal enabledelayedexpansion
set "search=%Old text"
set "replace=%new text"
set txtfile=D:\new_%date:~-7,2%%date:~-10,2%%date:~-4,4%.txt
set newfile=D:\new_D%date:~-7,2%%date:~-10,2%%date:~-4,4%_T%time:~-11,2%%time:~-8,2%%time:~-5,2%.txt
if exist "%newfile%" del /f /q "%newfile%"
for /f "tokens=*" %%a in (%txtfile%) do (
set newline=%%a
set newline=!newline:%search%=%replace%!
echo !newline! >> %newfile%

)
copy %newfile% %textfile%

嘗試替換這個:

echo !newline! >> %newfile%

這樣 :

>>%newfile% echo !newline!

編輯 :您最好使用@MC ND建議的(echo !newline!)

編輯II :如果您想要更快的速度(用於大文件),請使用BAT / Powershell

@echo off
setlocal
set $source=%cd%\out.txt
set $Dest=%cd%\out1.txt

set "search=Old"
set "replace=New"

for /f "delims=" %%a in ('powershell -c "(get-content '%$source%') | foreach-object {$_ -replace '%Search%', '%replace%'} | set-content '%$dest%'"') do echo %%a

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM