Changing a line in file using a batch file
Moderator: DosItHelp
Changing a line in file using a batch file
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
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
Re: Changing a line in file using a batch file
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.
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!
)
Re: Changing a line in file using a batch file
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!!
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!!
Re: Changing a line in file using a batch file
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
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
Re: Changing a line in file using a batch file
This should toggle it (untested):
Edited:
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!
)
)
Re: Changing a line in file using a batch file
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
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
Re: Changing a line in file using a batch file
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.
Re: Changing a line in file using a batch file
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.
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.

Re: Changing a line in file using a batch file
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?
Re: Changing a line in file using a batch file
@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.
@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.
Re: Changing a line in file using a batch file
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.
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.
Re: Changing a line in file using a batch file
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
Note: The .bak file will be the last company file existed before the last changes
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
Re: Changing a line in file using a batch file
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.
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"
Re: Changing a line in file using a batch file
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

Re: Changing a line in file using a batch file
That's got it working!
Thanks alot, will save me alot of time in not messing about changing to file path manually!
Thanks!!!!!!
Thanks alot, will save me alot of time in not messing about changing to file path manually!
Thanks!!!!!!
