-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
176 lines (166 loc) · 5.89 KB
/
Program.cs
File metadata and controls
176 lines (166 loc) · 5.89 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace HttpCommand
{
class Program
{
class Parser
{
public string Text;
public int Index = 0;
public Parser(string txt)
{
Text = txt;
Index = 0;
}
public bool IsEnd()
{
return Text == null || Index == -1 || Index >= Text.Length;
}
public bool Find(string pattern)
{
Index = Text.IndexOf(pattern, Index);
return (Index != -1);
}
public string GetQuoted()
{
if (!IsEnd())
{
if (Find("\""))
{
int start = ++Index;
if (Find("\""))
{
int end = Index++;
return Text.Substring(start, end - start);
}
}
}
return null;
}
};
static List<string> GetFiles(string directory)
{
List<string> result = new List<string>();
try
{
Parser p = new Parser(directory);
while (!p.IsEnd())
{
if (p.Find("<li><a href="))
{
string name = p.GetQuoted();
if (name != null)
{
result.Add(name);
}
}
}
}
catch (Exception ex)
{
System.Console.WriteLine("Exception = " + ex.ToString());
}
return result;
}
static string Source = null;
static string Target = ".";
static bool Subdirectories = false;
static int Copy(System.Net.WebClient web, string source, string target)
{
int count = 0;
string dir = "";
try
{
dir = web.DownloadString(source);
}
catch (Exception)
{
System.Console.WriteLine("Failed = " + source);
return 0;
}
List<string> files = GetFiles(dir);
foreach (string f in files)
{
string fname = f.Replace("%20", " ");
if (fname.StartsWith(".."))
{
// Ignore
}
else if (fname.EndsWith("/")) // Directory?
{
if (Subdirectories)
{
string src = System.IO.Path.Combine(source, fname);
string dst = System.IO.Path.Combine(target, fname);
count += Copy(web, src, dst);
}
}
else
{
try
{
string web_file = System.IO.Path.Combine(source, fname);
string local_file = System.IO.Path.Combine(target, fname);
string folder = System.IO.Path.GetDirectoryName(local_file);
System.IO.Directory.CreateDirectory(folder);
string rel = web_file.Substring(Source.Length+1);
System.Console.WriteLine("File = " + rel);
web.DownloadFile(web_file, local_file);
count += 1;
}
catch (Exception ex)
{
System.Console.WriteLine("Exception = " + fname + "\n" + ex.ToString());
}
}
}
return count;
}
static void Main(string[] args)
{
for (int i = 0; i < args.Length; ++i)
{
string arg = args[i];
if (arg[0] == '-')
{
if (arg.ToUpper() == "-S")
{
Subdirectories = true;
System.Console.WriteLine("Subdirectories = ON");
}
}
else if (Source == null)
{
Source = arg;
System.Console.WriteLine("Source = " + Source);
}
else if (Target == ".")
{
Target = arg;
System.Console.WriteLine("Target = " + Target);
}
}
if (Source == null)
{
System.Console.WriteLine("HttpCommand - Copyright (c) 2011 Paul Isaac");
System.Console.WriteLine("Utility to download files from a web server.");
System.Console.WriteLine("");
System.Console.WriteLine(@"Useage: HttpCommand {flags} [http:\\uri] {local-target}");
System.Console.WriteLine(@"Flags: -S = recurse subdirectories");
System.Console.WriteLine("");
System.Console.WriteLine(@"For HTTP resources the GET method is used.");
System.Console.WriteLine(@"For FTP resources the RETR command is used.");
return;
}
System.Console.WriteLine("Downloading...");
System.Net.WebClient web = new System.Net.WebClient();
int count = Copy(web, Source, Target);
string msg = string.Format("\nDownloaded {0} files.", count);
System.Console.WriteLine(msg);
}
}
}