| 1 | = Replace Text Example = |
---|
| 2 | |
---|
| 3 | ''Part of'' StandardLibraryCategory |
---|
| 4 | |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | == Description == |
---|
| 8 | |
---|
| 9 | Take the input file and replace one string with another (repeatedly). |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | == Example == |
---|
| 14 | |
---|
| 15 | {{{ |
---|
| 16 | #!d |
---|
| 17 | import std.stream; /* for File */ |
---|
| 18 | import std.string; /* for replace */ |
---|
| 19 | import std.stdio; /* for writefln */ |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | void main(char[][] args) |
---|
| 24 | { |
---|
| 25 | char[] fromFileStr; |
---|
| 26 | char[] toFileStr; |
---|
| 27 | |
---|
| 28 | char[] fromStr; |
---|
| 29 | char[] toStr; |
---|
| 30 | |
---|
| 31 | char[] fileStr; |
---|
| 32 | |
---|
| 33 | char[] lnStr; |
---|
| 34 | |
---|
| 35 | if(args.length < 5) |
---|
| 36 | { |
---|
| 37 | writefln(`Usage: replace_text.exe fileIn fileOut "from" "to"` \n); |
---|
| 38 | return; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | debug writefln("args.length: %s", args.length); |
---|
| 42 | debug for(int i = 0; i < args.length; i++) writefln("args[%s]: %s", i, args[i]); |
---|
| 43 | |
---|
| 44 | fromFileStr = args[1]; |
---|
| 45 | toFileStr = args[2]; |
---|
| 46 | fromStr = args[3]; |
---|
| 47 | toStr = args[4]; |
---|
| 48 | |
---|
| 49 | File inFile = new File(fromFileStr); |
---|
| 50 | while (!inFile.eof()) |
---|
| 51 | { |
---|
| 52 | lnStr = inFile.readLine(); |
---|
| 53 | debug writefln("%s", lnStr); |
---|
| 54 | fileStr ~= lnStr ~ \n; |
---|
| 55 | } |
---|
| 56 | inFile.close(); |
---|
| 57 | |
---|
| 58 | fileStr = replace(fileStr, fromStr, toStr); |
---|
| 59 | |
---|
| 60 | File outFile = new File(); |
---|
| 61 | with(outFile) |
---|
| 62 | { |
---|
| 63 | outFile.create(toFileStr); |
---|
| 64 | outFile.writeString(fileStr); |
---|
| 65 | outFile.close(); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | return; |
---|
| 69 | } |
---|
| 70 | }}} |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | == Batch File == |
---|
| 75 | |
---|
| 76 | {{{ |
---|
| 77 | @echo off |
---|
| 78 | dmd ReplaceTextExample.d |
---|
| 79 | ReplaceTextExample.exe |
---|
| 80 | ReplaceTextExample.exe "ReplaceTextExample.d" "ReplaceTextExampleX.d" "Str" "String" |
---|
| 81 | pause |
---|
| 82 | }}} |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | == Compatibility Notes == |
---|
| 87 | |
---|
| 88 | Tested with Digital Mars D Compiler v0.162 on Windows 2000. |