Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Using the replace command when text contains : (semi-colon)

$
0
0

New user to powershell here.  

I have to replace text, that is a directory path with another directory path, in a file (dbid.bcp) 

Basically I need to replace this:

N:\FooBar\File Directoryr\Repository\qc\File_Directory_1

with:

M:\FooBar\File Directory\Repository\qc\Archived_File_Directory_1

I am trying to use this command:

(get-content dbid.bcp) | {$_ -replace "N:\FooBar\File Directoryr\Repository\qc\File_Directory_1", "M:\FooBar\File Directory\Repository\qc\Archived_File_Directory_1"} | (set-content dbid.bcp)

In essence I really only have to replace N: with M: and File_Directory_1 with Archived_File_Directory_1.  

I tried the following to attempt to escape out the 2 instances of the : character.

(get-content dbid.bcp) | {$_ -replace "N`:\FooBar\File Directoryr\Repository\qc\File_Directory_1", "M`:\FooBar\File Directory\Repository\qc\Archived_File_Directory_1"} | (set-content dbid.bcp)

I am getting 2 errors.  

The first is a parser error on the :  (I tried using the ` character before each of the :'s to no avail)

The second error is: Expressions are only allowed as the first element of a pipeline.  This is getting reported at the second | character.

I have a second question once I get this resolved.  I have 3 files in the subdirectory I am trying to do this replace in.  All 3 files have the same prefix "dbid", but have 3 different suffixes.  bcp, prod and xml.  Is there a way to code this to change all 3 with some sort of wildcard in the get-content parameter?

Any help would be greatly appreciated.

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles