Jump to content

To replace number content with new value


ur
 Share

Recommended Posts

Hi,

I have a file whose content will be as below.

//[publish:include]********************************************************************************
/// \file   PSXBuildNumber.h        Contains the build number for the entire product suite.
///
/// Copyright (c) 2006 CA. All rights reserved
///
/// This software and all information contained therein is confidential
/// and proprietary and shall not be duplicated, used, disclosed or 
/// disseminated in any way except as authorized by the applicable 
/// license agreement, without the express written permission of CA. 
/// All authorized reproductions must be marked with this language.
///
/// EXCEPT AS SET FORTH IN THE APPLICABLE LICENSE AGREEMENT, TO THE 
/// EXTENT PERMITTED BY APPLICABLE LAW, CA PROVIDES THIS SOFTWARE 
/// WITHOUT WARRANTY OF ANY KIND, INCLUDING WITHOUT LIMITATION, ANY 
/// IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR 
/// PURPOSE.  IN NO EVENT WILL CA BE LIABLE TO THE END USER OR ANY 
/// THIRD PARTY FOR ANY LOSS OR DAMAGE, DIRECT OR INDIRECT, FROM THE 
/// USE OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, LOST PROFITS, 
/// BUSINESS INTERRUPTION, GOODWILL, OR LOST DATA, EVEN IF CA IS EXPRESSLY 
/// ADVISED OF SUCH LOSS OR DAMAGE.
//*************************************************************************************************

#define PSX_BUILD_NUMBER 5011
#define PSX_BUILD_NUMBER_STRING "5011"

In the above file, if you observe, I have 2 lines where I can see a number 5011.
This 5011 is not fixed but my requirement is to replace the number mentioned after PSX_BUILD_NUMBER and  PSX_BUILD_NUMBER_STRING with the number I pass in the code.

Can you suggest how to do

Link to comment
Share on other sites

As @JohnOne stated.

StringReplace, possibly setting the 4th parameter ( occurrence ) to the max number of times you want to replace in the file to prevent accidental changes to other parts of the file.

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects

WhyNotWin11
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Link to comment
Share on other sites

  • Moderators

@ur are you looking to pass this value in at run-time? Something like this?

Local $PSX_BUILD_NUMBER

If $CmdLine[0] = "" Then
    MsgBox(0, "", "You must enter a build number")
Else
    $PSX_BUILD_NUMBER = $CmdLine[1]
    MsgBox(0, "", $PSX_BUILD_NUMBER)
EndIf

You would then run the compiled script like this:

scriptname.exe <buildnumber>

-or-

scriptname.exe 1511

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

3 hours ago, ur said:

But I don't want to replace only 5011.

it should replace whatever the number might be in the two lines with PSX_BUILD_NUMBER_STRING and PSX_BUILD_NUMBER

 

Either method I mentioned (_stringBetween easier for noob) will get you whatever that number is.

Please refer to help file example, and have a go.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

This are the 2 solutions i prefer for such a case, 1:

#include <File.au3>

Global $PSX_BUILD_NUMBER

If $CmdLine[0] = "" Then
    MsgBox(0, "", "You must enter a build number")
    Exit
Else
    $PSX_BUILD_NUMBER = $CmdLine[1]
    ;MsgBox(0, "", $PSX_BUILD_NUMBER)
EndIf

Global $aFile, $sFile='PSXBuildNumber.h'

_FileReadToArray($sFile,$aFile)

Global $aSplit=StringSplit($aFile[$aFile[0]],' ')
Global $sSearch=StringReplace($aSplit[$aSplit[0]],'"','')
_ReplaceStringInFile($sFile,$sSearch,$PSX_BUILD_NUMBER)

2:

#include <File.au3>

Global $PSX_BUILD_NUMBER

If $CmdLine[0] = "" Then
    MsgBox(0, "", "You must enter a build number")
    Exit
Else
    $PSX_BUILD_NUMBER = $CmdLine[1]
    ;MsgBox(0, "", $PSX_BUILD_NUMBER)
EndIf

Global $aFile, $sFile='PSXBuildNumber.h'

Global $sData=FileRead($sFile)
Global $iPos=StringInStr($sData,' ',0,-1)
Global $sSearch=StringMid($sData,$iPos+1)
$sSearch=StringReplace($sSearch,'"','')
_ReplaceStringInFile($sFile,$sSearch,$PSX_BUILD_NUMBER)

note: doing this with one ob both  scripts is only possible if last entry in file is the numer or the number in quotes.

Edited by AutoBert
Link to comment
Share on other sites

And what about a SRER ?

$s = FileRead("1.txt")
$new_val = "1234"

$r = StringRegexpReplace($s, '(PSX_BUILD_NUMBER(?:_STRING)?\h*"?)(\d+)', "${1}" & $new_val)
Msgbox(0,"", $r)

Edit : another flavour w/o backreference

$r = StringRegexpReplace($s, 'PSX_BUILD_NUMBER(?:_STRING)?\h*"?\K\d+', $new_val)

 

Edited by mikell
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...