batch command Search a file and replace a string in files.
Moderator: DosItHelp
batch command Search a file and replace a string in files.
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
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
-
- Posts: 23
- Joined: 20 Dec 2013 05:41
Re: batch command Search a file and replace a string in file
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
compile it and use it (in a batch) as
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
Re: batch command Search a file and replace a string in file
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.
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
Re: batch command Search a file and replace a string in file
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!
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 lineCode: 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) asCode: Select all
java Replace "deployment.javaws.jre.1.args" "new value" config_file > temp
ren temp config_file
-
- Posts: 23
- Joined: 20 Dec 2013 05:41
Re: batch command Search a file and replace a string in file
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.
Re: batch command Search a file and replace a string in file
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 batchCode: 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.
Re: batch command Search a file and replace a string in file
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 .
[/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%"
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
@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