我有两个Powershell 脚本,第一个帮助我从中国下载ftp文件:
下载 FTP 脚本
# Parametros
param([string]$url, [string]$user, [string]$pass, [string]$folder, [string]$fecpro)
if (!$fecpro)
{
$date = Get-Date
$date = $date.AddDays(-1)
$fecpro = '{0:yyyyMMdd}' -f $date
}
$FtpServer = "ftp://$url/$fecpro"
$Username = $user
$Password = $pass
$RemoteFolder = $FtpServer
if (!$folder)
{
$LocalFolder = $pwd.path + "\"
}
else
{
$LocalFolder = $folder + "\"
if (-not(Test-Path -Path $LocalFolder))
{
New-Item -ItemType Directory -Force -Path $LocalFolder
}
}
Write-Host "Searching *.zip files from $RemoteFolder..."
$existfiles = gci $LocalFolder | Where-Object { $_.name -like "*.zip" }
$cntFile = 0
$totFiles = 1
$listFiles = @(0)*$totfiles
$listFiles[1] = "cs2-" + $fecpro + "-6004-0000-1.zip"
$existfiles = gci $LocalFolder | Where-Object { $_.name -like "*.zip" }
do
{
try
{
$f_exist = $false
$cntFile = $cntFile + 1
if ($existfiles) {
foreach($file in $existfiles) {
$tmpname = $file.name.ToString()
$bool = $listFiles[$cntFile].CompareTo($file.name)
if ($bool -eq 0) {
$f_exist = ($f_exist -or $true)
} else {
$f_exist = ($f_exist -or $false)
}
}
}
if (!$f_exist -and $rescnt['CNT'] -eq 0) {
$ftpfile = $RemoteFolder + "/" + $listFiles[$cntFile]
$FTPRequest1 = [System.Net.FtpWebRequest]::Create($ftpfile)
$FTPRequest1.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)
$FTPRequest1.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$FTPRequest1.UsePassive = $true
$FTPRequest1.UseBinary = $true
$FTPRequest1.KeepAlive = $false
$destfile = $LocalFolder + $listFiles[$cntFile]
$file = New-Object IO.FileStream ($destfile, [IO.FileMode]::Create)
$FTPResponse = $FTPRequest1.GetResponse()
$ResponseStream = $FTPResponse.GetResponseStream()
[byte[]] $buffer = New-Object byte[] 64
$FTPReader = $ResponseStream.Read($buffer, 0, 64)
while($FTPReader -ne 0){
$FTPReader = $ResponseStream.Read($buffer,0, 64)
$file.Write($buffer,0,$FTPReader)
}
Write-Host "File $ftpfile is downloaded in local folder."
$file.Close()
$FTPReader.Close()
$FTPResponse.Close()
$ResponseStream.Close()
}
else {
Write-Host "The file $name already exists in local folder."
}
}
catch
{
if ($file) {
$file.Close()
}
}
}
while ($cntFile -lt $totFiles+1)
我的第二个脚本使用Oracle 客户端附带的 unzip.exe 解压缩文件。(%ORACLE_HOME%\product\10.2.0\client_1\BIN)
解压文件脚本
param([string]$folder_origen, [string]$folder_destino, [string]$fecpro)
$P_FOLDER = $folder_origen + "\"
$P_DESTINO = $folder_destino + "\"
if (!$fecpro)
{
$date = Get-Date
$date = $date.AddDays(-1)
$fecpro = '{0:yyyyMMdd}' -f $date
}
Write-Host "Searching *.zip files in $P_FOLDER"
# Inicio del programa
$LocalFolder = $P_FOLDER + "*$fecpro*.zip"
$filelocation = gci $LocalFolder
foreach ($file in $filelocation){
if (($file) -and ($file.length -gt 0kb))
{
$shell = new-object -com shell.application
$filename = $file.name.ToString()
$split = $filename.Split(".")
$dest = $P_DESTINO + $split[0]
If (!(Test-Path $dest))
{
New-Item $dest -Type Directory
}
$orifile = $P_FOLDER + $filename
$time = "{0:hh_mm_ss.ffff}" -f (get-date)
$prefile = "d." + (get-date).ToShortDateString() + ".t." + $time
$prefile = $prefile -replace "/", "_"
$filename = $prefile + ".f." + $filename
$P_DESTZIP = $P_FOLDER + "proc" + "\"
If (!(Test-Path $P_DESTZIP))
{
New-Item $P_DESTZIP -Type Directory
}
#Copy-Item $orifile "$P_DESTZIP$prefile_$filename"
$zipname = $orifile
#"$P_DESTZIP$prefile_$filename"
$zip = $shell.NameSpace($zipname)
$destn = $shell.Namespace($dest)
unzip -o -qq $zipname "*$fecpro*afsbjdtl.*" "*$fecpro*afgnled.bin" -d $dest
Write-Host "$orifile was renamed and unzipped."
} else
{
Write-Host "Zip files not found in the folder."
}
}
当我可以使用文件大小、名称、扩展名等过滤器时,我会搜索文件以提取模式,而且速度非常快。
最后关于负载,我也建议使用命令LOAD DATA。它的性能最好。