【问题标题】:Unable to work with lines i csv created by export-csv无法使用由 export-csv 创建的行 i csv
【发布时间】: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


【解决方案1】:

首先不需要将文件列表导出为 CSV - 将Get-ChildItem 的输出分配给一个变量,然后将内容直接通过管道传输到Copy-Item

$files = Get-ChildItem -Attributes !Directory -Path $input_path -Recurse -Force

$files |Copy-Item -Destination $copy_to

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-03
    • 2014-06-01
    • 1970-01-01
    • 2020-06-28
    • 2014-02-20
    • 2018-02-08
    • 2020-12-06
    • 1970-01-01
    相关资源
    最近更新 更多