header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Insert double quotes into string
Last Post 04 Dec 2008 10:58 PM by EBGreen. 3 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
clay202User is Offline
New Member
New Member
Posts:7
Avatar

--
30 Nov 2008 06:42 AM  
Hi there, I'm a newbie to PowerShell (and novice to general scripting)-- I'm writing a script to deploy code to our websites... I'm basically doing a "Copy-item..." of each file.

So far so good with my script but I notice the script doesn't like a space in the folder or filename.  I need to be able to insert a double quote at the beginnign of the string and at the end of the string so that when the "Copy-Item" looks at the source full filename, it won't choke on spaces in the folder or filename.

My questions are this:
(1) how do I insert a double quote into the string?
(2) what is the syntax to insert the double quote at the beginning of the string and at the end of the string ?

Thanks... as I write more scripts, it helps build up my skills.


halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:334
Avatar

--
30 Nov 2008 02:03 PM  
Glad you found us. You probably do not need to worry about this. If you rely on passing the file objects rather than the file names, then the problem goes away. For example:

get-childitem myfiles* | copy-item -dest c:\other|path


If you really do need to work with the filenames in the way you describe, then do something like this:

copy-item -path "$filename" -dest $path

Does that help?
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
clay202User is Offline
New Member
New Member
Posts:7
Avatar

--
04 Dec 2008 10:39 PM  
halr9000, thanks for the quick response. 

lol -- that sounds pretty darn easy.  I think that should work.

thanks again
EBGreenUser is Offline
New Member
New Member
Posts:61
Avatar

--
04 Dec 2008 10:58 PM  
Hal is of course completely right about how to handle file/path names. Just to help with the learning though, here are some ways to add double quotes if you ever need to for some other reason:

Let's make a variable named $foo and stick a string in it:
$foo = "This is my string"

If we write this out to the command line we get the string with no quotes of course:
Write-Host $foo
this is my string

Double quotes cause variables to be interpolated, so:
Write-Host "$foo"
This is my string

To add double quotes, one method is to use the escape character within other double quotes. In PS, the ` (grave) is the escape character, so:
Write-Host "`"$foo`""
"This is my string"

The single quote character will generate a string without interpolating the variable, so:
Write-Host '$foo'
$foo

One more way to get double quotes around your string is to use the string formatting that is built into the string object in .Net. So:
Write-Host ('"{0}"' -f $foo)


Hope this helps.
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer