Solved Batch to replace line in .inf file

July 3, 2011 at 03:26:25
Specs: Windows XP
Dear Sir,

I need help for creating a batch file for replacing line in .inf file
here's a sample of pc.inf

ComputerName=PC8
ComputerNumber=8

------------------------------------------------
What i want is to replace ComputerName=PC8 to ComputerName=PC1 or etc

i have a sample script also but don't know what's next.

if %computername%==COM1 (set pc=1)
if %computername%==COM2 (set pc=2)
if %computername%==COM3 (set pc=3)

Can someone continue my sample script to create a batch file?

Sorry for my English

Best Regards,
Hotshot


See More: Batch to replace line in .inf file

Report •


✔ Best Answer
July 4, 2011 at 14:44:36
Is this what you want, Try if out on dummy files first to see if it's ok


@echo off
SetLocal EnableDelayedExpansion

cls
echo %ComputerName%

type nul>tmp.txt

for /L %%a in (1,1,10) do (
if /i %ComputerName%==PC%%a (set pc=%%a)
)

echo [%pc%]

echo.
set Flag=0
for /f "tokens=* delims=" %%a in (TEST.INF) do (
echo %%a | find /i "ComputerName" > nul && set Flag=1
echo %%a | find /i "ComputerNumber" > nul && set Flag=2
if !Flag!==0 ( echo %%a >> tmp.txt)
if !Flag!==1 ( echo ComputerName=PC%pc% >> tmp.txt)
if !Flag!==2 ( echo ComputerNumber=%pc% >> tmp.txt
set Flag=0
)
)

del TEST.INF
rename tmp.txt TEST.INF



#1
July 3, 2011 at 08:32:39
your if have no then

1 - Set up a command set to process the inf file line by line to do a simple copy of the file
2 - add a test for _____.
2a - if it is, (command to modify)
2b - if it is not, (continue process)

Not the most elegant but effective.

* using COM1 may cause problems as it refers to a communications port (reserved name).


Report •

#2
July 3, 2011 at 08:40:01
I don't have a knowledge about scripting batch file.

can you correct this script?

@echo off
if %computername%==PC1 (set pc=1)
if %computername%==PC2 (set pc=2)
if %computername%==PC3 (set pc=3)

SET "C:\Program Files\pc.inf" "ComputerName=PC8" "ComputerName=PC%pc%"

I tried this already but nothing happens :(

Thanks for helping

Regards,
Hotshot


Report •

#3
July 3, 2011 at 11:36:29
Hi Hotshot30

Are you trying to replace these lines in the *.inf file with the line
SET "C:\Program Files\pc.inf" "ComputerName=PC8" "ComputerName=PC%pc%"

If so this would not work.
Does your inf file contain only these two lines or are there more lines.


Report •

Related Solutions

#4
July 3, 2011 at 23:08:02
that line is line 5, there are more lines for a total of 20 lines i guess.

Yeah im just trying to replace only these line
ComputerName=PC8

to any of this

if %computername%==PC1 (set pc=1)
if %computername%==PC2 (set pc=2)
if %computername%==PC3 (set pc=3)

Regards
Hotshot


Report •

#5
July 4, 2011 at 14:44:36
✔ Best Answer
Is this what you want, Try if out on dummy files first to see if it's ok


@echo off
SetLocal EnableDelayedExpansion

cls
echo %ComputerName%

type nul>tmp.txt

for /L %%a in (1,1,10) do (
if /i %ComputerName%==PC%%a (set pc=%%a)
)

echo [%pc%]

echo.
set Flag=0
for /f "tokens=* delims=" %%a in (TEST.INF) do (
echo %%a | find /i "ComputerName" > nul && set Flag=1
echo %%a | find /i "ComputerNumber" > nul && set Flag=2
if !Flag!==0 ( echo %%a >> tmp.txt)
if !Flag!==1 ( echo ComputerName=PC%pc% >> tmp.txt)
if !Flag!==2 ( echo ComputerNumber=%pc% >> tmp.txt
set Flag=0
)
)

del TEST.INF
rename tmp.txt TEST.INF


Report •

#6
July 9, 2011 at 01:52:50
Thanks, it worked. but with some errors
is there a workaround on how to run this, before the username/password on windows?
specially windows XP.


Thanks again


Report •

Ask Question