2013-11-04 77 views
2

我需要一个批处理文件,应该在PC上搜索今天发布的所有文件命名为“example.ini”,并替换将在“example.ini”第二行的字符串,它应该与其他服务器的ip SERVER_IP=222.222.222.222批处理文件来搜索和替换字符串

更换

SERVER_IP=111.111.111.111这怎么可能?

+1

是'SERVER_IP ='上的任何其他行? – foxidrive

+0

SERVER_IP =仅在第二行 –

+0

谢谢。搜索整个驱动​​器并检查每个'example.ini'是否为'今天日期'可能会非常缓慢。有没有办法缩小搜索范围? 'example.ini'会有其他特色吗?也许IP地址? – foxidrive

回答

3

对一些示例文件进行测试。

这将改变c:\并在其中包含SERVER_IP=212.83.63.108,将其更改为SERVER_IP=222.22.22.222

它采用所谓的repl.bat一个辅助批处理文件从子目录中的example.ini文件 - 在同一个文件夹中https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

广场repl.bat批处理文件。

@echo off 
for /r c:\ %%a in (example.ini) do (
find "SERVER_IP=212.83.63.108" <"%%a" >nul && (
    echo processing "%%a" 
    type "%%a"|repl "SERVER_IP=212\.83\.63\.108" "SERVER_IP=222.22.22.222" >"%%a.tmp" 
    move /y "%%a.tmp" "%%a" >nul 
    ) 
) 
+0

批处理文件可以找到“example.ini”但试图更换时出现错误 “repl”未被识别为内部或外部命令,可操作的程序或批处理,虽然我有repl.bat在同一个文件夹 –

+0

我想说你是用管理员权限提升批处理文件。将'repl.bat'放在'c:\ windows'文件夹中,使它位于路径上。 – foxidrive

+0

非常感谢现在可以替代我的“example.ini”中的字符串,谢谢您的付出 –

0

在32位窗口Edlin(dos编辑器)可用。

12 
New text 
e 

是在文件的第12行写入“新文本”的edlin脚本。键入

edlin 

然后

? 

要使用

edlin c:\folder\filename.ext < edlin.script 

您必须使用短文件名。文件必须在64K行之下。已创建今天在驱动器C的任何子目录

0

更改String1中被String2的文件中的第二行命名example.ini:

@echo off 
    setlocal enableextensions enabledelayedexpansion 

    rem Arguments: 
    rem  %1 = search string 
    rem  %2 = replace string 
    rem  [ %3 = file in where to replace ] 
    rem   this parameters is internally used by the 
    rem   batch file while processing 

    rem chech for parameters 
    if "%~2"=="" goto :EOF 

    rem retrieve parameters 
    set "_search=%~1" 
    set "_replace=%~2" 

    rem check if asked to replace in a file 
    rem this will be done recursively from this same batch 
    if not "%~3"=="" (
     set "_file=%~3" 
     goto doFileReplace 
    ) 

    forfiles /P C:\ /S /M example.ini /D +0 /C "cmd /c if @isdir==FALSE findstr /c:\"%_search%\" \"@path\" >nul && %~dpnx0 \"%_search%\" \"%_replace%\" @path " 

    goto :EOF 

:doFileReplace 
    echo "%_file%" 
    set "_tempFile=%temp%\%~nx0.tmp" 
    break > "%_tempFile%" 
    (for /F "tokens=1,* delims=:" %%l in ('findstr /n /r "." "%_file%"') do (
     if %%l EQU 2 (
      set "_text=%%m" 
      echo !_text:%_search%=%_replace%! 
     ) else (
      echo %%m 
     ) 
    ))>>"%_tempFile%" 

    copy /Y "%_tempFile%" "%_file%" >nul 
    goto :EOF 
相关问题