find and replace text in a text file (including spaces!)

I have been using F.A.R.T and simple batch to edit a txt file with the out put of the net use command:

in my text file in need to change often I have a defined path changeme\software\sql

in the batch net use shows location of a shared folder on the net work \server\shared

I "mark" this in the CMD window and paste into user input prompt

then FART will replace changeme with \server\shared to complete fully into \server\shared\software\sql

this has been working a treat, till today where there was a space in the path to the shared

\server\shared area

and FART doesn't understand spaces and breaks with syntax error's and things. I looked into all the switches and none seem to be let me replace text with a space in it.

Is there a better way to do this, better tool, seen power shell is good, but I don't know what versions im going to arrive at so could be more trouble changing the codes to suit the versions.

Wonder I can do this purely in batch without farting. Many thanks

echo.
type "%~dp0logo.txt"
echo.
echo.
echo F| XCOPY "%~dp0Software\client.txt" /y "%~dp0DSoftware\config.cfg" 
net use
pause
set /p N=enter shared:
"%~dp0Software\fart" -i -r "%~dp0Software\config.cfg" \\changeme %N%

Solution 1:

POWERSHELL

Example PowerShell syntax below for find and replacement of strings within a specific file.

EXAMPLE

(Get-Content "C:\Users\user\Desktop\testtestzzz.txt") | 
Foreach-Object {$_.replace("\\changeme", "\\server\share name")} | 
Set-Content "C:\Users\user\Desktop\test\testzzz.txt"

Resources:

  • http://ss64.com/ps/replace.html
  • http://blogs.technet.com/b/heyscriptingguy/archive/2008/01/17/how-can-i-use-windows-powershell-to-replace-characters-in-a-text-file.aspx

F.A.R.T

For the F.A.R.T issue with spaces specifically, I think you just need to add double-quotes around the strings you're passing to it. Give that a shot and then perhaps you can just continue to keep using it without further change.

EXAMPLE

"%~dp0Software\fart" -i -r "%~dp0Software\config.cfg" "\\changeme" "%N%"

You can also add the capital -V switch (see below example) to get verbose detail of the error message if needed to troubleshoot further, but I've resolved this issue with F.A.R.T and a space in a replacement string by adding the double-quotes around it as in my below example #2.

Example #2

fart -i -C -V "C:\Users\user\Desktop\test\testzzz.txt" "\\changeme" "\\server\share name"

You also may want to consider excluding the -r option if you're only changing the text in a SINGLE file (i.e. config.cfg). Below is the FART /? options I see exaplaining the options.

Usage: FART [options] [--] <wildcard>[,...] [find_string] [replace_string]

Options:
 -h, --help          Show this help message (ignores other options)
 -q, --quiet         Suppress output to stdio / stderr
 -V, --verbose       Show more information
 -r, --recursive     Process sub-folders recursively
 -c, --count         Only show filenames, match counts and totals
 -i, --ignore-case   Case insensitive text comparison
 -v, --invert        Print lines NOT containing the find string
 -n, --line-number   Print line number before each line (1-based)
 -w, --word          Match whole word (uses C syntax, like grep)
 -f, --filename      Find (and replace) filename instead of contents
 -B, --binary        Also search (and replace) in binary files (CAUTION)
 -C, --c-style       Allow C-style extended characters (\xFF\0\t\n\r\\ etc.)
     --cvs           Skip cvs dirs; execute "cvs edit" before changing files
     --svn           Skip svn dirs
     --remove        Remove all occurences of the find_string
 -a, --adapt         Adapt the case of replace_string to found string
 -b, --backup        Make a backup of each changed file
 -p, --preview       Do not change the files but print the changes