0

I have con.properties file as below.

data1=users
data2=admins

When a user clicks on batch file provided, values of the above have to be changed based on the area.

if user selected country1 con.properties file has to be changed as below

data1=hello
data2=bye

if user selected country2 con.properties file has to be changed as below

data1=users
data2=admins

I have tried this:

@ECHO OFF

SET /P area="Choose a country (type Country1, Country2): "

2>NUL CALL :%area% # jump to :Country1, :Country2, :England, etc. IF ERRORLEVEL 1 CALL :DEFAULT_CASE # If label doesn't exist

ECHO Done. EXIT /B

:Country1 powershell -Command "& {(Get-Content "con.properties") -replace 'users','hello' | Out-File -FilePath con.properties;}" GOTO END_CASE :Country2 powershell -Command "& {(Get-Content "con.properties") -replace 'users','bye' | Out-File -FilePath con.properties;}" GOTO END_CASE :DEFAULT_CASE ECHO Wrong Case GOTO END_CASE :END_CASE VER > NUL # reset ERRORLEVEL GOTO :EOF # return from CALL

Above script replacing values mentioned in the argument but it has to replace the values based the data1 and data2

DavidPostill
  • 162,382
Swamy
  • 1

2 Answers2

0
@ECHO OFF

SET /P area="Choose a country (type Country1, Country2): "

2>NUL CALL :%area% # jump to :Country1, :Country2, :England, etc.
IF ERRORLEVEL 1 CALL :DEFAULT_CASE # If label doesn't exist

ECHO Done.
EXIT /B

:Country1
  powershell -C  ^& ^{(cat 'noc.properties'^).replace('users','hello'^)^|Out-File -FilePath .\'noc.properties'^}
  GOTO END_CASE
:Country2
  powershell -C  ^& ^{(cat 'noc.properties'^).replace('users','bye'^)^|Out-File -FilePath .\'noc.properties'^}
  GOTO END_CASE
:England1
  powershell -C ^& ^{(cat 'noc.properties'^).replace('hello','users'^)^|Out-File -FilePath .\'noc.properties'^}
  GOTO END_CASE
:England2
  powershell -C ^& ^{(cat 'noc.properties'^).replace('bye','admins'^)^|Out-File -FilePath .\'noc.properties'^}
  GOTO END_CASE
:DEFAULT_CASE
  ECHO Wrong Case
  GOTO END_CASE
:END_CASE
  VER > NUL # reset ERRORLEVEL
  GOTO :EOF # return from CALL

Note that the editing is done focusing only on the non-functionality of your current code, however the edits only focus on these points listed below and added England options (1,2) to allow loop testing of changes.

  1. Why use CON device name for file name? This makes PowerShell not see your file, it will find the use of "?,*" wildcards in the forms con*.properties and con?.properties, but con.properties it doesn't even list in the dir con.properties command.

  2. Pay attention to syntaxes where they require the use of escapes relevant to characters (including operators) in calls to PowerShell

  3. The current code posted in response has been tested and works for the noc.properties file name, and it simply doesn't work for the original name con.properties

Io-oI
  • 9,237
-1

Note: as lo-ol said in his answer is 'Con' a reserved name that shouldn't be used for a file name, so I changed the file name to 'noc.properties'

@ECHO OFF

SET /P area="Choose a country (type Country1, Country2): "

2>NUL CALL :%area% # jump to :Country1, :Country2, :England, etc. IF ERRORLEVEL 1 CALL :DEFAULT_CASE # If label doesn't exist

ECHO Done. EXIT /B

:Country1 powershell -Command "& {(Get-Content 'noc.properties') -replace 'data1=.$','data1=hello' -replace 'data2=.$','data2=bye' | Set-Content 'noc.properties'}" GOTO END_CASE :Country2 powershell -Command "& {(Get-Content 'noc.properties') -replace 'data1=.$','data1=hello' -replace 'data2=.$','data2=bye' | Set-Content 'noc.properties'}" GOTO END_CASE :DEFAULT_CASE ECHO Wrong Case GOTO END_CASE :END_CASE VER > NUL # reset ERRORLEVEL GOTO :EOF # return from CALL

Or a shorter syntax

@ECHO OFF

SET /P area="Choose a country (type Country1, Country2): "

powershell -Command "$data1, $data2 = switch ($env:area) { Country1 {'hello','bye'} Country2 {'users','admins'} default {Write-Error 'Wrong Case'-EA 1}} ; sc 'noc.properties' ( (gc 'noc.properties') -replace 'data1=.$',('data1='+$data1) -replace 'data2=.$',('data2='+$data2) )"

ECHO Done. EXIT /B

GOTO END_CASE :END_CASE VER > NUL # reset ERRORLEVEL GOTO :EOF # return from CALL

y y
  • 239