Page 1 of 1

Help with characters replacement in a INI file and psexec command

Posted: 10 Nov 2022 14:28
by Dasmius
I'm currently using the following command to replace some numbers inside an INI file:

powershell -Command "(Get-Content -Path '\\%pc%\c$\patch\NewSrc\SRC.ini') -replace '12335','%ramal%' | Set-Content -Path '\\% pc%\c$\patch\NewSrc\SRC.ini'

This command works fine if the numbers inside the file are 12335, but if anything else, camndo will not work. The question is, is there a way to replace a sequence of unknown characters knowing only what has before it?

Here's how the line with the replaced characters is found inside the .INI file:

EXTENSION=12335

I would like to replace any character set after the "=" with whatever I want without having to know in advance what they are.
Has as?

Another problem I'm having is with p psexec, I wanted to run setup a .reg registry file on a computer remotely. However when I enter the command line, it returns that the specified file could not be found.

Here's what the command line looks like

Copy "c:\troca.reg" "\\%host%\c$\"
psexec \\%host" cmd /c "c:\exchange.reg" /y
or
psexec \\%host" "c:\exchange.reg" /y
or
psexec \\%host" cmd /c "regedit /s c:\exchange.reg"
or
psexec \\%host" regedit /s "c:\exchange.reg"

Even if I specify a user with administrative rights, it returns saying that the specified file could not be found, so I tried this:

PsExec \\%pc% -u "%domain%\%user%" -p "%pass%" -s regedit /s "c:\exchange.reg"

But it returns saying that regedit exited with error 0

Can you guys enlight me?

Re: Help with characters replacement in a INI file and psexec command

Posted: 21 Nov 2022 12:58
by Dasmius
Up,

Any idea abou at least the characters replacements at the .INI files

Re: Help with characters replacement in a INI file and psexec command

Posted: 21 Nov 2022 13:20
by miskox
For string substitution you should see: help set

Code: Select all

    %PATH:str1=str2%

would expand the PATH environment variable, substituting each occurrence
of "str1" in the expanded result with "str2".  "str2" can be the empty
string to effectively delete all occurrences of "str1" from the expanded
output.  "str1" can begin with an asterisk, in which case it will match
everything from the beginning of the expanded output to the first
occurrence of the remaining portion of str1.
You don't provide an .ini file so we don't know how to proceed. Maybe you could make your .ini file from scratch (so you have no need for substitution).

Saso

Posted: 22 Nov 2022 12:34
by Dasmius
SRC.INI
(504 Bytes) Downloaded 430 times
That is the .INI file, so I need to be able to replce the to lines "Ramal=xxxxx" whatever the have or not number after "=" cuz actually I'm using this command to replace an previous number after "=" sign

powershell -Command "(Get-Content -Path '\\%pc%\c$\patch\NewSrc\SRC.ini') -replace '12335','%ramal%' | Set-Content -Path '\\% pc%\c$\patch\NewSrc\SRC.ini'

But I need to do this in case there is any other number or nothing after the "=" sign.

So, if you guys can help me :)

Re: Help with characters replacement in a INI file and psexec command

Posted: 22 Nov 2022 16:14
by miskox
This works. I tried to use delayedexpansion with no luck. I see you have some spaces at the end of the lines - they are preserved.

Code: Select all

@echo off
(echo [SRC] )>new_ini.ini
for /f "skip=1 tokens=1,2 delims==" %%f in (src.ini) do call :DOSOME "%%f" "%%g"
goto :EOF

:DOSOME
set new_var=%~1=%~2
REM IF below is CASE SENSITIVE
if "%new_var:~0,6%"=="Ramal=" set new_var=Ramal=%%ramal%%
(echo %new_var%)>>new_ini.ini
goto :EOF
Saso

Re: Help with characters replacement in a INI file and psexec command

Posted: 24 Nov 2022 06:30
by Dasmius
Oh I see, I'll try it so.

tks a lot!

Re: Help with characters replacement in a INI file and psexec command

