0

I'm short on time and am looking to create a very simple batch script that will:

1) Rename a string within another file in the same directory (name of file is known) Example: Replace "[replace_me_with_path_to_second_file]" with "current_working_directory/second_file.txt" in first_file.txt

2) Open/Execute that file "first_file.txt"

3) close

Thanks in advance!

Natan
  • 1
  • 1
    Beginning the question with »I'm short on time« ... is that just a code phrase for »I'm too lazy to look for solutions myself and rather have someone else solve my problem«? – Joey Jul 06 '11 at 18:49
  • It is possible that your being short on time has temporarily reduced your ability of finding good solutions by yourself. Yet by hinting that you are constrained in time you are effectively putting us in the same constraints, which is somewhat impolitic of you, I think. – Andriy M Jul 06 '11 at 19:46
  • also, not even bothering to comment on previous comments or to give credit to the answer really casts doubts about your real interest on your question. – PA. Jul 07 '11 at 06:46

2 Answers2

0

This is a perfect job for sed, if you're comfortable with regular expressions. Sed uses '/' as the delimeter between the expression to find and its replacement, so you should escape any slashes in the file path. To use your example:

sed -i 's/replace_me_with_path_to_second_file/current_working_directory\/second_file.txt/' first_file.txt
A B
  • 8,340
  • 2
  • 31
  • 35
0

Thanks Alberge, I was hoping of finding a solution that wouldn't require sed since this batch file would be used within a downloaded file in a windows environment to simplify configuration. But looks like I'll have to package it: http://gnuwin32.sourceforge.net/packages/sed.htm

As for the rest of you, the whole premise of a collaborative question/answer directory implies that the person asking the question is either: 1) unable to find an answer 2) short on time and is looking for advice from those with more experience or knowledge in the subject matter.

Criticizing me for stating that I am asking a question because I am constrained on time is somewhat rude.


Turns out that the sed dependencies (dlls) where a little heavy so I found this useful and compact vbs script to emulate the s/r - Is there any sed like utility for cmd.exe

I'll re-post it here:

Dim pat, patparts, rxp, inp
pat = WScript.Arguments(0)
patparts = Split(pat,"/")
Set rxp = new RegExp
rxp.Global = True
rxp.Multiline = False
rxp.Pattern = patparts(1)
Do While Not WScript.StdIn.AtEndOfStream
inp = WScript.StdIn.ReadLine()
WScript.Echo rxp.Replace(inp, patparts(2))
Loop

And my finished Batch script:

@echo off

set full_path=%CD%\file_1.txt
set input_file=file_2.txt
set output_file=file_2.txt

set str_search=FIND_ME
set str_replace=%full_path%

if not exist "%full_path%" (echo this file does not exist...)&goto :eof

cscript //nologo sed.vbs s/%str_search%/"%str_replace%"/ <%input_file% >%output_file%

Works like a charm :)

Community
  • 1
  • 1
Natan
  • 1
  • 1