2014-09-24 66 views
0

我搜索了,我Google搜索..关于粉碎我的头在桌子上

这怎么会不起作用?

move-Item $path$file $targetdir 

它给了我一个错误 布展项目:在指定的路径C的对象:\库\ 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 
+0

你试过只是把双引号“‘$ PATH $文件’'调用串插? – TheMadTechnician 2014-09-24 16:57:53

+0

@TheMadTechnician正确我有,同样的错误。 – LuisEN 2014-09-24 16:58:59

+0

如果您执行'Test-Path“$ path $ file”'会发生什么?我正在考虑你的问题中的最后一行 – Matt 2014-09-24 17:01:03

回答

1


代码执行目标目录中已经存在?如果目标目录不存在,我相信Move-Item会失败。如果是这种情况,您可以预先测试目录是否存在,然后根据需要进行创建。

If (!(Test-Path -Path $targetdir)) { 
    New-Item -ItemType directory -Path $targetdir 
} 
+0

是的目录有工作正常,如果我不使用变量,但使用确切的字符串@kris powell – LuisEN 2014-09-24 17:17:20

0

这个怎么样,那么:

ForEach($File in $List){ 
    Join-Path $path $file | Move-Item -Dest $Targetdir 
} 

编辑:还...你LIST.TXT的创作,它困扰着我,所以我不得不发表评论。格式表应该用于格式化文本,而不是用于选择要输出到文件的值。有一个更好的方式来做到这一点,认为这种替代:

Get-ChildItem "$path*.csv" | Select -ExpandProperty Name | Out-File $pathlist.txt 

既然你说$path = 'C:\test\'要添加在有额外的反斜杠,可能会导致某些命令的问题。好吧,如果这不起作用,为什么不使用文件本身而不是输出到文件,从该文件导入,然后处理事情。

$path='c:\test\' 
$TargetDir = 'c:\test\NewDir' 
$FileList = Get-ChildItem $path*.csv 
If(!(Test-Path $TargetDir)){New-Item -ItemType Directory -Path $TargetDir|Out-Null} 
$FileList | Move-Item -Destination $TargetDir 

然后,如果你真的想这些文件的名称的列表只是管$文件清单,以

$FileList | Select -ExpandProperty Name | Out-File 'C:\Test\list.txt' 

这里选择,然后到Out-文件,通过这一下,看看是否有你喜欢的东西。我做了一些改变,例如在开始处声明所有路径,我将WebClient对象创建移到了循环之外,并改变了在屏幕上显示的内容。另外我跳过整个导出到文本文件并重新导入它。

$path = 'C:\test' 
$ftpaddr = 'ftp://ftp.example.com/uploads' 
$TimeStamp = Get-Date -Format "MM/dd/yyyy hh:mm:ss tt" 
$LogFile = Get-Date -Format "MM_dd_yyyy" 

$LogDir = "C:\Test\Logs" 
If(!(test-path $LogDir)){New-Item -ItemType Directory -Path $LogDir | Out-Null} 
$targetdir = 'C:\test\Uploaded' 
If(!(test-path $targetdir)){New-Item -ItemType Directory -Path $targetdir | Out-Null} 

$list = Get-ChildItem -path $path\* -Include *.csv 

$webclient = New-Object System.Net.WebClient 

"ftp url: $ftpaddr" 

foreach ($file in ($list|select -ExpandProperty Name)) 
{ 
    $uri = New-Object System.Uri(("$ftpaddr/$file")) 

    Write-Host "Uploading $file... " -NoNewline -ForegroundColor White 

    $succeeded = $true 
    & { 
    trap { $script:succeeded = $false; continue } 
    $webclient.UploadFile($uri, "$Path\$file") 
     } 

if ($succeeded) 
     { 
     Write-Host "Success!" -ForegroundColor Green 
     "$Timestamp`t$File was successfully uploaded!" | Out-File "$logdir\logfile$LogFile.log" -Append 
     move-Item -path "$path\$file" -destination $targetdir 
     } 

    else 
     { 
     Write-Host "Failed! Will retry later." -ForegroundColor Red 
     "$Timestamp`t$File was not successfully uploaded, will retry later" | Out-File "$logdir\logfile$LogFile.log" -Append 
     } 

} 
+0

相同的错误,不存在。 – LuisEN 2014-09-24 17:27:18

+0

看看我上面创建的完整代码。让我知道这是否有帮助。这将有助于更好地理解它。即时通讯基本上上载后,我得到列表并将文件移动到一个Uploaded文件夹。 – LuisEN 2014-09-24 17:36:25

+0

我想我是在想它= |它很混乱 – LuisEN 2014-09-24 17:38:58

0

这对我有效。谢谢@TheMadTechnician。希望这有助于大家

$TimeStamp = Get-Date -Format "MM-dd-yyyy_hh-mm-ss" 
$LogFile = Get-Date -Format "MM_dd_yyyy" 

$path='C:\test\' 
$targetDir = 'C:\test\Uploaded\' 
$fileList = Get-ChildItem $path*.csv 
If(!(Test-Path $TargetDir)){New-Item -ItemType Directory -Path $TargetDir|Out-Null} 
$fileList | Select -ExpandProperty Name | Out-File 'C:\test\list.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$Timestamp"_"$file 
     #test-path $path$file 
     } 

    else 
     { 

     echo $file 'Was not successfully uploaded, will retry later' $Timestamp >> logfile$LogFile.log 

     } 


} 

exit 
+0

很高兴你的工作,只是为了也许学习一种技术或两个看看我刚刚添加到我的答案,也许会有一两件事,你想纳入你的东西有时脚本。 – TheMadTechnician 2014-09-24 18:37:03

0

基础是:

  • 测试的路径,你动它(文件和目的地)
  • 移动文件之前,请确保您有权限(强制移动)

这样:

echo $targetdir 
echo "$path$file" 
if (!(Test-Path $targetdir)) { 
    New-Item -ItemType directory $targetdir 
} 
if(Test-Path "$path$file") { 
    Move-Item "$path$file" $targetdir -Force 
} else { 
    echo "file does not exist" 
} 

如果你遍历你必须使用对象的“.FullName”属性的集合:

Get-ChildItem $path | ForEach-Object { Move-Item $_.FullName $targetdir -Force }