Posted: 24 Nov 2022 06:55
by Dasmius
Worked like a charm.

I still have some code lines before it to run in my batch and need to rename file back to SRC.ini and replace de old one. So can I do it with ren command or need for /f ?

Re: Help with characters replacement in a INI file and psexec command

Posted: 24 Nov 2022 07:09
by Dasmius
I've tried to change it to get back to my menu config removing goto :EOF and changing it to goto :MENU, but when I do it, the new_ini.ini file turn into a mess, with no data...

Re: Help with characters replacement in a INI file and psexec command

Posted: 24 Nov 2022 07:16
by Dasmius
That is more like I need

@echo off
CHCP 65001
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION



:menu
CLS
ECHO.
ECHO.
ECHO [104;93m ____________________________________ [0m
ECHO [104;93m│ ********************************** │[0m
ECHO [104;93m│ SUBMENU - Configuração SRC │[0m
ECHO [104;93m│ ********************************** │[0m
ECHO [104;93m│ │[0m
ECHO [104;93m│ 1 - Set SRC │[0m
ECHO [104;93m│ │[0m
ECHO [104;93m│ │[0m
ECHO [104;93m│ 6 - Exit │[0m
ECHO [104;93m│ RØÐRIGØ │[0m
ECHO [104;93m│────────────────────────────────────│[0m
ECHO.

ECHO [101;1m

CHOICE /N /C:12 /M "Escolha uma opção: "

ECHO [0m

if errorlevel ==2 goto EXIT
if errorlevel ==1 goto SRC

GOTO MENU



:SRC
set /p ramal="Digite o Ramal"
(echo [SRC] )>new_ini.ini
for /f "skip=1 tokens=1,2 delims==" %%f in (src.ini) do call :DOSOME "%%f" "%%g"
goto :EOF

:DOSOME
set new_var=%~1=%~2
REM IF below is CASE SENSITIVE
if "%new_var:~0,6%"=="Ramal=" set new_var=Ramal=%ramal%
(echo %new_var%)>>new_ini.ini
REM HERE I NEED A COMMAND TO RENAME AND REPLACE THE OLD SRC.ini FILE
REM PAUSE HERE CUZ NEED TO CHECK ANOTHER PREVIOUS COMMANDS AND TRANSFER FILES
PAUSE
goto menu


:EXIT

EXIT



BUT, when I do it, the new_ini.ini file returns almos empyt inside of it, just with this lines inside of

[SRC]
DDD=11

What I'm doing wrong?

Re: Help with characters replacement in a INI file and psexec command

Posted: 24 Nov 2022 07:23
by Dasmius
That's what I'm doing actually

:SRC
CLS
ECHO.
ECHO [101;1m
SET /P user="Informe o usuário administrativo: "
ECHO [0m
ECHO.
ECHO [101;1m
SET /P pass="Digite a senha do usuário administrativo: "
ECHO [0m
ECHO.
ECHO [101;1m
SET /P ramal="Digite o Ramal: "
ECHO [0m
ECHO.
ECHO [92mImplementando o SRC e configurando o Ramal, aguarde...[0m
RD /S /Q "\\%pc%\c$\Integrador"
RD /S /Q "\\%pc%\c$\NewSrc"
MD "\\%pc%\c$\Program Files (x86)\Borland\Common Files\BDE"
MD "\\%pc%\c$\Integrador"
MD "\\%pc%\c$\NewSrc"
XCOPY "C:\Suporte\SRC\SRC" "\\%pc%\c$\" /D/I/E/H/C/Y
COPY "C:\Suporte\SRC\BDE\*.*" "C:\Program Files (x86)\Borland\Common Files\BDE" /Y
COPY "C:\Suporte\SRC\ODBC\ODBC32.reg" "\\%pc%\c$\NewSrc" /Y
PsExec \\%pc% -u "domainbr\%user%" -p "%pass%" -s regedit /s "c:\NewSrc\ODBC32.reg"
REM PsExec \\%pc% -u "domainbr\%user%" -p "%pass%" -s "c:\NewSrc\ODBC32.reg" /y
powershell -Command "(Get-Content -Path '\\%pc%\c$\NewSrc\SRC.ini') -replace '12335','%ramal%' | Set-Content -Path '\\%pc%\c$\NewSrc\SRC.ini'
PAUSE
GOTO MENU

So I need to access an remote computer, copy some files and modify "Ramal=xxxxxx" lines in SRC.ini file

Re: Help with characters replacement in a INI file and psexec command

Posted: 25 Nov 2022 00:19
by miskox
Dasmius wrote:
24 Nov 2022 07:09
I've tried to change it to get back to my menu config removing goto :EOF and changing it to goto :MENU, but when I do it, the new_ini.ini file turn into a mess, with no data...
Please use code tags (select </> button).

You should leave that

Code: Select all

goto :EOF
because it has to leave the loop. Your code should go *after* the FOR command. FOR command reads every line in your source .ini file and calls the subroutine (:DOSOME) which writes the output file. So this subroutine should be left as it is.

Saso

Re: Help with characters replacement in a INI file and psexec command

Posted: 25 Nov 2022 00:22
by miskox
Adding some code blocks (as I am not the mod I can't change the original post):
Dasmius wrote: That is more like I need

Code: Select all

@echo off
CHCP 65001
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION



:menu
CLS
ECHO.
ECHO.
ECHO [104;93m ____________________________________ [0m
ECHO [104;93m│ ********************************** │[0m
ECHO [104;93m│     SUBMENU - Configuração SRC     │[0m
ECHO [104;93m│ ********************************** │[0m
ECHO [104;93m│                                    │[0m
ECHO [104;93m│ 1 - Set SRC                        │[0m
ECHO [104;93m│                                    │[0m
ECHO [104;93m│                                    │[0m
ECHO [104;93m│ 6 - Exit                           │[0m
ECHO [104;93m│                            RØÐRIGØ │[0m
ECHO [104;93m│────────────────────────────────────│[0m
ECHO. 

ECHO [101;1m

CHOICE /N /C:12 /M "Escolha uma opção: "

ECHO [0m

if errorlevel ==2 goto EXIT
if errorlevel ==1 goto SRC

GOTO MENU



:SRC
set /p ramal="Digite o Ramal"
(echo [SRC] )>new_ini.ini
for /f "skip=1 tokens=1,2 delims==" %%f in (src.ini) do call :DOSOME "%%f" "%%g"
goto :EOF

:DOSOME
set new_var=%~1=%~2
REM IF below is CASE SENSITIVE
if "%new_var:~0,6%"=="Ramal=" set new_var=Ramal=%ramal%
(echo %new_var%)>>new_ini.ini
REM HERE I NEED A COMMAND TO RENAME AND REPLACE THE OLD SRC.ini FILE
REM PAUSE HERE CUZ NEED TO CHECK ANOTHER PREVIOUS COMMANDS AND TRANSFER FILES
PAUSE
goto menu


:EXIT

EXIT

BUT, when I do it, the new_ini.ini file returns almos empyt inside of it, just with this lines inside of

Code: Select all

[SRC] 
DDD=11 
What I'm doing wrong?
Saso

Re: Help with characters replacement in a INI file and psexec command

Posted: 25 Nov 2022 00:23
by miskox
(adding code blocks)
Dasmius wrote:
24 Nov 2022 07:23
That's what I'm doing actually

Code: Select all

:SRC
CLS
ECHO.
ECHO [101;1m
SET /P user="Informe o usuário administrativo: "
ECHO [0m
ECHO.
ECHO [101;1m
SET /P pass="Digite a senha do usuário administrativo: "
ECHO [0m
ECHO.
ECHO [101;1m
SET /P ramal="Digite o Ramal: "
ECHO [0m
ECHO.
ECHO [92mImplementando o SRC e configurando o Ramal, aguarde...[0m
RD /S /Q "\\%pc%\c$\Integrador"
RD /S /Q "\\%pc%\c$\NewSrc"
MD "\\%pc%\c$\Program Files (x86)\Borland\Common Files\BDE"
MD "\\%pc%\c$\Integrador"
MD "\\%pc%\c$\NewSrc"
XCOPY "C:\Suporte\SRC\SRC" "\\%pc%\c$\" /D/I/E/H/C/Y
COPY "C:\Suporte\SRC\BDE\*.*" "C:\Program Files (x86)\Borland\Common Files\BDE" /Y
COPY "C:\Suporte\SRC\ODBC\ODBC32.reg" "\\%pc%\c$\NewSrc" /Y
PsExec \\%pc% -u "domainbr\%user%" -p "%pass%" -s regedit /s "c:\NewSrc\ODBC32.reg"
REM PsExec \\%pc% -u "domainbr\%user%" -p "%pass%" -s "c:\NewSrc\ODBC32.reg" /y
powershell -Command "(Get-Content -Path '\\%pc%\c$\NewSrc\SRC.ini') -replace '12335','%ramal%' | Set-Content -Path '\\%pc%\c$\NewSrc\SRC.ini'
PAUSE
GOTO MENU
So I need to access an remote computer, copy some files and modify "Ramal=xxxxxx" lines in SRC.ini file

Re: Help with characters replacement in a INI file and psexec command

Posted: 25 Nov 2022 00:31
by miskox
And here is the reply to both posts:

Code: Select all

@echo off
CHCP 65001
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION


:menu
CLS
ECHO.
ECHO.
ECHO [104;93m ____________________________________ [0m
ECHO [104;93m│ ********************************** │[0m
ECHO [104;93m│     SUBMENU - Configuração SRC     │[0m
ECHO [104;93m│ ********************************** │[0m
ECHO [104;93m│                                    │[0m
ECHO [104;93m│ 1 - Set SRC                        │[0m
ECHO [104;93m│                                    │[0m
ECHO [104;93m│                                    │[0m
ECHO [104;93m│ 6 - Exit                           │[0m
ECHO [104;93m│                            RØÐRIGØ │[0m
ECHO [104;93m│────────────────────────────────────│[0m
ECHO. 

ECHO [101;1m

CHOICE /N /C:12 /M "Escolha uma opção: "

ECHO [0m

if errorlevel ==2 goto EXIT
if errorlevel ==1 goto SRC

GOTO MENU



:SRC
set /p ramal="Digite o Ramal"
(echo [SRC] )>new_ini.ini
for /f "skip=1 tokens=1,2 delims==" %%f in (src.ini) do call :DOSOME "%%f" "%%g"
REM your code goes here

REM HERE I NEED A COMMAND TO RENAME AND REPLACE THE OLD SRC.ini FILE
REM PAUSE HERE CUZ NEED TO CHECK ANOTHER PREVIOUS COMMANDS AND TRANSFER FILES

REM I would suggest you use the current file name in your .bat which you can delete when you are done (so you don't loose your original .ini file)


goto :EOF

:DOSOME
set new_var=%~1=%~2
REM IF below is CASE SENSITIVE
if "%new_var:~0,6%"=="Ramal=" set new_var=Ramal=%ramal%
(echo %new_var%)>>new_ini.ini
goto :EOF

:EXIT

EXIT

And from another post:

Code: Select all

:SRC
CLS
ECHO.
ECHO [101;1m
SET /P user="Informe o usuário administrativo: "
ECHO [0m
ECHO.
ECHO [101;1m
SET /P pass="Digite a senha do usuário administrativo: "
ECHO [0m
ECHO.
ECHO [101;1m
SET /P ramal="Digite o Ramal: "
ECHO [0m
ECHO.
ECHO [92mImplementando o SRC e configurando o Ramal, aguarde...[0m
RD /S /Q "\\%pc%\c$\Integrador"
RD /S /Q "\\%pc%\c$\NewSrc"
MD "\\%pc%\c$\Program Files (x86)\Borland\Common Files\BDE"
MD "\\%pc%\c$\Integrador"
MD "\\%pc%\c$\NewSrc"
XCOPY "C:\Suporte\SRC\SRC" "\\%pc%\c$\" /D/I/E/H/C/Y
COPY "C:\Suporte\SRC\BDE\*.*" "C:\Program Files (x86)\Borland\Common Files\BDE" /Y
COPY "C:\Suporte\SRC\ODBC\ODBC32.reg" "\\%pc%\c$\NewSrc" /Y
PsExec \\%pc% -u "domainbr\%user%" -p "%pass%" -s regedit /s "c:\NewSrc\ODBC32.reg"
REM PsExec \\%pc% -u "domainbr\%user%" -p "%pass%" -s "c:\NewSrc\ODBC32.reg" /y

REM the command below should be updated:
REM original command: 
REM powershell -Command "(Get-Content -Path '\\%pc%\c$\NewSrc\SRC.ini') -replace '12335','%ramal%' | Set-Content -Path '\\%pc%\c$\NewSrc\SRC.ini'

REM new command. Check if it is OK. I guess '-replace' switch is not needed anymore, right? Is the location of the new_ini.ini correct?
    powershell -Command "(Get-Content -Path '\\%pc%\c$\NewSrc\new_ini.ini') | Set-Content -Path '\\%pc%\c$\NewSrc\SRC.ini'

PAUSE
GOTO MENU

Saso

Re: Help with characters replacement in a INI file and psexec command

Posted: 25 Nov 2022 07:33
by Dasmius
miskox wrote:
25 Nov 2022 00:31
And here is the reply to both posts:

Code: Select all

@echo off
CHCP 65001
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION


:menu
CLS
ECHO.
ECHO.
ECHO [104;93m ____________________________________ [0m
ECHO [104;93m│ ********************************** │[0m
ECHO [104;93m│     SUBMENU - Configuração SRC     │[0m
ECHO [104;93m│ ********************************** │[0m
ECHO [104;93m│                                    │[0m
ECHO [104;93m│ 1 - Set SRC                        │[0m
ECHO [104;93m│                                    │[0m
ECHO [104;93m│                                    │[0m
ECHO [104;93m│ 6 - Exit                           │[0m
ECHO [104;93m│                            RØÐRIGØ │[0m
ECHO [104;93m│────────────────────────────────────│[0m
ECHO. 

ECHO [101;1m

CHOICE /N /C:12 /M "Escolha uma opção: "

ECHO [0m

if errorlevel ==2 goto EXIT
if errorlevel ==1 goto SRC

GOTO MENU



:SRC
set /p ramal="Digite o Ramal"
(echo [SRC] )>new_ini.ini
for /f "skip=1 tokens=1,2 delims==" %%f in (src.ini) do call :DOSOME "%%f" "%%g"
REM your code goes here

REM HERE I NEED A COMMAND TO RENAME AND REPLACE THE OLD SRC.ini FILE
REM PAUSE HERE CUZ NEED TO CHECK ANOTHER PREVIOUS COMMANDS AND TRANSFER FILES

REM I would suggest you use the current file name in your .bat which you can delete when you are done (so you don't loose your original .ini file)


goto :EOF

:DOSOME
set new_var=%~1=%~2
REM IF below is CASE SENSITIVE
if "%new_var:~0,6%"=="Ramal=" set new_var=Ramal=%ramal%
(echo %new_var%)>>new_ini.ini
goto :EOF

:EXIT

EXIT

And from another post:

Code: Select all

:SRC
CLS
ECHO.
ECHO [101;1m
SET /P user="Informe o usuário administrativo: "
ECHO [0m
ECHO.
ECHO [101;1m
SET /P pass="Digite a senha do usuário administrativo: "
ECHO [0m
ECHO.
ECHO [101;1m
SET /P ramal="Digite o Ramal: "
ECHO [0m
ECHO.
ECHO [92mImplementando o SRC e configurando o Ramal, aguarde...[0m
RD /S /Q "\\%pc%\c$\Integrador"
RD /S /Q "\\%pc%\c$\NewSrc"
MD "\\%pc%\c$\Program Files (x86)\Borland\Common Files\BDE"
MD "\\%pc%\c$\Integrador"
MD "\\%pc%\c$\NewSrc"
XCOPY "C:\Suporte\SRC\SRC" "\\%pc%\c$\" /D/I/E/H/C/Y
COPY "C:\Suporte\SRC\BDE\*.*" "C:\Program Files (x86)\Borland\Common Files\BDE" /Y
COPY "C:\Suporte\SRC\ODBC\ODBC32.reg" "\\%pc%\c$\NewSrc" /Y
PsExec \\%pc% -u "domainbr\%user%" -p "%pass%" -s regedit /s "c:\NewSrc\ODBC32.reg"
REM PsExec \\%pc% -u "domainbr\%user%" -p "%pass%" -s "c:\NewSrc\ODBC32.reg" /y

REM the command below should be updated:
REM original command: 
REM powershell -Command "(Get-Content -Path '\\%pc%\c$\NewSrc\SRC.ini') -replace '12335','%ramal%' | Set-Content -Path '\\%pc%\c$\NewSrc\SRC.ini'

REM new command. Check if it is OK. I guess '-replace' switch is not needed anymore, right? Is the location of the new_ini.ini correct?
    powershell -Command "(Get-Content -Path '\\%pc%\c$\NewSrc\new_ini.ini') | Set-Content -Path '\\%pc%\c$\NewSrc\SRC.ini'

PAUSE
GOTO MENU

Saso
Sorry about my misunderstand, but guess I got it now. So I left it like this and it's working (It's not my original batch, It's just an exemple that I was using to try explain what I need)

@echo off
CHCP 65001
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION



:menu
CLS
ECHO.
ECHO.
ECHO [104;93m ____________________________________ [0m
ECHO [104;93m│ ********************************** │[0m
ECHO [104;93m│ SUBMENU - Configuração SRC │[0m
ECHO [104;93m│ ********************************** │[0m
ECHO [104;93m│ │[0m
ECHO [104;93m│ 1 - Set SRC │[0m
ECHO [104;93m│ │[0m
ECHO [104;93m│ │[0m
ECHO [104;93m│ 6 - Exit │[0m
ECHO [104;93m│ RØÐRIGØ │[0m
ECHO [104;93m│────────────────────────────────────│[0m
ECHO.

ECHO [101;1m

CHOICE /N /C:12 /M "Escolha uma opção: "

ECHO [0m

if errorlevel ==2 goto EXIT
if errorlevel ==1 goto SRC

GOTO MENU



:SRC
set /p ramal="Digite o Ramal"
(echo [SRC] )>"%temp%\temp.txt"
for /f "skip=1 tokens=1,2 delims==" %%f in (c:\SRC.ini) do call :DOSOME "%%f" "%%g"
powershell -Command "(Get-Content -Path '%temp%\temp.txt') | Set-Content -Path 'C:\Users\rpcastro\Desktop\SRC.ini'
GOTO menu

REM SUB ROTINA DE :SRC
:DOSOME
set new_var=%~1=%~2
REM IF below is CASE SENSITIVE
if "%new_var:~0,6%"=="Ramal=" set new_var=Ramal=%ramal%
(echo %new_var%)>>"%temp%\temp.txt"
REM HERE I NEED A COMMAND TO RENAME AND REPLACE THE OLD SRC.ini FILE
REM PAUSE HERE CUZ NEED TO CHECK ANOTHER PREVIOUS COMMANDS AND TRANSFER FILES
GOTO :EOF


:EXIT

EXIT
So basically I run an Batch on local computer that copy, register and configure some files at this PC, but I can't write or modify anithing at the server cuz the local PC's doesn't have permissions for that. So I decided to make it at the TEMP folder and use powershell to overwrite the new file on local computer.

So thank you very much for your patience and time :)