Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # create the FtpWebRequest and configure it
- $ftp = [System.Net.FtpWebRequest]::Create("ftp://localhost/me.png")
- $ftp = [System.Net.FtpWebRequest]$ftp
- $ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
- $ftp.Credentials = new-object System.Net.NetworkCredential("anonymous","anonymous@localhost")
- $ftp.UseBinary = $true
- $ftp.UsePassive = $true
- # read in the file to upload as a byte array
- $content = [System.IO.File]::ReadAllBytes("C:me.png")
- $ftp.ContentLength = $content.Length
- # get the request stream, and write the bytes into it
- $rs = $ftp.GetRequestStream()
- $rs.Write($content, 0, $content.Length)
- # be sure to clean up after ourselves
- $rs.Close()
- $rs.Dispose()
- $File = "D:Devsomefilename.zip";
- $ftp = "ftp://username:password@example.com/pub/incoming/somefilename.zip";
- Write-Host -Object "ftp url: $ftp";
- $webclient = New-Object -TypeName System.Net.WebClient;
- $uri = New-Object -TypeName System.Uri -ArgumentList $ftp;
- Write-Host -Object "Uploading $File...";
- $webclient.UploadFile($uri, $File);
- ftp -s:script.txt
- $server = "ftp.lolcats.com"
- $filelist = "file1.txt file2.txt"
- "open $server
- user $user $password
- binary
- cd $dir
- " +
- ($filelist.split(' ') | %{ "put ""$_""`n" }) | ftp -i -in
- $ftp.Proxy = $null;
- #Directory where to find pictures to upload
- $Dir= 'c:fffmedias'
- #Directory where to save uploaded pictures
- $saveDir = 'c:fffsave'
- #ftp server params
- $ftp = 'ftp://10.0.1.11:21/'
- $user = 'user'
- $pass = 'pass'
- #Connect to ftp webclient
- $webclient = New-Object System.Net.WebClient
- $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
- #Initialize var for infinite loop
- $i=0
- #Infinite loop
- while($i -eq 0){
- #Pause 1 seconde before continue
- Start-Sleep -sec 1
- #Search for pictures in directory
- foreach($item in (dir $Dir "*.jpg"))
- {
- #Set default network status to 1
- $onNetwork = "1"
- #Get picture creation dateTime...
- $pictureDateTime = (Get-ChildItem $item.fullName).CreationTime
- #Convert dateTime to timeStamp
- $pictureTimeStamp = (Get-Date $pictureDateTime).ToFileTime()
- #Get actual timeStamp
- $timeStamp = (Get-Date).ToFileTime()
- #Get picture lifeTime
- $pictureLifeTime = $timeStamp - $pictureTimeStamp
- #We only treat pictures that are fully written on the disk
- #So, we put a 2 second delay to ensure even big pictures have been fully wirtten in the disk
- if($pictureLifeTime -gt "2") {
- #If upload fails, we set network status at 0
- try{
- $uri = New-Object System.Uri($ftp+$item.Name)
- $webclient.UploadFile($uri, $item.FullName)
- } catch [Exception] {
- $onNetwork = "0"
- write-host $_.Exception.Message;
- }
- #If upload succeeded, we do further actions
- if($onNetwork -eq "1"){
- "Copying $item..."
- Copy-Item -path $item.fullName -destination $saveDir$item
- "Deleting $item..."
- Remove-Item $item.fullName
- }
- }
- }
- }
- ###########################################
- ### upload folder content to FTP server ###
- ###########################################
- #Local folder
- $localFolder= 'C:tempfolder1'
- #remote folder on FTP server. Do not user '/' at begining for root
- $remoteFolder = 'www/folder1/'
- #ftp server info
- $ftp = 'ftp://ftp.server.com:21/'
- $user = 'username'
- $pass = 'password'
- #Connect to ftp server using webclient
- $webclient = New-Object System.Net.WebClient
- $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
- foreach($file in Get-ChildItem $localFolder)
- {
- try
- {
- $uri = New-Object System.Uri($ftp + $remoteFolder + $file.Name)
- write-host "uploading: $uri"
- $webclient.UploadFile($uri, $file.FullName)
- }
- catch [Exception]
- {
- write-host $_.Exception.Message;
- }
- }
- $webclient.Dispose()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement