1. 程式人生 > >使用bat批處理替換文字中的內容

使用bat批處理替換文字中的內容

@echo off

setlocal enabledelayedexpansion

set file=

set /p file=  請輸入要操作的檔名稱(包括副檔名):

set "file=%file:"=%"

for %%i in ("%file%") do set file=%%~fi

echo.

set replaced=

set /p replaced= 請輸入即將被替換的內容:

echo.

set all=

set /p all=  請輸入替換字串:

for /f "delims=" %%i in ('type "%file%"') do (

   set str=%%i

   set "str=!str:%replaced%=%all%!"

   echo !str!>>"%file%"_tmp.txt

)

copy "%file%" "%file%"_bak.txt >nul 2>nul

move "%file%"_tmp.txt "%file%"

start "" "%file%"