forked from edouardkombo/PowerShellFtp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.ps1
More file actions
137 lines (109 loc) · 4.2 KB
/
script.ps1
File metadata and controls
137 lines (109 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#
# Edouard Kombo <edouard.kombo@gmail.com>
# 2014/02/19
# 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
#Get the date and time of file transfer
$eventdate = Get-Date -format U
#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://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)
#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 second before continue
Start-Sleep -sec 1
#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 file creation dateTime...
$fileDateTime = (Get-ChildItem $item.fullName).CreationTime
#Convert dateTime to timeStamp
$fileTimeStamp = (Get-Date $fileDateTime).ToFileTime()
#Get actual timeStamp
$timeStamp = (Get-Date).ToFileTime()
#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] {
$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..."
$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
}
}
}
}
}
# Added in log write if file not found
else
{LogWrite "File Not Found for $eventdate"}
Exit
}