0

I have .ini file and there is a line with

HOST='pc-name'

fragment. But it can be something else instead of pc-name. HOST='random' or HOST='welcome' for example. I need to find it and replace with HOST='%COMPUTERNAME%' where %COMPUTERNAME% is real computer name where this .cmd file is running. I've only found how to replace static things, so the problem is to detect this fragment with random value. And = is interpreted as assignment operation but I need it as simple text symbol.

P.S. I need to do it with .cmd file.

2
  • You can use a regular expression search to find such strings. Commented May 29, 2017 at 13:50
  • What have you tried so far, what do you have trouble with? Commented May 29, 2017 at 17:36

4 Answers 4

1

A tool to search/replace files/pipe lines/blocks: 1 command line

Use msr.exe in my open project https://github.com/qualiu/msr tools directory.

For your case: (Remove -R to preview replacing result; Add -K to backup)

  • Replace any content HOST=.* to HOST=%COMPUTERNAME% :

    msr -p my.ini -t "^(HOST)=.*" -o "$1=\%COMPUTERNAME\%" -R

  • More general: ignore case, white-spaces:

    msr -p my.ini -it "^(HOST)\s*=.*" -o "$1=\%COMPUTERNAME\%" -R

Additionally:

  • Use msr-Win32.exe if your windows is 32-bit.

  • Recursively replace in multiple paths and files:

    msr -rp directory1,dirN,file1,fileN -f "\.ini$" -it "^(HOST)\s*=.*" -o "$1=\%COMPUTERNAME\%" -R

Sign up to request clarification or add additional context in comments.

Comments

0

You can use tools like Notepad++ to open the .ini file.

You can then use a regular expression search to find such strings. For example, you can use HOST='.*' as a search expression with Search mode as "Regular Expression" in Notepad++. This will search for the pattern HOST='any value' and then you can replace it with HOST='%COMPUTERNAME%'

Using CMD, you can use findstr "HOST='.*'" < _place your .ini file name_ >.ini to find all strings which match the pattern and then replace them

3 Comments

I need to do it with .cmd file.
superuser.com/questions/339118/regex-replace-from-command-line explains some tools you can use to achieve this.
I can use pure cmd only, this is a restriction.
0

If you can assume that HOME= will always start in the first column and that the only thing after it could be a string enclosed in apostrophes, you could do something like.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "usebackq tokens=*" %%s IN (`TYPE "thefile.txt"`) DO (
    SET "STR=%%~s"

    IF "!STR:~0,6!" EQU "HOME='" (
        ECHO HOME='newsetting'
    ) ELSE (
        ECHO %%~s
    )
)

Comments

0

If you would have a file with one or more lines containing variables as

HOST='%COMPUTERNAME%'

as you indicate halfway in your post, then you could easily have them replaced as follows. I'm assuming your file is called inputfile and your output goes to outputfile.

for /F "tokens=*" %%I in (inputfile) do call echo %%I>> outputfile

Notes:

  1. The above command should be inside a cmd script, otherwise one should use %I instead of %%I if one wants to run it from the cmd prompt directly.
  2. The "call" statement makes sure that the variables are converted into their values. Without it, they would remain as variables.
  3. Empty lines get discarded, not sure if there is a way to avoid that.
  4. Make sure you don't put any spaces before the ">>" otherwise they will be there in your output file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.