tags:

views:

49

answers:

1

Hello all,

I am trying to create a simple batch file that will replace a line in a text file with a different line, but keep every other line in the file the same. I found a script somewhere on the internet which I modified slightly and came up with the following code. However, for some reason this code is not echoing any lines ending in "=0" to the new text file as it should be, and is instead echoing them to screen (even with echo off). Does anyone have any suggestions?

Thanks much.

@echo off
if exist prod.ini del new_prod.ini
for /f "tokens=*" %%a in (prod.ini) do call :AddText "%%a"
del prod.ini
rename new_prod.ini prod.ini
exit /b

:AddText %1
@echo on
echo %1
@echo off
set Text=%~1%
if "%Text%"=="server=server-2.domain.com" (
echo server=server-3.domain.com>> new_prod.ini
) else (
echo %~1%>> new_prod.ini
)
exit /b

The original file has some lines like those that follow, but the new file after the script has left out these lines. The script works exactly as expected on the replace, as well as on 20 or so other lines. Only the lines that look as follows cause problems.

_sflag=0
debug_lvl=0
sel_test_lvl=0
ipc_error_logging_lvl=0
ipc_client_debug_lvl=0
ipc_server_debug_lvl=0

Thanks again.

edit: This will be run on hundreds of computers in the organization and using 3rd party tools is not really an option, as installing another application on all machines (even though theoretically possible with the remote management software we will use) is not desired.

Thanks.

+2  A: 

Instead of trying to search and replace using script commands, how about using a command line tool to do the same thing? It's easier and faster (and works), and you can script it too. Google has a ton of them, for example, SSR.

Allon Guralnek
That would be great, unfortunately this will be done on hundreds of computers via a remote management utility. Since those machines do not have any 3rd part tool installed I would then have to deal with the pain of installing that tool remotely (and ensuring that it actually does get installed everywhere) prior to running this. Since it really should be possible with this (just that stupid little "=0" error) this seems like the more logical route to installing new things on all machines in the organization. Any other ideas? Thanks for your response.
wilbbe01
On second thought, I am going to ask the lab admins if they care if I throw that program on there. I can't see why they should care. I like the way you think.
wilbbe01
Awesome, clearance has been granted and SSR will be transferred to all machines alongside the other scripting. Thanks much.
wilbbe01