Changing a line in file using a batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Goomba79
Posts: 7
Joined: 04 Dec 2012 06:02

Changing a line in file using a batch file

#1 Post by Goomba79 » 04 Dec 2012 06:07

Hello,

I'm trying to make a batch file to change a line of text in a file depending what the line is....

I have written this bit of code (with the help of google) but it doesnt work!

Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "C:\ProgramData\Sage\Company.txt"
Set objFile = objFS.OpenTextFile(strFile)

Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine,"S:\Accounts") Then
strLine = Replace(strLine,"\\SAGE\SG50Acc$\Data\")

else
strLine = Replace(strLine,"S:\Accounts")

End If

WScript.Echo strLin

Loop

Can anybody point me in the direction of what i need to do to get it working?
i'm sure that its something really obvious!!

MAny thanks,

Iain

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Changing a line in file using a batch file

#2 Post by foxidrive » 04 Dec 2012 06:19

Your script is VB and not batch.

Spell out what you want to replace - apparently it is a line with this:
S:\Accounts
with this:
\\SAGE\SG50Acc$\Data\

Are there any blank lines in the file?

This code will replace "S:\Accounts:" with "\\SAGE\SG50Acc$\Data\" and empty lines are removed.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('type "C:\ProgramData\Sage\Company.txt"') do (
set "var=%%a"
set var=!var:S:\Accounts=\\SAGE\SG50Acc$\Data\!
>>"newfile.txt" echo !var!
)

Goomba79
Posts: 7
Joined: 04 Dec 2012 06:02

Re: Changing a line in file using a batch file

#3 Post by Goomba79 » 04 Dec 2012 06:27

Hello,

there are no blank lines in the file
just the a line that will either be S:\Accounts or \\SAGE\SG50Acc$\Data\

my company has 2 versions of sage and point to different locations (the file paths above)
so i want the batch file to change the file so so the user can swap between sage locations!

I will give your code a go and let you know how i get on!

Thanks for your help!!

Goomba79
Posts: 7
Joined: 04 Dec 2012 06:02

Re: Changing a line in file using a batch file

#4 Post by Goomba79 » 04 Dec 2012 06:34

from looking at it it looks as though it will just change s:\accounts to \\sage\SG50Acc$\Data\

i need it to look at whats there and swap to the other, maybe using an if staement?

if text in file = S:\Accounts then text in file = \\sage\SG50Acc$\Data\

elseif text in file = \\sage\SG50Acc$\Data\ then text in file = S:\Sage

end if

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Changing a line in file using a batch file

#5 Post by foxidrive » 04 Dec 2012 06:39

This should toggle it (untested):

Edited:


Code: Select all

@echo off
del "newfile.txt" 2>nul
find /i "S:\Accounts" < "C:\ProgramData\Sage\Company.txt">nul


if errorlevel 1 (

setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('type "C:\ProgramData\Sage\Company.txt"') do (
set "var=%%a"
set var=!var:\\SAGE\SG50Acc$\Data\=S:\Accounts!
>>"newfile.txt" echo !var!
)

) else (

setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('type "C:\ProgramData\Sage\Company.txt"') do (
set "var=%%a"
set var=!var:S:\Accounts=\\SAGE\SG50Acc$\Data\!
>>"newfile.txt" echo !var!
)

)


Goomba79
Posts: 7
Joined: 04 Dec 2012 06:02

Re: Changing a line in file using a batch file

#6 Post by Goomba79 » 04 Dec 2012 06:59

Thanks for the reply Foxidrive,

I gave that a go but unfotunately it didnt work.

could it be because what im trying to edit isnt a txt file its just listed as 'file' as the type?

you can open it with notepad to see the contents.

Thanks again,

iain

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Changing a line in file using a batch file

#7 Post by foxidrive » 04 Dec 2012 07:16

Goomba79 wrote:I gave that a go but unfotunately it didnt work.

could it be because what im trying to edit isnt a txt file its just listed as 'file' as the type?
you can open it with notepad to see the contents.


It works here - is the source file in notepad just plain text lines? Without seeing it we can't be sure. (zip and upload somewhere is an option and post the URL)

The batch file will create newfile.txt and you will need to copy that one over the source file, if you want it to toggle the source file.
Backup the source file first, if you choose to do that.

Goomba79
Posts: 7
Joined: 04 Dec 2012 06:02

Re: Changing a line in file using a batch file

#8 Post by Goomba79 » 04 Dec 2012 07:54

hello again.

