我刚接触批处理并开发了一段代码,它将文件夹中的文件夹和文件(文件夹(911)放在我的桌面上)并将字符串(在该文件夹中的所有文件内)替换为另一个字符串,如果找到。(这两个字符串应该从命令提示符用户输入中获取)。另外我想将文件夹内的已更改文件重定向到包含所有已更改文件的某个文件夹,或者至少将它们保存在原始文件中,重写旧字符串。怎么样?
我知道这很简单,但我的代码没有用。我是全新的。请启发并帮助我。
@ECHO OFF
pause
pushd %cd%
set folder="%USERPROFILE%\Desktop"
pause
cd %folder%
SETLOCAL DISABLEDELAYEDEXPANSION
FOR /D %%g IN (???) DO (
set checkpath="%cd%\%%g\991"
if exist !checkpath! (
for /F %%L in ('%%g\991\*.*') do (
SET "Code=%%L"
SET /p branchCode=Enter a Branch Code:
SET /p newBranchCode=Enter New Branch Code:
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO !Code:branchCode=newBranchCode!
ENDLOCAL
pause
)
)
)
答案 0 :(得分:0)
这将在评论中由@Magoo概述 - 该术语将被找到并替换它所在的位置。 Note
%branchcode%
术语是正则表达式repl.bat
,某些类型的字符需要特殊处理。字母数字会很好。
它使用来自 - https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
的名为repl.bat
的帮助程序批处理文件
将@echo off
pushd "%USERPROFILE%\Desktop\source folder"
SET /p "branchCode=Enter a Branch Code: "
SET /p "newBranchCode=Enter New Branch Code: "
for %%a in (*.txt) do (
type "%%a" | repl "%branchcode%" "%newbranchcode%" > "%%~na-edited%%~xa"
)
popd
放在您处理文件的同一文件夹中,或放在路径上的文件夹中,或指定repl.bat的完整路径
{{1}}
答案 1 :(得分:0)
@echo off
setlocal EnableDelayedExpansion
rem These two strings should be taken from command prompt user input
SET /p branchCode=Enter a Branch Code:
SET /p newBranchCode=Enter New Branch Code:
rem takes a folder and files inside that folder(folder(911)
set "folder=%USERPROFILE%\Desktop\911"
cd %folder%
rem inside all the files in that folder
for %%a in (*.*) do (
(for /F "usebackq delims=" %%b in ("%%a") do (
set "line=%%b"
rem replaces a string to another string,if found
echo !line:%branchCode%=%newBranchCode%!
rem redirect the changed files inside the folder to some folder
)) > "%folder%\someFolder\%%a"
)
此批处理文件包含一些应修复的细节;但是,我们必须首先知道这是否是你想要的......