Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 创建批处理文件以提示响应并替换文本或.ini文件中的文本行_Batch File_Text_Replace - Fatal编程技术网

Batch file 创建批处理文件以提示响应并替换文本或.ini文件中的文本行

Batch file 创建批处理文件以提示响应并替换文本或.ini文件中的文本行,batch-file,text,replace,Batch File,Text,Replace,我已经让批处理文件为第一行工作,但是当我尝试添加一个额外的for循环以获取下一行文本时,它实际上复制或复制了文本,而不是替换它。 当前批处理文件甚至不会更改.txt文件。 代码如下-- 如果目标是根据原始文件的内容创建更新的文件,则需要测试文件内容的值,对其进行相应修改,将修改后的行输出到新文件,然后使用“重命名”将原始文件替换为新版本。在您当前的脚本中,您似乎只是将修改过的行添加到文件中。我只是让它将这些行作为“查找并替换”来读取,它对一行有效,因此是一个“for”循环,但是如果我添加另一个循

我已经让批处理文件为第一行工作,但是当我尝试添加一个额外的for循环以获取下一行文本时,它实际上复制或复制了文本,而不是替换它。 当前批处理文件甚至不会更改.txt文件。 代码如下--


如果目标是根据原始文件的内容创建更新的文件,则需要测试文件内容的值,对其进行相应修改,将修改后的行输出到新文件,然后使用“重命名”将原始文件替换为新版本。在您当前的脚本中,您似乎只是将修改过的行添加到文件中。我只是让它将这些行作为“查找并替换”来读取,它对一行有效,因此是一个“for”循环,但是如果我添加另一个循环来更改另一行,它将不起作用。您无法从文件中读取,也无法从类似的文件写入同一文件。我也看不出为什么不能在原始循环中执行第二个替换,而不是添加另一个替换,从而在不必要时加倍工作。此代码实际上已经在读写同一个.txt文件。我将尝试在第一个循环中添加第二个循环,看看t是否有效。谢谢@compotechnology,您的第一个循环既从同一个文件读取,又附加到同一个文件。不要这样做!任何事情出了问题,你都可能失去这两个。首先创建备份,然后读取备份,同时写入原始文件。一旦验证了原始文件的正确性,就可以选择删除备份。
::Find and Replace script allows the user to 
::define a file path, file name and a string 
::to find and replace so as to create a new file.
::
::Original file is backed up with an added extension of *.bak, in case
::the user finds the need to go back to the original.

@echo off
::Use the path from whence the script was executed as
::the Current Working Directory
set CWD=%~dp0

::***BEGIN MODIFY BLOCK***
::The variables below should be modified to the
::files to be changed and the strings to find/replace
::Include trailing backslash in _FilePath
set _FilePath=C:\
set _FileName=Password1.txt

::_WrkFile is the file on which the script will make
::modifications.
set /P Password="What is the admin Password?"
set /P AuditPassword="What is the Audit Password?"
set oldPass="cmspassword=Password1"
set OldAudit="existingauditingdbpassword=Password1"
set CMSAdmin="cmspassword="
set CMSAudit="existingauditingdbpassword="
set OldStr=%oldPass%
set OldStr2=%OldAudit%
set NewStr="%CMSAdmin%%Password%"
set NewStr2="%CMSAudit%%AuditPassword%"
::***END MODIFY BLOCK***

::Set a variable which is used by the
::search and replace section to let us
::know if the string to be modified was
::found or not.
set _Found=Not found

SETLOCAL
SETLOCAL ENABLEDELAYEDEXPANSION

if not exist "%_FilePath%%_FileName%" goto :NotFound

echo Searching for %OldStr% string...
echo.
for /f "usebackq tokens=*" %%a in ("%_FilePath%%_FileName%") do (
    set _LineChk=%%a
    if "!_LineChk!"==%OldStr% (
        SET _Found=Found 
        SET NewStr=!NewStr:^"=! 
        echo !NewStr!
        ) else (echo %%a)
        )>>"%_FilePath%%_FileName%" 2>&1

for /f "usebackq tokens=*" %%b in ("%_FilePath%%_FileName%") do (
    set _LineChk=%%b
    if "!_LineChk!"==%OldStr2% (
        SET _Found=Found 
        SET NewStr2=!NewStr2:^"=! 
        echo !NewStr2!
        )
        )
echo.
:Exit       
exit /b