需要批處理或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
那麼有一個工作的FTP服務器在你的本地主機上運行?你可以通過filezilla連接到自己嗎?有沒有防火牆? –
可能是'$ ftp + $ item.Name'是一個錯誤連接的路徑。當您將您的uri更改爲'「$ ftp/$(item.name)」時,會發生什麼情況'' – BenH
使用純批處理腳本可能很難正確讀取和訪問Excel文件... – aschipfl