How to use Windows .bat to edit contents of a file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Thai
Posts: 2
Joined: 02 Jun 2010 23:31

How to use Windows .bat to edit contents of a file

#1 Post by Thai » 02 Jun 2010 23:52

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to use Windows .bat to edit contents of a file

#2 Post by aGerman » 03 Jun 2010 11:42

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

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

Thai
Posts: 2
Joined: 02 Jun 2010 23:31

Re: How to use Windows .bat to edit contents of a file

#3 Post by Thai » 04 Jun 2010 06:11

Thanks for getting back aGerman.

I will have a play.

Thai

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to use Windows .bat to edit contents of a file

#4 Post by aGerman » 04 Jun 2010 08:09

The command line should be something like that:

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

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: How to use Windows .bat to edit contents of a file

#5 Post by ghostmachine4 » 26 Dec 2010 19:13

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

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: How to use Windows .bat to edit contents of a file

#6 Post by rfpd » 26 Dec 2010 19:15

i think it's possibel to do this using the regestry.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: How to use Windows .bat to edit contents of a file

#7 Post by ghostmachine4 » 26 Dec 2010 19:54

rfpd wrote:i think it's possibel to do this using the regestry.

show how then.

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: How to use Windows .bat to edit contents of a file

#8 Post by rfpd » 27 Dec 2010 05:53

i couldn't find looll but i think they save that kind of things in the regestry.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: How to use Windows .bat to edit contents of a file

#9 Post by ghostmachine4 » 27 Dec 2010 07:16

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.

Post Reply