batch command Search a file and replace a string in files.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
eddiechen
Posts: 7
Joined: 22 Dec 2013 20:50

batch command Search a file and replace a string in files.

#1 Post by eddiechen » 22 Dec 2013 21:17

Hi guy,

I want to write a batch find and replace a string in files?, the batch is used to modify the Java configuration. who can help me!

configuration file:

#deployment.properties
#Fri Dec 20 12:40:59 CST 2013
deployment.modified.timestamp=1387514459851
deployment.version=7.21
deployment.javaws.viewer.bounds=323,105,720,360
deployment.browser.path=C\:\\xxusenamexx\\AppData\\Roaming\\360se6\\Application\\360se.exe
#Java Deployment jre's
#Fri Dec 20 12:40:59 CST 2013
deployment.javaws.jre.0.registered=true
deployment.javaws.jre.0.platform=1.6
[color=#BF0000]deployment.javaws.jre.1.args= i need replace to deployment.javaws.jre.1.args=-Xms512m -Xmx768m \u00A8CXX\:permSize\=128m -XX\:MaxPermSize\=256m[/color]
deployment.javaws.jre.0.osname=Windows
deployment.javaws.jre.0.path=C\:\\Program Files (x86)\\Java\\jre7\\bin\\javaw.exe
deployment.javaws.jre.0.product=1.7.0_25
deployment.javaws.jre.0.osarch=x86
deployment.javaws.jre.0.location=http\://java.sun.com/products/autodl/j2se
deployment.javaws.jre.0.enabled=true this line need replace to deployment.javaws.jre.0.enabled=flase

Blyanadams
Posts: 23
Joined: 20 Dec 2013 05:41

Re: batch command Search a file and replace a string in file

#2 Post by Blyanadams » 22 Dec 2013 22:21

for replacement in dos batch you can see the post here for sugestion.

if you want to roll your own, you can use for loop to go over your file, search for the string and replace accoridingly. you can see the dostip site for various topic on string manipulation such as this .

I see you have Java, here's a progarm you can use to replace your line

Code: Select all

import java.io.*;
import java.util.Scanner;
public class Replace {
   
   public static void main(String[] args){
      if ( args.length != 3 ){
         System.out.println("Usage: java Replace [string] [=value] [config]") ;
         System.exit(1);
      }
      String str = args[0];            /* get the string */
      String value = args[1];            /* get the value to change to */
      String fileName = args[2];         /* file name */
      try{
         Scanner scan = new Scanner ( new File( fileName ) );
         while( scan.hasNextLine() ){
            String myLine = scan.nextLine();
            if ( myLine.indexOf( str  ) != -1 ){               
               myLine = str + "=" + value;
            }
            System.out.println( myLine );
         }
      }catch (FileNotFoundException ex){
         ex.printStackTrace();
      }
   }
}


compile it and use it (in a batch) as

Code: Select all

java Replace "deployment.javaws.jre.1.args" "new value" config_file > temp
ren temp config_file

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch command Search a file and replace a string in file

#3 Post by foxidrive » 22 Dec 2013 22:51

This uses a helper batch file called `repl.bat` - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.

Code: Select all

@echo off
type "file.cfg" | repl "(deployment.javaws.jre.0.enabled=)true" "$1false" | repl "(deployment.javaws.jre.1.args=).*" "$1-Xms512m -Xmx768m \u00A8CXX\:permSize\=128m -XX\:MaxPermSize\=256m" >"newfile.cfg"
pause

eddiechen
Posts: 7
Joined: 22 Dec 2013 20:50

Re: batch command Search a file and replace a string in file

#4 Post by eddiechen » 22 Dec 2013 23:46

thank you for your reply.
i problem is just replace deployment.properties file . i want to run this batch command through domain user .
i replace deployment.properties file to run ufdia software.

i install java jer7 and jer6, default deployment.properties file:
#deployment.properties
#Thu Dec 19 17:23:23 CST 2013
deployment.javaws.appicon.index=C\:\\Users\\xxx\\AppData\\LocalLow\\Sun\\Java\\Deployment\\cache\\6.0\\appIcon\\appIcon.xml
deployment.javaws.splash.index=C\:\\Users\\xxxx\\AppData\\LocalLow\\Sun\\Java\\Deployment\\cache\\6.0\\splash\\splash.xml
deployment.version=6.0
deployment.security.TLSv1=false
#Java Deployment jre's
#Thu Dec 19 17:23:23 CST 2013
deployment.javaws.jre.1.location=http\://java.sun.com/products/autodl/j2se
deployment.javaws.jre.0.args=-Xmx512m
deployment.javaws.jre.0.registered=true
deployment.javaws.jre.1.args=-Xmx512m
deployment.javaws.jre.1.enabled=true
deployment.javaws.jre.1.registered=true
deployment.javaws.jre.0.osarch=x86
deployment.javaws.jre.0.osname=Windows
deployment.javaws.jre.1.product=1.6.0_24
deployment.javaws.jre.0.platform=1.7
deployment.javaws.jre.0.path=C\:\\Program Files (x86)\\Java\\jre7\\bin\\javaw.exe
deployment.javaws.jre.0.location=http\://java.sun.com/products/autodl/j2se
deployment.javaws.jre.1.path=C\:\\Program Files (x86)\\Java\\jre6\\bin\\javaw.exe
deployment.javaws.jre.0.enabled=false
deployment.javaws.jre.1.osarch=x86
deployment.javaws.jre.1.osname=Windows
deployment.javaws.jre.1.platform=1.6
deployment.javaws.jre.0.product=1.7.0_09

so i need ones batch command can replace :
deployment.javaws.jre.1.args=-Xms512m -Xmx768m -XX:PermSize=128m -XX:MaxPermSize=256m
deployment.javaws.jre.0.enabled=false
deployment.javaws.jre.1.enabled=true


you proide code i don't how to use .Could you help me with it? thanks!

Blyanadams wrote:for replacement in dos batch you can see the post here for sugestion.

if you want to roll your own, you can use for loop to go over your file, search for the string and replace accoridingly. you can see the dostip site for various topic on string manipulation such as this .

I see you have Java, here's a progarm you can use to replace your line

Code: Select all

import java.io.*;
import java.util.Scanner;
public class Replace {
   
   public static void main(String[] args){
      if ( args.length != 3 ){
         System.out.println("Usage: java Replace [string] [=value] [config]") ;
         System.exit(1);
      }
      String str = args[0];            /* get the string */
      String value = args[1];            /* get the value to change to */
      String fileName = args[2];         /* file name */
      try{
         Scanner scan = new Scanner ( new File( fileName ) );
         while( scan.hasNextLine() ){
            String myLine = scan.nextLine();
            if ( myLine.indexOf( str  ) != -1 ){               
               myLine = str + "=" + value;
            }
            System.out.println( myLine );
         }
      }catch (FileNotFoundException ex){
         ex.printStackTrace();
      }
   }
}


compile it and use it (in a batch) as

Code: Select all

java Replace "deployment.javaws.jre.1.args" "new value" config_file > temp
ren temp config_file

Blyanadams
Posts: 23
Joined: 20 Dec 2013 05:41

Re: batch command Search a file and replace a string in file

#5 Post by Blyanadams » 23 Dec 2013 00:26

eddiechen wrote:i install java jer7 and jer6,:
you proide code i don't how to use .Could you help me with it? thanks!

you say you install java so i assume you know Java. To compile Java program you need to use the javac command. After that its just place the code below in a batch

Code: Select all

java Replace "deployment.javaws.jre.1.args" "new value" config_file > temp
ren temp config_file
java Replace "deployment.javaws.jre.0.enabled" "false" config_file > temp
ren temp config_file



if not, then just try the solution foxidrive did. he has done everything for you.

eddiechen
Posts: 7
Joined: 22 Dec 2013 20:50

Re: batch command Search a file and replace a string in file

#6 Post by eddiechen » 23 Dec 2013 00:41

thanks for you help me . i don;t needed to compile java. i only need to modify java a configuration file.

Blyanadams wrote:
eddiechen wrote:i install java jer7 and jer6,:
you proide code i don't how to use .Could you help me with it? thanks!

you say you install java so i assume you know Java. To compile Java program you need to use the javac command. After that its just place the code below in a batch

Code: Select all

java Replace "deployment.javaws.jre.1.args" "new value" config_file > temp
ren temp config_file
java Replace "deployment.javaws.jre.0.enabled" "false" config_file > temp
ren temp config_file



if not, then just try the solution foxidrive did. he has done everything for you.

eddiechen
Posts: 7
Joined: 22 Dec 2013 20:50

Re: batch command Search a file and replace a string in file

#7 Post by eddiechen » 23 Dec 2013 01:15

foxidrive:
thanks you for your reply.
i find the batch file,but it can't meet my request. You offered links inside the code too much.
i try to run you other code in c:\,but don;t run . can you help me ?thanks .

Code: Select all

@echo off
type "file.cfg" | repl "(deployment.javaws.jre.0.enabled=)true" "$1false" | repl "(deployment.javaws.jre.1.args=).*" "$1-Xms512m -Xmx768m \u00A8CXX\:permSize\=128m -XX\:MaxPermSize\=256m" >"newfile.cfg"
pause
[/quote]

@echo off
setlocal enabledelayedexpansion
set file= deployment.properties
set "file=%file:"=%"
for %%i in ("%file%") do set file=%%~fi
set replaced=deployment.javaws.jre.1.args=
set all=deployment.javaws.jre.1.args=-Xms512m -Xmx768m -XX:PermSize=128m -XX:MaxPermSize=256m
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%"

foxidrive wrote:This uses a helper batch file called `repl.bat` - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.

Code: Select all

@echo off
type "file.cfg" | repl "(deployment.javaws.jre.0.enabled=)true" "$1false" | repl "(deployment.javaws.jre.1.args=).*" "$1-Xms512m -Xmx768m \u00A8CXX\:permSize\=128m -XX\:MaxPermSize\=256m" >"newfile.cfg"
pause

Post Reply