it is plain text.
when i open the file using notepad (right click open with) all thats in the file is the text S:\Accounts

I cant upload the file unfortunatly as the companys firewall blocks file sharing sites. :x

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Changing a line in file using a batch file

#9 Post by foxidrive » 04 Dec 2012 08:06

Goomba79 wrote:it is plain text.
when i open the file using notepad (right click open with) all thats in the file is the text S:\Accounts


Is that the "C:\ProgramData\Sage\Company.txt" file?

Then what is in newfile.txt in the same folder as the batch file, when you run the batch file?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Changing a line in file using a batch file

#10 Post by abc0502 » 04 Dec 2012 08:22

@Goomba79, I tested Foxidrive's code and it works here too.

@Foxidrive, maybe he is looking at the company.txt file to see the changes, and not looking at the newfile.txt.
I think he want the changes to be done to the company.txt file.

I guess He wants that batch to be as a switch button, run it first time it change the location to the other location, run it again and it do the same like from A to a and then if run agian a to A and so every time he run the batch.

Goomba79
Posts: 7
Joined: 04 Dec 2012 06:02

Re: Changing a line in file using a batch file

#11 Post by Goomba79 » 04 Dec 2012 08:33

Hello,

I want it to work as abc0502 has stated.

the file company isnt a text file it is just listed as a file (its isn't called company.txt, just company) i have to open it with notedpad to see the contents.

It is a file that the accounts program, sage, looks at and is pointed to the location of the accounts data.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Changing a line in file using a batch file

#12 Post by abc0502 » 04 Dec 2012 08:35

This is the same code Foxidrive posted just made it take a backup copy of the existing file and then do the changes and modify the file.
Final result will be seen in the "company.txt" file

Code: Select all

@echo off

set "source=C:\ProgramData\Sage\Company"
set "bakup=C:\ProgramData\Sage\Company.bak"

del "%bakup%" 2>nul
find /i "S:\Accounts" < "c.txt">nul

if errorlevel 1 (
REM back-up copy
Ren "%source%" "%bakup%" >nul

setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('type "%bakup%"') do (
set "var=%%a"
set var=!var:\\SAGE\SG50Acc$\Data\=S:\Accounts!
>>"%source%" echo !var!
)

) else (
REM back-up copy
Ren "%source%" "%bakup%" >nul

setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('type "%bakup%"') do (
set "var=%%a"
set var=!var:S:\Accounts=\\SAGE\SG50Acc$\Data\!
>>"%source%" echo !var!
)

)


Note: The .bak file will be the last company file existed before the last changes

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Changing a line in file using a batch file

#13 Post by foxidrive » 04 Dec 2012 08:43

Try abc0502's version - and here is the same code as above but with the real company file name, and it should overwrite the "C:\ProgramData\Sage\Company" file and toggle the text inside it.

But backup the "C:\ProgramData\Sage\Company" file in case it turns out not to be a simple extensionless text file.


Code: Select all

@echo off
del "newfile.txt" 2>nul
find /i "S:\Accounts" < "C:\ProgramData\Sage\Company">nul


if errorlevel 1 (

setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('type "C:\ProgramData\Sage\Company"') do (
set "var=%%a"
set var=!var:\\SAGE\SG50Acc$\Data\=S:\Accounts!
>>"newfile.txt" echo !var!
)

) else (

setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('type "C:\ProgramData\Sage\Company"') do (
set "var=%%a"
set var=!var:S:\Accounts=\\SAGE\SG50Acc$\Data\!
>>"newfile.txt" echo !var!
)

)

if exist "newfile.txt" move /y "newfile.txt" "C:\ProgramData\Sage\Company"

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Changing a line in file using a batch file

#14 Post by foxidrive » 04 Dec 2012 08:46

abc0502 wrote:@Foxidrive, maybe he is looking at the company.txt file to see the changes, and not looking at the newfile.txt.
I think he want the changes to be done to the company.txt file.

I guess He wants that batch to be as a switch button, run it first time it change the location to the other location, run it again and it do the same like from A to a and then if run agian a to A and so every time he run the batch.


I gathered that - and commented earlier that he could make it overwrite the original file. I wasn't too clear, obviously :)

Goomba79
Posts: 7
Joined: 04 Dec 2012 06:02

Re: Changing a line in file using a batch file

#15 Post by Goomba79 » 04 Dec 2012 08:50

That's got it working!
Thanks alot, will save me alot of time in not messing about changing to file path manually!
Thanks!!!!!! :P

Post Reply