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 I had to do. Today, it’s uploading all the files in a folder (but not sub-folders) to an FTP endpoint. The script only does this if the files 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 them 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
()