[英]failed copy file from path using powershell
我有这个脚本:
$UserFile = Read-Host "Drag your file here"
Copy-Item -Path $UserFile -Destination .\input
我希望用户将他们的文件拖到控制台,以便脚本可以知道用户文件的确切路径并将用户文件复制到输入文件夹。 但我收到了这个错误
Copy-Item : Cannot find drive. A drive with the name '"C' does not exist.
At line:1 char:1
+ Copy-Item -Path $UserFile -Destination .\input
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ("C:String) [Copy-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
更新:越来越奇怪,我认为我的机器有问题,我尝试在 VM 上运行代码,代码运行时出现零问题,当我拖动文件时,我的机器和 VM 之间有一些不同在我的机器上它会这样写"C:\path\to\my\file.something"
但是当我在我的虚拟机上这样做时它会写C:\path\to\my\file.something
没有"
更新:我刚刚意识到我使用了不同的文件,我在我的机器上尝试的文件是file one.something
但我在 VM 上使用的文件没有空间file.something
但我尝试过像这样运行代码
Copy-Item -Path "C:\path\to\my\file one.something" -Destination .\input
使用"
并且它可以工作。但这不是我想要的,我希望用户拖动他们的文件,无论是否有空白。
尝试添加 -Prompt 参数
$UserFile = Read-Host -Prompt "Drag your file here"
*** drags file ***
Write-Host $Userfile ## should display the filepath
我的猜测是,这对您来说不再是问题,并且在拖动文件之前在终端中输入了一个额外的 "。对我来说,您发布的代码没有问题。
您可能想查看找不到驱动器。 如果问题完全相同,则不存在名称为“C”的驱动器。
根据此脚本的使用情况,您可以在使用之前修剪/删除任何字符,并且根据我的经验,使用绝对路径而不是相对路径(此处为 ./input)几乎总是更好,除非您明确希望拥有该脚本是相对的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.