Bat script replacing a string

Posted on 2010-09-19
Last Modified: 2012-05-10

I got the script below. I am trying to replace a line with a new one.

If I do like this it works:
C:\hudson\jobs\Galop_Release_130_-_Build\workspace\trunk\galopBuildUtilsV2\scripts\BatchSubstitute.bat "/JarRepositor
y/hibernate/hibernate-3.3.2.GA.jar" "/JarRepository/hibernate/hibernate-3.3.2-weaved-by-jnet-log-2.2.2.jar" C:\hudson\jobs\Galop_Release_130_-_Build\workspace\trunk\Foundation\a.classpath

If I do like this it dont work. how can I write the second string to make it work? I need to have it as I trying to do.

I understand that it is this last part sourcepath="/JarRepository/hibernate/hiber"  that it think it is a file. But how can I write the string to make dos understand it is a string?

C:\hudson\jobs\Galop_Release_130_-_Build\workspace\trunk\galopBuildUtilsV2\scripts\BatchSubstitute.bat "/JarRepositor
y/hibernate/hibernate-3.3.2.GA.jar" "/JarRepository/hibernate/hibernate-3.3.2-weaved-by-jnet-log-2.2.2.jar" sourcepath="/JarRepository/hibernate/hiber" C:\hudson\jobs\Galop_Release_130_-_Build\workspace\trunk\Foundation\a.classpath
The system cannot find the file specified.
@echo off
REM -- Prepare the Command Processor --

::BatchSubstitude - parses a File line by line and replaces a substring"
::syntax: BatchSubstitude.bat OldStr NewStr File
::          OldStr [in] - string to be replaced
::          NewStr [in] - string to replace with
::          File   [in] - file to be parsed
:$changed 20100915
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
    set "line=%%B"
    if defined line (
        call set "line=echo.%%line:%~1=%~2%%"
        for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
    ) ELSE echo.

Open in new window

Question by:Mickeys
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 86

Expert Comment

ID: 33711408
Even if it's possible to do it in a batch file, i suggest you don't. Use an Ant replace task or write another Java app to do it

Author Comment

ID: 33711429
and if I should use ant it will look like?

Author Comment

ID: 33711536
Buildfile: build.xml


C:\temp\build.xml:6: replace doesn't support the "sourcepath" attribute

Total time: 0 seconds
<?xml version="1.0" encoding="UTF-8"?>

<project default="replaceText" basedir="./tem">
	<target name="replaceText">
		<replace file=".classpath" token="/JarRepository/hibernate/hibernate-3.3.2.GA.jar" value="/JarRepository/hibernate/hibernate-3.3.2-weaved-by-jnet-log-2.2.2.jar" sourcepath="/JarRepository/hibernate/"/>

Open in new window

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

LVL 86

Expert Comment

ID: 33711539
I can't say exactly as i don't understand the batch file - at that level it becomes a 'write-only' language ;)

If you want to script Windows, Powershell is much better
LVL 86

Expert Comment

ID: 33711545
Get rid of the sourcepath attribute

Author Comment

ID: 33711549
but that is what the string includes. It is the same error as when I used bat. I need this line into the file

/JarRepository/hibernate/hibernate-3.3.2-weaved-by-jnet-log-2.2.2.jar" sourcepath="/JarRepository/hibernate/
LVL 86

Accepted Solution

CEHJ earned 500 total points
ID: 33711556
Try using the CDATA form
LVL 86

Expert Comment

ID: 33711602

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question