Home > Blockchain >  Batch XML/CONF modify
Batch XML/CONF modify

Time:08-12

Good afternoon guys, I'm having a lot of difficulties trying to modify two values ​​inside an exe.conf file, the values ​​are <add key="FileName" value="20220623.txt"/>, I need to modify the value of value.

The truth is that I am not very aware of the batch language, but if you give me a hand I will be very grateful.

I tried to do it with this line, and some variants but I couldn't modify it.

setlocal enableDelayedExpansion
set "newValue='20221008.txt'"
type "test.exe.conf"|findstr  /v "(<add key="row_process" value="$!newValue!"/>) >fileName.conf.new
move /y "fileName.conf.new" "fileName.conf"

CodePudding user response:

In windows, you can use MSXML and JavaScript from cscript.exe

You'd run the following command

cscript xmlbatch.wsf file.xml "new value"

Here's a sample xmlbatch.wsf script, written in JavaScript.

<package>
<job id="t1">
<script language="JScript">

    var fso = new ActiveXObject( "Scripting.FileSystemObject" );
    var objArgs = WScript.Arguments;

    var strDOMObject = "MSXML2.FreeThreadedDOMDocument";
    var xml = new ActiveXObject( this.strDOMObject );
    
    if( objArgs.length < 2 ) {
        WScript.Echo( "Usage: cscript xmlbatch.wsf file.xml value");
        WScript.Echo( "Outputs the value(s) of all matching nodes" );
        WScript.Quit( 1 );
    }
    var strFileName = objArgs(0);
    var strValue = objArgs(1);

    var strXPath = "//add[@key='FileName']";

    if (fso.FileExists(strFileName) == false) {
        WScript.Echo( "Cannot locate "   strFileName );
        WScript.Quit( 1 );
    }

    try {
        if( !xml.load( strFileName ) ) {
            // output any errors, for invalid XML
            var strErrMsg = '';
            strErrMsg = xml.parseError.reason;
            if( xml.parseError.srcText != "" )
                strErrMsg  = "Source: "   xml.parseError.srcText   "\r\n";
            if( xml.parseError.line != 0 )
                strErrMsg  = "Line: "   xml.parseError.line   "\r\n";
            if( xml.parseError.linepos != 0 )
                strErrMsg  = "Position: "   xml.parseError.linepos   "\r\n";
            throw new Error( xml.parseError.errorCode, strErrMsg ); 
        }

        var nodeList = xml.selectNodes( strXPath );
        if( nodeList != null ) {
            for( var i = 0; i < nodeList.length; i   ) {
                nodeList[i].setAttribute( "value", strValue );
            }
            xml.save( strFileName );
        } else {
            WScript.Echo( "No matching nodes found in "   strFileName   " with XPath \""   strXPath   "\"" );
            WScript.Quit( 1 );
        }
    
        WScript.Quit( 0 ); // success
    } catch( e) {
        WScript.Echo( e.description );
    }

</script> 
</job>
</package>

You may need to install MSXML if your machine doesn't have it already.

CodePudding user response:

It seems to me that this is an "XY" problem in SO parlance, that is the problem presented is a faulty solution to an unstated problem.

The fundamental problem appears to be: Starting with a template file test.exe.conf which contains a line <add key="FileName" value="20220623.txt"/>, produce a new file having substituted a new value for 20220623.

OP's code is designed to remove the <add key.. line, and there's nothing in that code to make a substitution.

Consequently, I'll present a solution aimed at curing the problem, not the code.

Since I'm making some assumptions about the problem, it may not be quite what is desired.

The first issue is what string to substitute for 22020623 - I'd conclude it's likely to change over time, so I've added a few line to enable that data to be supplied or entered instead of changing the batch file. This means that you could execute thisbatchname 20221008 to run the batch and produce a file with 20221008 substituted for 22020623, or execute thisbatchname and the batch will prompt for the string to substitute for 22020623. Be warned though that whatever string is entered in either case will be literally substituted.

The second issue is that I'm assuming that test.exe.conf is a template file, so it can be manipulated - hence, take the original test.exe.conf and modify it so that each <add key... line is replaced by a unique string. I chose substitute line here - but it could be almost any string - @#123 for instance.

@ECHO OFF
SETLOCAL
rem The following settings for the source directory, destination directory,
rem filename, and output filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "filename1=%sourcedir%\q73308290.txt"
SET "outfile=           
  • Related