2014-09-24 65 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 }