将文件夹名称添加到数组中的路径
Prepend folder name to paths in an array
我希望使用 foreach 语句将文件夹名称添加到(相对)路径数组的开头,但它没有对数组进行任何更改(也没有错误)
注意:这更多的是出于教育目的,而不是功能性的,因为我使用我已经注释掉的 for 循环工作,但我有兴趣了解 foreach 语句的工作原理
$myFiles = @(
"blah1\blah2\file1.txt"
"blah3\blah4\file2.txt"
"blah5\blah6\file3.txt"
)
$checkoutFolder = "folder1"
#for ($h = 0; $h -lt $myFiles.Length; $h++) {
#$myFiles[$h] = $checkoutFolder + "\" + $myFiles[$h]
#}
foreach ($path in $myFiles) {
$path = $checkoutFolder + "\" + $path
}
$myFiles
我也试过使用缓冲区变量,例如
$buffer = $checkoutFolder + "\" + $path
$path = $buffer
但结果相同,即
输出:
blah1\blah2\file1.txt
blah3\blah4\file2.txt
blah5\blah6\file3.txt
最好开始使用 Join-Path
cmdlet,以避免创建省略或加倍反斜杠的路径。
像这样就可以了:
$checkoutFolder = "folder1"
$myFiles = "blah1\blah2\file1.txt", "blah3\blah4\file2.txt", "blah5\blah6\file3.txt" | ForEach-Object {
Join-Path -Path $checkoutFolder -ChildPath $_
}
$myFiles
输出:
folder1\blah1\blah2\file1.txt
folder1\blah3\blah4\file2.txt
folder1\blah5\blah6\file3.txt
我想到了两个办法:
使用旧数组的修改数据创建新数组
$myFiles = @(
"blah1\blah2\file1.txt"
"blah3\blah4\file2.txt"
"blah5\blah6\file3.txt"
)
$checkoutFolder = "folder1"
#Create new array $myFilesnew
$myFilesnew = @()
#For each line in in old array
foreach ($file in $myFiles)
{
#Create new row from modied row $file of $myFiles array
$row = $checkoutFolder+"\"+$file
#Add row $row to a new array $myFilesnew
$myFilesnew+=$row
}
$myFilesnew
修改现有数组的每一行:
$myFiles = @(
"blah1\blah2\file1.txt"
"blah3\blah4\file2.txt"
"blah5\blah6\file3.txt"
)
$checkoutFolder = "folder1"
$i=0
while($i-lt $myFiles.Count)
{
#Get $i row $myFiles[$i] from aray, perform insert of modified data, write data back to $myFiles[$i] row of the array
$myfiles[$i]=$myFiles[$i].Insert(0,$checkoutFolder+"\");
#Add +1 to $i
$i++
}
$myFiles
您可以将每个字符串的正则表达式开头替换为文件夹名称。这也是生成计算机名的有用习惯用法。
'blah1\blah2\file1.txt','blah3\blah4\file2.txt','blah5\blah6\file3.txt' -replace '^','folder1\'
folder1\blah1\blah2\file1.txt
folder1\blah3\blah4\file2.txt
folder1\blah5\blah6\file3.txt
我希望使用 foreach 语句将文件夹名称添加到(相对)路径数组的开头,但它没有对数组进行任何更改(也没有错误)
注意:这更多的是出于教育目的,而不是功能性的,因为我使用我已经注释掉的 for 循环工作,但我有兴趣了解 foreach 语句的工作原理
$myFiles = @(
"blah1\blah2\file1.txt"
"blah3\blah4\file2.txt"
"blah5\blah6\file3.txt"
)
$checkoutFolder = "folder1"
#for ($h = 0; $h -lt $myFiles.Length; $h++) {
#$myFiles[$h] = $checkoutFolder + "\" + $myFiles[$h]
#}
foreach ($path in $myFiles) {
$path = $checkoutFolder + "\" + $path
}
$myFiles
我也试过使用缓冲区变量,例如
$buffer = $checkoutFolder + "\" + $path
$path = $buffer
但结果相同,即 输出:
blah1\blah2\file1.txt
blah3\blah4\file2.txt
blah5\blah6\file3.txt
最好开始使用 Join-Path
cmdlet,以避免创建省略或加倍反斜杠的路径。
像这样就可以了:
$checkoutFolder = "folder1"
$myFiles = "blah1\blah2\file1.txt", "blah3\blah4\file2.txt", "blah5\blah6\file3.txt" | ForEach-Object {
Join-Path -Path $checkoutFolder -ChildPath $_
}
$myFiles
输出:
folder1\blah1\blah2\file1.txt folder1\blah3\blah4\file2.txt folder1\blah5\blah6\file3.txt
我想到了两个办法:
使用旧数组的修改数据创建新数组
$myFiles = @(
"blah1\blah2\file1.txt"
"blah3\blah4\file2.txt"
"blah5\blah6\file3.txt"
)
$checkoutFolder = "folder1"
#Create new array $myFilesnew
$myFilesnew = @()
#For each line in in old array
foreach ($file in $myFiles)
{
#Create new row from modied row $file of $myFiles array
$row = $checkoutFolder+"\"+$file
#Add row $row to a new array $myFilesnew
$myFilesnew+=$row
}
$myFilesnew
修改现有数组的每一行:
$myFiles = @(
"blah1\blah2\file1.txt"
"blah3\blah4\file2.txt"
"blah5\blah6\file3.txt"
)
$checkoutFolder = "folder1"
$i=0
while($i-lt $myFiles.Count)
{
#Get $i row $myFiles[$i] from aray, perform insert of modified data, write data back to $myFiles[$i] row of the array
$myfiles[$i]=$myFiles[$i].Insert(0,$checkoutFolder+"\");
#Add +1 to $i
$i++
}
$myFiles
您可以将每个字符串的正则表达式开头替换为文件夹名称。这也是生成计算机名的有用习惯用法。
'blah1\blah2\file1.txt','blah3\blah4\file2.txt','blah5\blah6\file3.txt' -replace '^','folder1\'
folder1\blah1\blah2\file1.txt
folder1\blah3\blah4\file2.txt
folder1\blah5\blah6\file3.txt