需要Batch或Powershell脚本按内容查找某些文件(带有一些字符串的xls文件),然后上传到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) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId:WebException
答案 0 :(得分: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()