2016-12-06 45 views
-2

需要批處理或Powershell腳本通過其內容(包含一些字符串的xls文件)查找某些文件,然後上載到FTP服務器。按字符串內容查找一些文件並上傳到FTP

########################################################### 

$Path = "f:/temp" 
$Text = "123456" 
$PathArray = @() 
$Results = "F:/PROFIT/INSTALL/backup/search/test.txt" 

# This code snippet gets all the files in $Path that end in ".txt". 
Get-ChildItem $Path -Filter "*.txt" -Recurse| 
Where-Object { $_.Attributes -ne "Directory"} | 
ForEach-Object { 
If (Get-Content $_.FullName | Select-String -Pattern $Text) { 
$PathArray += $_.FullName 
$PathArray | % {$_} | Out-File $Results 
} 
} 
Write-Host "Contents of ArrayPath:" 
$PathArray | ForEach-Object {$_} 

#we specify the directory where all files that we want to upload 
$Dir="F:/PROFIT/INSTALL/backup/search"  

#ftp server 
$ftp = "ftp://127.0.0.1" 
$user = "ftp" 
$pass = "ftp" 

$webclient = New-Object System.Net.WebClient 

$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 

#list every sql server trace file 
foreach($item in (dir $Dir "*.*")){ 
    "Uploading $item..." 
    $uri = New-Object System.Uri($ftp+$item.Name) 
    $webclient.UploadFile($uri, $item.FullName) 
} 

想這一點,但每次有一個問題:

上傳的test.txt ...Исключениепривызове一個 「UploadFile」 с 「2」 аргументами:「Невозможноразрешитьудаленноеимя: '127.0.0.1test.txt'「F:\ PROFIT \ INSTALL \ backup \ search \ search.ps1:36 знак:5 + $ webclient.UploadFile($ uri,$ item.FullName) + ~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ified:(:) [],MethodInvocationException + FullyQualifiedErrorId:WebException

+0

那麼有一個工作的FTP服務器在你的本地主機上運行?你可以通過filezilla連接到自己嗎?有沒有防火牆? –

+0

可能是'$ ftp + $ item.Name'是一個錯誤連接的路徑。當您將您的uri更改爲'「$ ftp/$(item.name)」時,會發生什麼情況'' – BenH

+0

使用純批處理腳本可能很難正確讀取和訪問Excel文件... – aschipfl

回答

0

您在連接服務器和文件名時出錯。嘗試這樣的事情:

#variables 
$PathToSearch = "c:\temp" 
$SearchText = "hello" 
$Results = "c:\temp\test.txt" 
$ftp = "ftp://127.0.0.1/" 
$user = "ftp" 
$pass = "ftp" 

#get liste file to found with string 
$PathArray=Get-ChildItem $PathToSearch -Filter "*.txt" -Recurse -file | Select-String -Pattern $SearchText | get-item | select Name, FullName 

Write-Host "Files founded :" 
$PathArray.FullName 

#send to ftp server 
$webclient = New-Object System.Net.WebClient  
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)  

$PathArray | % { 
       $uri = New-Object System.Uri($ftp+$_.Name)  
       $webclient.UploadFile($uri, $_.FullName)  
       $_.FullName | Out-File -FilePath $Results -Append 
       } 

$webclient.Dispose() 
相關問題