0

i'm kind of stuck here. so for my final project i want to change our procedure for configuring new customers systems. To be precise - start a batch that does "everything" for you. First you have to name some things that will be needed later something like:

:NameNAMESPACE
echo Whats the Namespace?
set /p NAMESPACE=Name Namespace:
echo Is %NAMESPACE% correct? (Y)es / (N)o
set /P p=Please choose:
if /i "%wahl%"=="Y" goto:CreateFolder
if /i "%wahl%"=="N" goto:NameNAMESPACE
echo Wrong choise!
goto:NameNAMESPACE

:NameHOSTNAME
echo Whats the Hostname?
set /p HOSTNAME=Name Hostname:
echo Ist %HOSTNAME% correct? (Y)es / (N)o
set /P wahl=Please choose:
if /i "%wahl%"=="Y" goto:CreateFolder
if /i "%wahl%"=="N" goto:NameHOSTNAME
echo Wrong choise!
goto:NameHOSTNAME

I thing you get it :)
The problem is, at some point i need to edit an *.xml file (could be an text file at first, but must be saved as xml) In this file looks like this

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <!--Connection-->
  <add name="V4H_CONNECTIONINFO" value="Server=HOSTNAME;ServicePort=IISPort;ConnectionPort=1972;Namespace=NAMESPACE;ServiceUser=LicenseSlot_0;ServicePassword=!cgm!;Customer=SYS"/>
  <!--URLs-->
  <add name="V4H_URL_CLICKONCE" value="http://HOSTNAME:8010/v4h.Downloader.application"/>
  <add name="V4H_URL_CHANNEL" value="http://HOSTNAME:IISPort"/>
  <add name="V4H_URL_SERVICES" value="http://HOSTNAME:IISPort/Services/ChannelServices.asmx"/>
  <add name="V4H_URL_SILVERLIGHT" value="http://HOSTNAME:IISPort/?ConnectionInfo={0}"/>
  <!--ASP.NET-->
  <add name="V4H_DBCONNECTION_CHANNEL" value="HOSTNAME,1972,NAMESPACE,ChannelServices_{12:0-5},!cgm!,Pooling=false"/>
  <add name="V4H_DEBUGMODE_CHANNEL" value="false"/>
</root>

the parameters set earlier must be changed (hostname,namespace and port) and the saved as hostname_namespace.xml

I already tried to get some these to work from here click

or here click

i managed to get this to work but only to change one parameter

@echo off &setlocal
set NAMESPACE=TEST_CH_L

set "search=NAMESPACE"
set "replace=%NAMESPACE%"


set "textfile=test.txt"
set "newfile=Output.txt"
(for /f "delims=" %%i in (%textfile%) do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(!line!
    endlocal
))>"%newfile%"
del %textfile%
rename %newfile%  %textfile%
echo Done
pause

So i hope one of you can help me and sorry for the loooog post

thanks in advance Max

Community
  • 1
  • 1
Max K.
  • 1
  • 1
  • Batch files are primitive and archaic. They are intended to be used only to execute other programs. You will need a program that does what you need to do, at least parts. VBScript and JavaScript can do everything that a batch file can do plus more. – Sam Hobbs Apr 08 '16 at 18:06

1 Answers1

0
@ECHO Off
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q36505840.txt"
SET "outfile=%destdir%\outfile.txt"

set "NAMESPACE=TEST_CH_L"
SET "hostname=my_host"
SET "port=starboard"

REM set "textfile=test.txt"
REM set "newfile=Output.txt"
(
for /f "usebackqdelims=" %%i in ("%filename1%") do (
 set "line=%%i"
 CALL :CHANGE namespace HOSTNAME port
)
)>"%outfile%"

REM del %textfile%
REM rename %newfile%  %textfile%
echo Done

GOTO :EOF

:CHANGE
set "search=%1"
IF NOT DEFINED search GOTO output
FOR /f "tokens=1*delims==" %%n IN ('set %search%') DO IF /i "%%n"=="%search%" SET "replace=%%o"
CALL set "line=%%line:%search%=%replace%%%"
SHIFT
GOTO CHANGE

:output
FOR /f "tokens=1*delims==" %%n IN ('set line') DO IF /i "%%n"=="line" ECHO(%%o
GOTO :EOF 

You would need to change the settings of sourcedir and destdir to suit your circumstances.

I used a file named q36505840.txt containing your data for my testing.

Produces the file defined as %outfile%

I've left but remmed-out some of your original code & established fixed values for the three variables namespace hostname and port.

Note that batch-replace is case-insensitive, so I'd change the variablenames and fix your source file to suit.

No great surprises - simply replace the names specified by the arguments to :CHANGE

Magoo
  • 77,302
  • 8
  • 62
  • 84