How to concat path and file name for creation and removal

放肆的年华 提交于 2019-12-11 12:40:15

问题


I'm trying the following.

$woohoo = "c:\temp\shabang"
New-Item -ItemType File $woohoo

This creates a file at desired location. However, I'd like to split the $woo and $hoo and go something like this.

$woo = "c:\temp"
$hoo = "shabang"
New-Item -ItemType File ($woo + $hoo)

This doesn't work and I need a hint on how to make it fly. This suggestion nor this on worked out when applied to my situation. Apparently - given path format is unsupported.

Suggestions?

Edit

This is what it looks like:

Edit 2

$woo = "c:\temp"; $hoo= "shabang"; New-Item -ItemType File (Join-Path $woo $hoo)


回答1:


Doing $woo + $hoo does not return the proper filepath:

PS > $woo = "c:\temp"
PS > $hoo = "shabang"
PS > $woo + $hoo
c:\tempshabang
PS >

Instead, you should use the Join-Path cmdlet to concatenate $woo and $hoo:

New-Item -ItemType File (Join-Path $woo $hoo)

See a demonstration below:

PS > $woo = "c:\temp"
PS > $hoo = "shabang"
PS > Join-Path $woo $hoo
c:\temp\shabang
PS >


来源:https://stackoverflow.com/questions/24456429/how-to-concat-path-and-file-name-for-creation-and-removal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!