-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileOperations.cs
196 lines (176 loc) · 5.14 KB
/
FileOperations.cs
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using Microsoft.SqlServer.Server;
using System;
using System.Data.SqlTypes;
using System.Diagnostics;
using System.IO;
/* Example usage:
USE [test]
GO
DECLARE @arguments nvarchar(max) = 'test.dbo.OrderDetails out "C:\temp\OrderDetails.bcp" -T -c -C ACP'
DECLARE @output_msg nvarchar(max)
DECLARE @error_msg nvarchar(max)
DECLARE @return_val int
EXECUTE [dbo].[RunBCP]
@arguments
,@output_msg OUTPUT
,@error_msg OUTPUT
,@return_val OUTPUT
SELECT
@output_msg
,@error_msg
,@return_val
GO
*/
public partial class StoredProcedures
{
private static string EndsWithSeparator(string absolutePath)
{
return absolutePath?.TrimEnd('\\') + "\\";
}
[Microsoft.SqlServer.Server.SqlProcedure]
public static void RunBCP (SqlString arguments, out SqlString output_msg, out SqlString error_msg, out SqlInt32 return_val)
{
output_msg = string.Empty;
error_msg = string.Empty;
try
{
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "bcp",
Arguments = arguments.ToString(),
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
while (!proc.StandardOutput.EndOfStream)
{
output_msg += proc.StandardOutput.ReadLine();
}
return_val = proc.ExitCode;
}
catch (Exception e)
{
error_msg = e.Message;
return_val = 1;
}
}
/* Example usage:
USE [test]
GO
DECLARE @sourcePath nvarchar(max) = 'C:\IncomingData\FileToMove.txt'
DECLARE @targetPath nvarchar(max) = 'C:\OutgoingData'
DECLARE @overwrite bit = 1
DECLARE @output_msg nvarchar(max)
DECLARE @error_msg nvarchar(max)
DECLARE @return_val int
EXECUTE [dbo].[MoveFile]
@sourcePath
,@targetPath
,@overwrite
,@output_msg OUTPUT
,@error_msg OUTPUT
,@return_val OUTPUT
SELECT
@output_msg
,@error_msg
,@return_val
GO
*/
[Microsoft.SqlServer.Server.SqlProcedure]
public static void MoveFile(SqlString sourcePath, SqlString targetPath, SqlBoolean overwrite, out SqlString output_msg, out SqlString error_msg, out SqlInt32 return_val)
{
output_msg = string.Empty;
error_msg = string.Empty;
string[] allowedPaths = new string[] {
/*
** Add allowed paths here **
* Example:
@"\\FileSrv\SharedPath\", @"\\sql-srv\IncomingData\", @"C:\IncomingData\", @"C:\OutgoingData\"
* Leave empty to allow all folder paths
*/
};
bool sourceOkay = false;
bool targetOkay = false;
if (allowedPaths.Length == 0)
{
sourceOkay = true;
targetOkay = true;
}
else
{
foreach (var allowedPath in allowedPaths)
{
if (sourcePath.Value.StartsWith(allowedPath))
{
sourceOkay = true;
}
if (targetPath.Value.StartsWith(allowedPath))
{
targetOkay = true;
}
}
}
if (!sourceOkay)
{
error_msg = "Source path is not allowed: " + sourcePath.Value;
return_val = -1;
return;
}
if (!targetOkay)
{
error_msg = "Target path is not allowed: " + targetPath.Value;
return_val = -1;
return;
}
if (!File.Exists(sourcePath.Value))
{
error_msg = "Source file does not exist: " + sourcePath.Value;
return_val = -1;
return;
}
string targetFilePath = targetPath.Value;
if (Directory.Exists(targetFilePath))
{
targetFilePath = Path.Combine(targetFilePath, Path.GetFileName(sourcePath.Value));
}
if (File.Exists(targetFilePath))
{
if (overwrite.Value)
{
try
{
File.Delete(targetFilePath);
}
catch (Exception e)
{
error_msg = "Error while trying to delete existing target file: " + e.Message;
return_val = -1;
return;
}
}
else
{
// target file already exists and overwrite is false
error_msg = "Target file already exists: " + targetFilePath;
return_val = -1;
return;
}
}
// move sourcePath file to targetPath
try
{
System.IO.File.Move(sourcePath.Value, targetFilePath);
output_msg = "File moved successfully from " + sourcePath.Value + " to " + targetFilePath;
return_val = 0;
}
catch (Exception e)
{
error_msg = e.Message;
return_val = -1;
}
}
}