Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 87 additions & 29 deletions script.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,134 @@
# Powershell script
# Upload pictures through ftp
#
# Errol Straker <errol.strakerjr@gmail.com>
# 2015/11/03
# Powershell script
# CHANGELOG
#
# Changes logged in code
# 20151027_Added in file rename function


#Directory where to find pictures to upload
$Dir= 'c:\fff\medias\'
#Get the date and time of file transfer
$eventdate = Get-Date -format U

#Directory where to save uploaded pictures
$saveDir = 'c:\fff\save\'
#Directory where to find files to upload
$Dir= 'c:\ftp_test\'

#Directory where to save uploaded files
$saveDir = 'c:\ftp_test\backup\'

#ftp server params
$ftp = 'ftp://xx.x.x.xx:xx/'
$user = 'user'
$pass = 'pass'
$ftp = 'ftp://speedtest.tele2.net/upload/'
$user = ''
$pass = ''

#Connect to ftp webclient
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)

#Create log file for process or append if present
function LogWrite
{
Param ([string]$logstring)

$Logchk = Test-Path C:\ftp_test\logs\$(gc env:computername).log
if ($Logchk -eq $false)
{Add-content C:\ftp_test\logs\$(gc env:computername).log -Value $logstring}
else {$Logfile = "c:\ftp_test\logs\$(gc env:computername).log"
Add-Content $Logfile -Value $logstring}
}

#Initialize var for infinite loop
$i=0

#Infinite loop
while($i -eq 0){

#Pause 1 seconde before continue
#Pause 1 second before continue
Start-Sleep -sec 1

#Search for pictures in directory
foreach($item in (dir $Dir "*.jpg"))
#Search for files in directory after verifing existance
$Listchk = Test-Path C:\ftp_test\*.csv
if ($Listchk -eq $true)
{

foreach($item in (dir $Dir "*.csv"))
{
#Set default network status to 1
$onNetwork = "1"

#Get picture creation dateTime...
$pictureDateTime = (Get-ChildItem $item.fullName).CreationTime
#Get file creation dateTime...
$fileDateTime = (Get-ChildItem $item.fullName).CreationTime

#Convert dateTime to timeStamp
$pictureTimeStamp = (Get-Date $pictureDateTime).ToFileTime()
$fileTimeStamp = (Get-Date $fileDateTime).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") {
#Get file lifeTime
$fileLifeTime = $timeStamp - $fileTimeStamp

#We only treat files that are fully written on the disk
#So, we put a 2 second delay to ensure even big files have been fully wirtten in the disk
if($fileLifeTime -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] {

} catch [Exception] {

$onNetwork = "0"
write-host $_.Exception.Message;
}


#If upload succeeded, we do further actions
#Copy has been expanded to append a datestamp to the filename
#Then copy that renamed file to the file backup directory
#Then the script writes the changes to the log file
if($onNetwork -eq "1"){
"Copying $item..."
Copy-Item -path $item.fullName -destination $saveDir$item
$filename = get-item $item.fullname
#Properly pull in item for name change
$fileObj = get-item $item.fullname
#Get the date
$DateStamp = get-date -uformat "%Y-%m-%d@%H-%M-%S"
$extOnly = $fileObj.extension
if ($extOnly.length -eq 0){
$nameOnly = $fileObj.fullname
Rename-Item "$fileObj" "$nameOnly-$DateStamp"
}
else {
$nameOnly = $fileObj.Name.Replace( $fileObj.Extension,'')
Rename-Item "$filename" "$nameOnly-$DateStamp$extOnly"
}

#After renaming file, script loses file path
#Band-Aid: Read in contents of $DIR again
#Then perform closing actions
foreach ($nfile in (dir $Dir "*.csv"))
{
"Successfully uploaded and now performing housecleaning for file $item..."
Move-Item -Path $nfile.fullName -Destination $saveDir$nfile

LogWrite "On $eventdate, $item was successfully uploaded on $fileDateTime to $ftp"
LogWrite "$item has been renamed to $nfile with a backup saved in $saveDir"
Exit
}

"Deleting $item..."
Remove-Item $item.fullName
}


}
}
}
}
# Added in log write if file not found
else
{LogWrite "File Not Found for $eventdate"}
Exit
}