Upload all files in a folder to FTP | Steve Fenton
source link: https://www.stevefenton.co.uk/2022/06/upload-all-files-in-a-folder-to-ftp/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Upload all files in a folder to FTP
This is the second old-school post this week. Hey, I’m clearing the decks of some odd stuff that I had to do. Today, it’s uploading all files in a folder (but not sub-folders) to FTP, if they have been updated in the past 24 hours.
There’s not much to explain here. It’s a simple System.Net.WebClient
doing the work, with a credential added for the username and password. The source folder is searched for non-directory entries using Get-ChildItem
and passing into Where-Object
to filter by last write time. Each match is moved up to the FTP folder with the same name.
$source = '../src' $server = 'ftp://0.0.0.0' $date = (Get-date).AddDays(-1) Write-Host $date # Create a web client with credentials $webclient = New-Object System.Net.WebClient $webclient.Credentials = New-Object System.Net.NetworkCredential('MyUserName','MyPassword') # Find matching files (only files, not folders) | then filter by last write time $files = Get-ChildItem $source -Attributes !Directory | Where-Object { $_.LastWriteTime -ge $date } # Upload 'em all foreach ($file in $files) { $path = "$server/www/destinationfolder/$file" Write-Host $file.FullName $path $webclient.UploadFile($path, $file.FullName) } $webclient.Dispose()
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK