【发布时间】:2015-06-09 01:19:19
【问题描述】:
我搜索了,我用谷歌搜索了.. 差点把我的头砸在桌子上
这怎么行不通?
move-Item $path$file $targetdir
它给了我一个错误 Move-Item : 指定路径 C:\Repository\test.csv 处的对象 不存在。
现在,如果我调试它并使用
输出write-output move-Item $path$file $targetdir
并获取该输出并将其粘贴(带有路径和目标的文件名)它可以工作!
相信我,文件就在那里。 =\
代码如下
$path = 'C:\test\'
$TimeStamp = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
$LogFile = Get-Date -Format "MM_dd_yyyy"
$targetdir = "C:\test\Uploaded\"
#Get-ChildItem -path $path\* -Include *.csv | foreach-object {$_.Fullname} | Format-Table name -hidetableheaders | Out-File $path\list.txt
Get-ChildItem -path $path\* -Include *.csv | Format-Table name -hidetableheaders | Out-File $path\list2.txt
get-content C:\test\list2.txt | where {$_ -ne ""} | out-file C:\test\list.txt
Remove-Item C:\test\list2.txt
$list = get-content C:\test\list.txt
foreach ($file in $list)
{
$ftp = "ftp://REMOVED/$file"
"ftp url: $ftp"
$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)
"Uploading $file..."
$succeeded = $true;
& {
trap { $script:succeeded = $false; continue }
$webclient.UploadFile($uri, $path+$file)
}
if ($succeeded)
{
echo $file 'Was successfully uploaded!' $Timestamp >> logfile$LogFile.log
move-Item -path $path$file -destination $targetdir
#test-path $path$file
}
else
{
echo $file 'Was not successfully uploaded, will retry later' $Timestamp >> logfile$LogFile.log
}
}
exit
【问题讨论】:
-
你试过在
"$path$file"周围加上双引号来调用字符串插值吗? -
@TheMadTechnician 正确我有,同样的错误。
-
如果你这样做
Test-Path "$path$file"会发生什么?我正在考虑您问题中的最后一行 -
@TheMadTechnician 输出 = True,不带 " 和带
-
我刚试了一下,无法重现问题。在这一点上,我们需要更多信息。什么版本的 PowerShell? $path、$file 和 $targetdir 是字符串吗?文件信息对象? PSCustom 对象?我使用了这些变量,将它们设置为字符串,然后毫无问题地运行您的命令。
$path='c:\temp\'$file='list.csv'和$targetdir='c:\temp\test'正常工作(并移动了文件)
标签: powershell