如何使用 Powershell 中的变量将一系列文件移动到文件夹?
How to move a series of files to a folder using variables in Powershell?
我正在努力将一系列文件移动到 Powershell 中的文件夹中。我在显式引用路径名时设法做到了,但在使用变量名时却无法做到这一点。
这是我的代码的简化版本。
files = Get-ChildItem C:\Users\edward.muldrew\Documents759175
$pathDestination = "C:\Users\edward.muldrew\Documents759175\Test"
for ($i=0; $i -lt $files.Count; $i++) {
$pathSource = "C:\Users\edward.muldrew\Documents759175$files[$i]"
Move-Item $pathSource -Destination $pathDestination
如有任何帮助,我们将不胜感激
您可以将变量设置在字符串外部,并将它的值与您的字符串连接起来,而不是将变量放在字符串中:
files = Get-ChildItem C:\Users\edward.muldrew\Documents759175
$pathDestination = "C:\Users\edward.muldrew\Documents759175\Test"
for ($i=0; $i -lt $files.Count; $i++) {
$pathSource = "C:\Users\edward.muldrew\Documents759175\" + $files[$i]
Move-Item $pathSource -Destination $pathDestination
编辑:我已经看到 powershell 允许您将变量放在字符串中而不通过将它们包围在 $()
中进行连接
files = Get-ChildItem C:\Users\edward.muldrew\Documents759175
$pathDestination = "C:\Users\edward.muldrew\Documents759175\Test"
for ($i=0; $i -lt $files.Count; $i++) {
$pathSource = "C:\Users\edward.muldrew\Documents759175$($files[$i])"
Move-Item $pathSource -Destination $pathDestination
如果您只需要移动文件。使用 whatif 标志查看 运行 实际命令
之前的结果
mv -WhatIf -Path "$source\*.*" -Destination $pathDestination
我正在努力将一系列文件移动到 Powershell 中的文件夹中。我在显式引用路径名时设法做到了,但在使用变量名时却无法做到这一点。
这是我的代码的简化版本。
files = Get-ChildItem C:\Users\edward.muldrew\Documents759175
$pathDestination = "C:\Users\edward.muldrew\Documents759175\Test"
for ($i=0; $i -lt $files.Count; $i++) {
$pathSource = "C:\Users\edward.muldrew\Documents759175$files[$i]"
Move-Item $pathSource -Destination $pathDestination
如有任何帮助,我们将不胜感激
您可以将变量设置在字符串外部,并将它的值与您的字符串连接起来,而不是将变量放在字符串中:
files = Get-ChildItem C:\Users\edward.muldrew\Documents759175
$pathDestination = "C:\Users\edward.muldrew\Documents759175\Test"
for ($i=0; $i -lt $files.Count; $i++) {
$pathSource = "C:\Users\edward.muldrew\Documents759175\" + $files[$i]
Move-Item $pathSource -Destination $pathDestination
编辑:我已经看到 powershell 允许您将变量放在字符串中而不通过将它们包围在 $()
files = Get-ChildItem C:\Users\edward.muldrew\Documents759175
$pathDestination = "C:\Users\edward.muldrew\Documents759175\Test"
for ($i=0; $i -lt $files.Count; $i++) {
$pathSource = "C:\Users\edward.muldrew\Documents759175$($files[$i])"
Move-Item $pathSource -Destination $pathDestination
如果您只需要移动文件。使用 whatif 标志查看 运行 实际命令
之前的结果 mv -WhatIf -Path "$source\*.*" -Destination $pathDestination