Replace filepath string in file

Multi tool use
I have the following script that gets the default log and data locations for an SQL server:
$Server = '.DEV_MIGRATIONS'
$SMOServer = new-object ('Microsoft.SqlServer.Management.Smo.Server') $Server
# Get the Default File Locations
### Get log and data locations
$DefaultFileLocation = $SMOServer.Settings.DefaultFile
$DefaultLogLocation = $SMOServer.Settings.DefaultLog
if ($DefaultFileLocation.Length -eq 0) $DefaultFileLocation = $SMOServer.Information.MasterDBPath
if ($DefaultLogLocation.Length -eq 0) $DefaultLogLocation = $SMOServer.Information.MasterDBLogPath
$Schema_DataLocation = ($DefaultFileLocation + "Test.mdf")
$Schema_DataLocation
[Regex]::Escape($Schema_DataLocation)
I am trying to use the $Schema_DataLocation
in a replace function for a schema creation script but i get errors when trying to replace the path which requires escaping regex.
What i get from the [Regex]::Escape
call is:
C:\Program Files\Microsoft SQL Server\MSSQL14.DEV_MIGRATIONS\MSSQL\DATA\Test.mdf
instead of:
C:Program FilesMicrosoft SQL ServerMSSQL14.DEV_MIGRATIONSMSSQLDATATest.mdf
the replace commands:
(Get-Content $Script_SchemaCreate) |
Foreach-Object $_ -replace "DFILEPATH", $Schema_DataLocation |
Set-Content $Script_SchemaCreate
(Get-Content $Script_SchemaCreate) |
Foreach-Object $_ -replace [Regex]::Escape($Schema_DataLocation), "DFILEPATH" |
Set-Content $Script_SchemaCreate
The first replace works, but the second fails because it is trying to match a different value.
Removing [Regex]::Escape
i get the following error:
The regular expression pattern C:Program FilesMicrosoft SQL ServerMSSQL14.DEV_MIGRATIONSMSSQLDATAMigration_Data.mdf is not valid.
regex powershell replace
add a comment |
I have the following script that gets the default log and data locations for an SQL server:
$Server = '.DEV_MIGRATIONS'
$SMOServer = new-object ('Microsoft.SqlServer.Management.Smo.Server') $Server
# Get the Default File Locations
### Get log and data locations
$DefaultFileLocation = $SMOServer.Settings.DefaultFile
$DefaultLogLocation = $SMOServer.Settings.DefaultLog
if ($DefaultFileLocation.Length -eq 0) $DefaultFileLocation = $SMOServer.Information.MasterDBPath
if ($DefaultLogLocation.Length -eq 0) $DefaultLogLocation = $SMOServer.Information.MasterDBLogPath
$Schema_DataLocation = ($DefaultFileLocation + "Test.mdf")
$Schema_DataLocation
[Regex]::Escape($Schema_DataLocation)
I am trying to use the $Schema_DataLocation
in a replace function for a schema creation script but i get errors when trying to replace the path which requires escaping regex.
What i get from the [Regex]::Escape
call is:
C:\Program Files\Microsoft SQL Server\MSSQL14.DEV_MIGRATIONS\MSSQL\DATA\Test.mdf
instead of:
C:Program FilesMicrosoft SQL ServerMSSQL14.DEV_MIGRATIONSMSSQLDATATest.mdf
the replace commands:
(Get-Content $Script_SchemaCreate) |
Foreach-Object $_ -replace "DFILEPATH", $Schema_DataLocation |
Set-Content $Script_SchemaCreate
(Get-Content $Script_SchemaCreate) |
Foreach-Object $_ -replace [Regex]::Escape($Schema_DataLocation), "DFILEPATH" |
Set-Content $Script_SchemaCreate
The first replace works, but the second fails because it is trying to match a different value.
Removing [Regex]::Escape
i get the following error:
The regular expression pattern C:Program FilesMicrosoft SQL ServerMSSQL14.DEV_MIGRATIONSMSSQLDATAMigration_Data.mdf is not valid.
regex powershell replace
add a comment |
I have the following script that gets the default log and data locations for an SQL server:
$Server = '.DEV_MIGRATIONS'
$SMOServer = new-object ('Microsoft.SqlServer.Management.Smo.Server') $Server
# Get the Default File Locations
### Get log and data locations
$DefaultFileLocation = $SMOServer.Settings.DefaultFile
$DefaultLogLocation = $SMOServer.Settings.DefaultLog
if ($DefaultFileLocation.Length -eq 0) $DefaultFileLocation = $SMOServer.Information.MasterDBPath
if ($DefaultLogLocation.Length -eq 0) $DefaultLogLocation = $SMOServer.Information.MasterDBLogPath
$Schema_DataLocation = ($DefaultFileLocation + "Test.mdf")
$Schema_DataLocation
[Regex]::Escape($Schema_DataLocation)
I am trying to use the $Schema_DataLocation
in a replace function for a schema creation script but i get errors when trying to replace the path which requires escaping regex.
What i get from the [Regex]::Escape
call is:
C:\Program Files\Microsoft SQL Server\MSSQL14.DEV_MIGRATIONS\MSSQL\DATA\Test.mdf
instead of:
C:Program FilesMicrosoft SQL ServerMSSQL14.DEV_MIGRATIONSMSSQLDATATest.mdf
the replace commands:
(Get-Content $Script_SchemaCreate) |
Foreach-Object $_ -replace "DFILEPATH", $Schema_DataLocation |
Set-Content $Script_SchemaCreate
(Get-Content $Script_SchemaCreate) |
Foreach-Object $_ -replace [Regex]::Escape($Schema_DataLocation), "DFILEPATH" |
Set-Content $Script_SchemaCreate
The first replace works, but the second fails because it is trying to match a different value.
Removing [Regex]::Escape
i get the following error:
The regular expression pattern C:Program FilesMicrosoft SQL ServerMSSQL14.DEV_MIGRATIONSMSSQLDATAMigration_Data.mdf is not valid.
regex powershell replace
I have the following script that gets the default log and data locations for an SQL server:
$Server = '.DEV_MIGRATIONS'
$SMOServer = new-object ('Microsoft.SqlServer.Management.Smo.Server') $Server
# Get the Default File Locations
### Get log and data locations
$DefaultFileLocation = $SMOServer.Settings.DefaultFile
$DefaultLogLocation = $SMOServer.Settings.DefaultLog
if ($DefaultFileLocation.Length -eq 0) $DefaultFileLocation = $SMOServer.Information.MasterDBPath
if ($DefaultLogLocation.Length -eq 0) $DefaultLogLocation = $SMOServer.Information.MasterDBLogPath
$Schema_DataLocation = ($DefaultFileLocation + "Test.mdf")
$Schema_DataLocation
[Regex]::Escape($Schema_DataLocation)
I am trying to use the $Schema_DataLocation
in a replace function for a schema creation script but i get errors when trying to replace the path which requires escaping regex.
What i get from the [Regex]::Escape
call is:
C:\Program Files\Microsoft SQL Server\MSSQL14.DEV_MIGRATIONS\MSSQL\DATA\Test.mdf
instead of:
C:Program FilesMicrosoft SQL ServerMSSQL14.DEV_MIGRATIONSMSSQLDATATest.mdf
the replace commands:
(Get-Content $Script_SchemaCreate) |
Foreach-Object $_ -replace "DFILEPATH", $Schema_DataLocation |
Set-Content $Script_SchemaCreate
(Get-Content $Script_SchemaCreate) |
Foreach-Object $_ -replace [Regex]::Escape($Schema_DataLocation), "DFILEPATH" |
Set-Content $Script_SchemaCreate
The first replace works, but the second fails because it is trying to match a different value.
Removing [Regex]::Escape
i get the following error:
The regular expression pattern C:Program FilesMicrosoft SQL ServerMSSQL14.DEV_MIGRATIONSMSSQLDATAMigration_Data.mdf is not valid.
regex powershell replace
regex powershell replace
edited Nov 15 '18 at 23:33
mklement0
131k20245281
131k20245281
asked Nov 15 '18 at 4:31
Owain EsauOwain Esau
886718
886718
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Don't use [regex]::Escape()
to escape the replacement string in a -replace
operation - it isn't a regular expression, and has no special meaning inside of it.
Instead, manually escape $
chars. as $$
, because $
does have special meaning in the replacement operand, namely to refer to results from the matching operation, notably capture-group results, as detailed in this answer.
Performing this manual escaping with -replace
is somewhat tricky, because $
is special both in a regex and in the replacement operand, with different escaping requirements:
# Escape a string for use as the replacementt string
# for another -replace call:
<string> -replace '$', '$$$$' # replace literal '$' with literal '$$'
Therefore, in this case it may be simpler to use the string-literal .Replace()
method:
<string>.Replace('$', '$$') # replace literal '$' with literal '$$'
Here's a roundtrip example:
$str = 'c:program filesa-name-with-$-in-it'
# Perform substitution and output.
($new = '[DFILEPATH]' -replace 'DFILEPATH', $str.Replace('$', '$$'))
# Perform the inverse replacement.
$new -replace [regex]::Escape($str), 'DFILEPATH'
The above yields the following, proving that the substitution worked as intended:
[c:program filesa-name-with-$-in-it]
[DFILEPATH]
1
Glad to hear it, @OwainEsau; my pleasure.
– mklement0
Nov 15 '18 at 23:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312475%2freplace-filepath-string-in-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Don't use [regex]::Escape()
to escape the replacement string in a -replace
operation - it isn't a regular expression, and has no special meaning inside of it.
Instead, manually escape $
chars. as $$
, because $
does have special meaning in the replacement operand, namely to refer to results from the matching operation, notably capture-group results, as detailed in this answer.
Performing this manual escaping with -replace
is somewhat tricky, because $
is special both in a regex and in the replacement operand, with different escaping requirements:
# Escape a string for use as the replacementt string
# for another -replace call:
<string> -replace '$', '$$$$' # replace literal '$' with literal '$$'
Therefore, in this case it may be simpler to use the string-literal .Replace()
method:
<string>.Replace('$', '$$') # replace literal '$' with literal '$$'
Here's a roundtrip example:
$str = 'c:program filesa-name-with-$-in-it'
# Perform substitution and output.
($new = '[DFILEPATH]' -replace 'DFILEPATH', $str.Replace('$', '$$'))
# Perform the inverse replacement.
$new -replace [regex]::Escape($str), 'DFILEPATH'
The above yields the following, proving that the substitution worked as intended:
[c:program filesa-name-with-$-in-it]
[DFILEPATH]
1
Glad to hear it, @OwainEsau; my pleasure.
– mklement0
Nov 15 '18 at 23:48
add a comment |
Don't use [regex]::Escape()
to escape the replacement string in a -replace
operation - it isn't a regular expression, and has no special meaning inside of it.
Instead, manually escape $
chars. as $$
, because $
does have special meaning in the replacement operand, namely to refer to results from the matching operation, notably capture-group results, as detailed in this answer.
Performing this manual escaping with -replace
is somewhat tricky, because $
is special both in a regex and in the replacement operand, with different escaping requirements:
# Escape a string for use as the replacementt string
# for another -replace call:
<string> -replace '$', '$$$$' # replace literal '$' with literal '$$'
Therefore, in this case it may be simpler to use the string-literal .Replace()
method:
<string>.Replace('$', '$$') # replace literal '$' with literal '$$'
Here's a roundtrip example:
$str = 'c:program filesa-name-with-$-in-it'
# Perform substitution and output.
($new = '[DFILEPATH]' -replace 'DFILEPATH', $str.Replace('$', '$$'))
# Perform the inverse replacement.
$new -replace [regex]::Escape($str), 'DFILEPATH'
The above yields the following, proving that the substitution worked as intended:
[c:program filesa-name-with-$-in-it]
[DFILEPATH]
1
Glad to hear it, @OwainEsau; my pleasure.
– mklement0
Nov 15 '18 at 23:48
add a comment |
Don't use [regex]::Escape()
to escape the replacement string in a -replace
operation - it isn't a regular expression, and has no special meaning inside of it.
Instead, manually escape $
chars. as $$
, because $
does have special meaning in the replacement operand, namely to refer to results from the matching operation, notably capture-group results, as detailed in this answer.
Performing this manual escaping with -replace
is somewhat tricky, because $
is special both in a regex and in the replacement operand, with different escaping requirements:
# Escape a string for use as the replacementt string
# for another -replace call:
<string> -replace '$', '$$$$' # replace literal '$' with literal '$$'
Therefore, in this case it may be simpler to use the string-literal .Replace()
method:
<string>.Replace('$', '$$') # replace literal '$' with literal '$$'
Here's a roundtrip example:
$str = 'c:program filesa-name-with-$-in-it'
# Perform substitution and output.
($new = '[DFILEPATH]' -replace 'DFILEPATH', $str.Replace('$', '$$'))
# Perform the inverse replacement.
$new -replace [regex]::Escape($str), 'DFILEPATH'
The above yields the following, proving that the substitution worked as intended:
[c:program filesa-name-with-$-in-it]
[DFILEPATH]
Don't use [regex]::Escape()
to escape the replacement string in a -replace
operation - it isn't a regular expression, and has no special meaning inside of it.
Instead, manually escape $
chars. as $$
, because $
does have special meaning in the replacement operand, namely to refer to results from the matching operation, notably capture-group results, as detailed in this answer.
Performing this manual escaping with -replace
is somewhat tricky, because $
is special both in a regex and in the replacement operand, with different escaping requirements:
# Escape a string for use as the replacementt string
# for another -replace call:
<string> -replace '$', '$$$$' # replace literal '$' with literal '$$'
Therefore, in this case it may be simpler to use the string-literal .Replace()
method:
<string>.Replace('$', '$$') # replace literal '$' with literal '$$'
Here's a roundtrip example:
$str = 'c:program filesa-name-with-$-in-it'
# Perform substitution and output.
($new = '[DFILEPATH]' -replace 'DFILEPATH', $str.Replace('$', '$$'))
# Perform the inverse replacement.
$new -replace [regex]::Escape($str), 'DFILEPATH'
The above yields the following, proving that the substitution worked as intended:
[c:program filesa-name-with-$-in-it]
[DFILEPATH]
edited Nov 15 '18 at 13:56
answered Nov 15 '18 at 13:38
mklement0mklement0
131k20245281
131k20245281
1
Glad to hear it, @OwainEsau; my pleasure.
– mklement0
Nov 15 '18 at 23:48
add a comment |
1
Glad to hear it, @OwainEsau; my pleasure.
– mklement0
Nov 15 '18 at 23:48
1
1
Glad to hear it, @OwainEsau; my pleasure.
– mklement0
Nov 15 '18 at 23:48
Glad to hear it, @OwainEsau; my pleasure.
– mklement0
Nov 15 '18 at 23:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312475%2freplace-filepath-string-in-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
gAP5,ZgwIV4Su 3aGYsJvimXinzILZNthyPbj7vVLuuLy3n3BcLWc WF1SKbtIDSRQ,AcIz 2GBl mIm BIWsMwtQ,3 L0GhaQx53kT3A0