How to use Windows .bat to edit contents of a file
Moderator: DosItHelp
How to use Windows .bat to edit contents of a file
Hi,
I'm managing a school web server and would like to be able to quickly change the home page by using explorer context menu, invoking a batch file.
I know how to overwrite the home page file via this method, but would like to be able to generate a new, replacement file (index.htm), incorporating the name of the file selected in explorer within a meta redirect, such as:
<meta http-equiv="REFRESH" content="0;url=http://192.168.0.1/e-learn/english/english1.htm">
So how do I take the name of the file selected and edit it into a file?
Otherwise, any other ideas would be welcomed......
Thanks.
I'm managing a school web server and would like to be able to quickly change the home page by using explorer context menu, invoking a batch file.
I know how to overwrite the home page file via this method, but would like to be able to generate a new, replacement file (index.htm), incorporating the name of the file selected in explorer within a meta redirect, such as:
<meta http-equiv="REFRESH" content="0;url=http://192.168.0.1/e-learn/english/english1.htm">
So how do I take the name of the file selected and edit it into a file?
Otherwise, any other ideas would be welcomed......
Thanks.
Re: How to use Windows .bat to edit contents of a file
Thai,
this is a batch forum and thats why maybe I shouldn't say that, but:
Don't use a batch file for replacements in websites.
The main reason is that you have to read / replace / write it line by line. But a line into the source text could be too long for batch. Another reason is that you have to escape a lot of characters to echo it (each < to ^< and each > to >^).
I posted a small VBScript in a different forum, so why not here again.
Save as Xchange.vbs
You will find the usage in the comment lines (beginning with ' ). Call this tool into a batch process or from the command prompt.
Regards
aGerman
this is a batch forum and thats why maybe I shouldn't say that, but:
Don't use a batch file for replacements in websites.
The main reason is that you have to read / replace / write it line by line. But a line into the source text could be too long for batch. Another reason is that you have to escape a lot of characters to echo it (each < to ^< and each > to >^).
I posted a small VBScript in a different forum, so why not here again.
Save as Xchange.vbs
Code: Select all
Option Explicit
' Syntax:
'
' Xchange.vbs Filename -Option1 Old -Option2 New
'
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'
' Option1/Option2 | Old/New
' ----------------|------------------------------------------------
' -L | any literal expression, like a word or letter
' |
' -C | use VB constants:
' | "Tab" tab
' | "CrLf" Windows line break
' | "Cr" carriage return
' | "Lf" line feed
' |
' -U | HEX code of any character
'
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'
' Example Calls:
'
' Xchange.vbs "C:\test.txt" -l ";" -c "tab"
' will replace semicolons with tabs in C:\test.txt
'
' Xchange.vbs "C:\test.txt" -u "3F" -l "!"
' will replace Character 3F (question mark) with exclamation marks
'
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'
' Return Code (Errorlevel):
'
' 2 Syntax Error
' 1 File not found
' 0 No Error
On Error Resume Next
Dim strFile, optF, xFind, optR, xReplace, fso, text
If Not WScript.Arguments.Count = 5 Then WScript.Quit (2)
strFile = WScript.Arguments(0)
optF = WScript.Arguments(1)
xFind = WScript.Arguments(2)
optR = WScript.Arguments(3)
xReplace = WScript.Arguments(4)
Select Case UCase(optF)
Case "-L"
Case "-U"
xFind = ChrW(CLng("&H" & xFind))
Case "-C"
If UCase(xFind) = "TAB" Then
xFind = vbTab
ElseIf UCase(xFind) = "CRLF" Then
xFind = vbCrLf
ElseIf UCase(xFind) = "CR" Then
xFind = vbCr
ElseIf UCase(xFind) = "LF" Then
xFind = vbLf
Else
WScript.Quit 2
End If
Case Else
WScript.Quit 2
End Select
Select Case UCase(optR)
Case "-L"
Case "-U"
xReplace = ChrW(CLng("&H" & xReplace))
Case "-C"
If UCase(xReplace) = "TAB" Then
xReplace = vbTab
ElseIf UCase(xReplace) = "CRLF" Then
xReplace = vbCrLf
ElseIf UCase(xReplace) = "CR" Then
xReplace = vbCr
ElseIf UCase(xReplace) = "LF" Then
xReplace = vbLf
Else
WScript.Quit 2
End If
Case Else
WScript.Quit 2
End Select
If Not Err.Number = 0 Then WScript.Quit 2
Set fso = CreateObject("Scripting.FileSystemObject")
text = fso.OpenTextFile(strFile, 1).ReadAll
If Not text = "" Then
fso.CreateTextFile(strFile).Write Replace(text, xFind, xReplace)
End If
Set fso = Nothing
If Err.Number = 0 Then
WScript.Quit 0
Else
WScript.Quit 1
End If
You will find the usage in the comment lines (beginning with ' ). Call this tool into a batch process or from the command prompt.
Regards
aGerman
Re: How to use Windows .bat to edit contents of a file
Thanks for getting back aGerman.
I will have a play.
Thai
I will have a play.
Thai
Re: How to use Windows .bat to edit contents of a file
The command line should be something like that:
Use the entire path for the .vbs and .html files if they are not in the same folder like your batch file.
Regards
aGerman
Code: Select all
"Xchange.vbs" "Thai.html" -l "/english/english1.htm" -l "/thai/thai1.htm"
Use the entire path for the .vbs and .html files if they are not in the same folder like your batch file.
Regards
aGerman
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: How to use Windows .bat to edit contents of a file
Thai wrote:Hi,
I'm managing a school web server and would like to be able to quickly change the home page by using explorer context menu, invoking a batch file.
I know how to overwrite the home page file via this method, but would like to be able to generate a new, replacement file (index.htm), incorporating the name of the file selected in explorer within a meta redirect, such as:
<meta http-equiv="REFRESH" content="0;url=http://192.168.0.1/e-learn/english/english1.htm">
So how do I take the name of the file selected and edit it into a file?
Otherwise, any other ideas would be welcomed......
Thanks.
if you want to modify files, use a tool or a programming language specialized for this. If you prefer native solution, you may try the vbscript provided, however if you can download stuff, you can use sed for windows or gawk for windows ( which has been used in the *nix world long ago till now to edit files ) . A simple illustration
Code: Select all
C:\test>more file
<meta http-equiv="REFRESH" content="0;url=http://192.168.0.1/e-learn/[i]english/english1.htm[/i]">
C:\test>sed "s|english/english1.htm|thai/thai.html|" file
<meta http-equiv="REFRESH" content="0;url=http://192.168.0.1/e-learn/[i]thai/thai.html[/i]">
use -i option to save the file inplace
Code: Select all
C:\test>sed -i.bak "s|english/english1.htm|thai/thai.html|" file
Re: How to use Windows .bat to edit contents of a file
i think it's possibel to do this using the regestry.
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: How to use Windows .bat to edit contents of a file
rfpd wrote:i think it's possibel to do this using the regestry.
show how then.
Re: How to use Windows .bat to edit contents of a file
i couldn't find looll but i think they save that kind of things in the regestry.
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: How to use Windows .bat to edit contents of a file
rfpd wrote:i couldn't find looll but i think they save that kind of things in the regestry.
the answer is no. you don't use the registry to edit contents of a file. you use a programming language/ tools that allows i/o operations. eg Perl/Python/Java/Ruby etc... or specialized tools such as sed/awk , or editors such as emacs, vi etc.