March 19, 2015 at 4:00 pm
Hi Experts,
Please help me getting a double quote in between the string. Thereafter I will write a logic to delete that specific folder which I can easily manage through test-path.
$INSTALLSHAREDDIR = Get-Content "C:\test\Install_ConfigurationFile.ini" | Where-Object {$_ -like'INSTALLSHAREDDIR=*'}
##O/P : INSTALLSHAREDDIR="c:\Program Files\Microsoft SQL Server"
$INSTALLSHAREDDIR = Get-Content "C:\test\Install_ConfigurationFile.ini" | Where-Object {$_ -like'INSTALLSHAREDDIR=*'}
$INSTALLSHAREDDIR = $INSTALLSHAREDDIR + "\MSSQL11.INST3"
$INSTALLSHAREDDIR1 = $INSTALLSHAREDDIR -replace '"', """"
o/P: INSTALLSHAREDDIR=c:\Program Files\Microsoft SQL Server\MSSQL11.INST3
Want the output as (within double quotes):-
"c:\Program Files\Microsoft SQL Server\MSSQL11.INST3"
Thanks
Thanks.
March 22, 2015 at 2:21 am
$INSTALLSHAREDDIR = Get-Content "C:\test\Install_ConfigurationFile.ini" | Where-Object {$_ -like'INSTALLSHAREDDIR=*'}
$INSTALLSHAREDDIR = "'"" + $INSTALLSHAREDDIR + "\MSSQL11.INST3'""
What I did was escape " with '.
Gaz
-- Stop your grinnin' and drop your linen...they're everywhere!!!
March 23, 2015 at 9:31 am
Thanks GAry for the help.
Somehow, your script is throwing an error.
Msg:
The string is missing the terminator: '.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
However, I wrote the below one and it works.
$INSTALLSHAREDDIR = Get-Content "C:\test\Install_ConfigurationFile.ini" | Where-Object {$_ -like'INSTALLSHAREDDIR=*'}
$INSTALLSHAREDDIR = $INSTALLSHAREDDIR + "\MSSQL11.INST30"
$INSTALLSHAREDDIR1 = $INSTALLSHAREDDIR -replace '"', ""
$INSTALLSHAREDDIR1
Thanks.
March 23, 2015 at 10:34 am
Well done.
In my defence I was supporting with only my phone to hand 😉
Gaz
-- Stop your grinnin' and drop your linen...they're everywhere!!!
March 23, 2015 at 11:03 am
This is my more refined script, "Gaz" Sir..
$INSTALLSHAREDDIR = Get-Content "C:\test\Install_ConfigurationFile.ini" | Where-Object {$_ -match 'INSTALLSHAREDDIR='}
$pos = $INSTALLSHAREDDIR.IndexOf("=")
$leftPart = $INSTALLSHAREDDIR.Substring(0, $pos)
$rightPart = $INSTALLSHAREDDIR.Substring($pos+1)
#$leftPart
$rightPart = $rightPart -replace '"', ""
$rightPart
$rightPart = $rightPart + "\MSSQL11.INST3"
$rightPart
if (Test-Path $rightPart) {
write-host "Yes"
Write-Host "Cleaning up target Script folder "$rightPart""
Remove-Item -Path "$rightPart" -Force -Recurse
$rightPart
}
Else
{
write-host "Path did not found"
}
Thanks.
March 23, 2015 at 11:40 am
Now doesn't that look better!!!
Gaz
-- Stop your grinnin' and drop your linen...they're everywhere!!!
March 23, 2015 at 11:47 am
Further modified. Please check that in your place and let me know how does it look now. 🙂
********************************************************************************
clear
function getPATH {
Param ([string]$name=(Read-Host "Enter Instancename"))
$length = $name;
IF (!$length)
{
write-host "***** NO Input Provided.*****"
Break
}
ELSE
{write-host "You entered***:"$name.ToUpper();}
$INSTALLSHAREDDIR = Get-Content "C:\test\Install_ConfigurationFile.ini" | Where-Object {$_ -match 'INSTALLSHAREDDIR='}
$pos = $INSTALLSHAREDDIR.IndexOf("=")
$leftPart = $INSTALLSHAREDDIR.Substring(0, $pos)
$rightPart = $INSTALLSHAREDDIR.Substring($pos+1)
#$leftPart
$rightPart = $rightPart -replace '"', ""
#$rightPart
$rightPart = $rightPart + "\MSSQL11.$name"
#$rightPart
if (Test-Path $rightPart) {
write-host "**** Folder Does Exist in the path **** "
#Write-Host "Cleaning up target Script folder "$rightPart""
#$result = $host.ui.PromptForChoice($title, $message, $options, 0)
#Remove-Item -Path "$rightPart" -Force -Recurse
#$rightPart
}
Else
{
write-host "****** Folder Does NOT Exist in the path *****"
}
$title = "Delete Files"
$message = "Do you want to delete the remaining files in the folder?"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
"Deletes all the files in the folder."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
"Retains all the files in the folder."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
switch ($result)
{
0 {"You selected Yes."}
1 {"You selected No."}
}
if ($result -eq 0) {
Write-Host "Cleaning up target Script folder "$rightPart""
Remove-Item -Path "$rightPart" -Force -Recurse
$rightPart
}
Else
{
write-host "****** FOLDER Found in the Path but Instructed NOT to Delete. *****"
}
}
Thanks.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply