Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 变量连接顺序错误_String_Powershell_Filepath - Fatal编程技术网

String 变量连接顺序错误

String 变量连接顺序错误,string,powershell,filepath,String,Powershell,Filepath,我有以下资料: function createFolder($folderName, $curPath) { $dest = $curPath + $folderName write-host "Path is : " + $dest # code to mess around with files etc } 当我运行此命令时,它会提供以下输出: 路径是:+Test\u文件夹C:\Users\Me +运算符或用于此类函数的join/concat方法是否缺少什么?在P

我有以下资料:

function createFolder($folderName, $curPath)
{
    $dest = $curPath + $folderName
    write-host "Path is : " + $dest
    # code to mess around with files etc
}
当我运行此命令时,它会提供以下输出:

路径是:+Test\u文件夹C:\Users\Me
+
运算符或用于此类函数的join/concat方法是否缺少什么?在PowerShell中创建/连接/操作路径的正确方法是什么(我刚刚开始使用它来自动化桌面上的一些清理任务)

编辑:如果有区别,这是我在运行version命令时看到的:

PS C:\Users\Me> version
BladeLogic Network Shell 8.2.01.273 (Release) [May 12 2012 21:56:02]
Copyright (C) 1996-2012 BladeLogic Inc.
而且,我在一台没有管理权限的工作计算机上

我试过:

$currentPath = "C:\Users\n0223270\Downloads"
$test = "test"
createFolder($test, $currentPath)

function createFolder($folderName, $curPath)
{
    $dest = join-path -path $curPath -childpath $folderName
    Write-Host $dest   
}
这是以下错误:

Join Path:无法将参数绑定到参数“Path”,因为它为null。
第4行字符:28
+$dest=连接路径-路径
createFolder($test,$currentPath)

这不是调用Powershell函数的方式。这是以两个字符串的数组形式传递第一个参数。第二个参数将为null,因为它没有指定,也没有默认值

尝试:

createFolder($test,$currentPath)

这不是调用Powershell函数的方式。这是以两个字符串的数组形式传递第一个参数。第二个参数将为null,因为它没有指定,也没有默认值

尝试:


尝试使用Join Path cmdlet而不是字符串连接。什么是
$curPath
?什么是
$folderName
+
不是字符串concat,您可以将变量放入字符串或使用
-f
将变量格式化为字符串(通常更好)。$curPath是当前工作目录,因此在上面的示例中:$curPath=“C:\Users\Me”$folderName是要创建的文件夹的名称,如果该文件夹不存在,并用作稍后在函数中复制和粘贴作业的目标:$folderName=“Test_folder”@EBGreen中:$folderName=“Test_folder”@EBGreen我尝试了Join Path cmdlet,更新了问题,结果是您正在使用paren和逗号调用powerShell函数。尝试
createFolder$test$currentPath
尝试使用Join-Path cmdlet而不是字符串连接。什么是
$curPath
?什么是
$folderName
+
不是字符串concat,您可以将变量放入字符串或使用
-f
将变量格式化为字符串(通常更好)。$curPath是当前工作目录,因此在上面的示例中:$curPath=“C:\Users\Me”$folderName是要创建的文件夹的名称,如果该文件夹不存在,并用作稍后在函数中复制和粘贴作业的目标:$folderName=“Test_folder”@EBGreen中:$folderName=“Test_folder”@EBGreen我尝试了Join Path cmdlet,更新了问题,结果是您正在使用paren和逗号调用powerShell函数。请尝试
createFolder$test$currentPath
Ok,效果很好,谢谢。仍在尝试从Java后台迁移到powershellscripting@jbailie1991如果您以前没有遇到过它,这肯定是不明显的,因为该语言非常类似C语言。IMX,使用它的次数越多,就越会将命名参数用于
param()
构造,而不是类似C的函数定义。请参阅
获取有关函数的帮助*
文章。好的,效果很好,谢谢。仍在尝试从Java后台迁移到powershellscripting@jbailie1991如果您以前没有遇到过它,这肯定是不明显的,因为该语言非常类似C语言。IMX,使用它的次数越多,就越会将命名参数用于
param()
构造,而不是类似C的函数定义。请参阅
获取有关函数的帮助*
文章。
createFolder $test $currentPath