【发布时间】:2021-11-13 11:23:06
【问题描述】:
运行此脚本后:
# Input path
$input_path = 'C:\Users\larry\Pictures\2003'
# Export file
$documents_export_file = 'C:\Users\larry\Documents\Temp\ffmpeg\fullnames.csv'
# Copy-to folder
$copy_to = "C:\Users\larry\Documents\Temp\ffmpeg\movies"
# List filenames in $documents_export_file
Get-ChildItem -Attributes !Directory -Path $input_path -Recurse -force | select-object -Property fullName | Export-Csv $documents_export_file
# Copy the files listed in $documents_export_file to $copy_to folder
get-content $documents_export_file | Foreach-Object { copy-item -Path $_ -Destination $copy_to}
我收到以下错误:
copy-item : Cannot find drive. A drive with the name '"C' does not exist.
At line:18 char:55
+ ... ort_file | Foreach-Object { copy-item -Path $_ -Destination $copy_to}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ("C:String) [Copy-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
copy-item : Cannot find drive. A drive with the name '"C' does not exist.
At line:18 char:55
+ ... ort_file | Foreach-Object { copy-item -Path $_ -Destination $copy_to}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ("C:String) [Copy-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
据我所知,它与我的 Export-Csv 有关,但我无法弄清楚。 请帮我一把。
【问题讨论】:
-
首先检查循环中
$_和$copy_to的值。插入Write-Host "path: '$_' Copy To: '$copy_to'";或类似名称,以便查看值。 -
您使用的是哪个 Powershell 版本?为什么不使用
-File参数? -
[1] 将开关
-NoTypeInformation添加到Export-Csv命令以防止在标题行上方显示额外信息。 [2] 读取 CSV 文件时,请使用Import-Csv,而不是Get-Content,因为这是一个将文本文件读取为字符串数组的 cmdlet。
标签: powershell export-csv