Ticket #3152: FileCopyProblemCodeToReproduce.au3

File FileCopyProblemCodeToReproduce.au3, 5.1 KB (added by pscales@…, 5 years ago)

AutoIt source code

Line 
1#include <Date.au3>
2
3Dim $fh, $temp, $rc, $i, $stuff = "Jackdaws love my big sphinx of quartz."
4Dim $morestuff = "The quick brown fox jumps over the lazy dog.", $UNCpath
5
6; Prompt for UNC path to copy test data to - must exist and be writeable
7
8$UNCpath = InputBox("Please supply a network folder name in UNC format to copy test files to", "Must exist and be writeable!", "\\<SERVER>\<SHARE>[<\PATH>]")
9If $UNCpath == "" and @error == 1 Then Exit
10If StringRight($UNCpath, 1) = "\" Then $UNCpath = StringLeft($UNCpath, Stringlen($UNCpath) - 1)
11
12; Create folder and files for testing
13
14If Not FileExists("C:\temp") Then
15        If Not DirCreate("C:\temp") Then
16                ConsoleWriteError("Fatal: Failed to create folder C:\temp" & @CRLF)
17                Exit
18        EndIf
19EndIf
20
21$temp = _NowCalcDate() & _NowTime(5)
22$temp = StringLeft($temp, 4) & StringMid($temp, 6, 2) & StringMid($temp, 9, 4) & StringMid($temp, 14, 2) & StringRight($temp, 2) & ".$$$"
23If Not DirCreate("C:\Temp\" & $temp) Then
24        ConsoleWriteError("Fatal: Failed to create C:\Temp\" & $temp & @CRLF)
25        Exit
26EndIf
27
28If Not FileChangeDir("C:\Temp\" & $temp) Then
29        ConsoleWriteError("Fatal: Failed to change dir to C:\Temp\" & $temp & @CRLF)
30        Exit
31EndIf
32
33$fh = FileOpen("TestFile_1.txt", 2)
34If $fh == -1 Then
35        ConsoleWriteError("Fatal: Failed to create TestFile_1.txt" & @CRLF)
36        Exit
37Else
38        For $i = 1 to 10
39                If Not FileWriteLine($fh, $stuff) Then
40                        ConsoleWriteError("Fatal: Failed to write data to TestFile_1.txt" & @CRLF)
41                        Exit
42                EndIf
43        Next
44        FileClose($fh)
45EndIf
46
47$fh = FileOpen("Test File 2.txt", 2)
48If $fh == -1 Then
49        ConsoleWriteError("Fatal: Failed to create Test File 2.txt" & @CRLF)
50        Exit
51Else
52        For $i = 1 to 10
53                If Not FileWriteLine($fh, $morestuff) Then
54                        ConsoleWriteError("Fatal: Failed to write data to Test File 2.txt" & @CRLF)
55                        Exit
56                EndIf
57        Next
58        FileClose($fh)
59EndIf
60
61$fh = FileOpen("Test File 3.txt", 2)
62If $fh == -1 Then
63        ConsoleWriteError("Fatal: Failed to create Test File 3.txt" & @CRLF)
64        Exit
65Else
66        For $i = 1 to 5
67                If Not FileWriteLine($fh, $stuff) Then
68                        ConsoleWriteError("Fatal: Failed to write data to Test File 3.txt" & @CRLF)
69                        Exit
70                EndIf
71        Next
72        For $i = 1 to 5
73                If Not FileWriteLine($fh, $morestuff) Then
74                        ConsoleWriteError("Fatal: Failed to write data to Test File 3.txt" & @CRLF)
75                        Exit
76                EndIf
77        Next
78        FileClose($fh)
79EndIf
80
81; Do the test
82
83; This should work
84
85If Not FileCopy("C:\Temp\" & $temp & "\TestFile_1.txt", $UNCpath) Then
86        ConsoleWrite("Error: Failed to copy C:\Temp\" & $temp & "\TestFile_1.txt to " & $UNCpath & @CRLF)
87Else
88        ConsoleWrite("Success!  Copied TestFile_1.txt to " & $UNCpath & @CRLF)
89EndIf
90
91; This probably won't work
92
93If Not FileCopy("C:\Temp\" & $temp & "Test File 2.txt", $UNCpath) Then
94        ConsoleWrite("Error: Failed to copy C:\Temp\" & $temp & "\Test File 2.txt to " & $UNCpath & @CRLF)
95Else
96        ConsoleWrite("Success!  Copied Test File 2.txt to " & $UNCpath & @CRLF)
97EndIf
98
99If Not FileCopy("""C:\Temp\" & $temp & "Test File 2.txt""", $UNCpath) Then
100        ConsoleWrite("Error: Failed to copy ""C:\Temp\" & $temp & "\Test File 2.txt"" to " & $UNCpath & @CRLF)
101Else
102        ConsoleWrite("Success!  Copied Test File 2.txt to " & $UNCpath & @CRLF)
103EndIf
104
105If Not FileCopy("""C:\Temp\" & $temp & "Test File 2.txt""", """" & $UNCpath & """") Then
106        ConsoleWrite("Error: Failed to copy ""C:\Temp\" & $temp & "\Test File 2.txt"" to " & """" & $UNCpath & """" & @CRLF)
107Else
108        ConsoleWrite("Success!  Copied Test File 2.txt to " & $UNCpath & @CRLF)
109EndIf
110
111If Not FileCopy("C:\Temp\" & $temp & "Test File 2.txt", """" & $UNCpath & """") Then
112        ConsoleWrite("Error: Failed to copy C:\Temp\" & $temp & "\Test File 2.txt to " & """" & $UNCpath & """" & @CRLF)
113Else
114        ConsoleWrite("Success!  Copied Test File 2.txt to " & $UNCpath & @CRLF)
115EndIf
116
117; This should work
118
119If FileChangeDir("C:\Temp\" & $temp) Then
120        If Not FileCopy("Test File 3.txt", $UNCpath) Then
121                ConsoleWrite("Error: Failed to copy C:\Temp\" & $temp & "\Test File 3.txt to " & $UNCpath & @CRLF)
122        Else
123                ConsoleWrite("Success!  Copied Test File 3.txt to " & $UNCpath & @CRLF)
124        EndIf
125Else
126        ConsoleWrite("Error: Failed to change directory to C:\Temp\" & $temp & @CRLF)
127EndIf
128
129; Optionally, clean up
130
131$rc = MsgBox(36, "Tidy up test folders and files?", "Would you like to remove the test files and folder that were copied to your systems?")
132If $rc == 6 Then
133        If FileExists($UNCpath & "\TestFile_1.txt") Then
134                If Not FileDelete($UNCpath & "\TestFile_1.txt") Then
135                        ConsoleWrite("Warning: Failed to delete " & $UNCpath & "\testFile_1.txt" & @CRLF)
136                EndIf
137        EndIf
138        If FileExists($UNCpath & "\Test File 2.txt") Then
139                If Not FileDelete($UNCpath & "\Test File 2.txt") Then
140                        ConsoleWrite("Warning: Failed to delete " & $UNCpath & "\Test File 2.txt" & @CRLF)
141                EndIf
142        EndIf
143        If FileExists($UNCpath & "\Test File 3.txt") Then
144                If Not FileDelete($UNCpath & "\Test File 3.txt") Then
145                        ConsoleWrite("Warning: Failed to delete " & $UNCpath & "\Test File 3.txt" & @CRLF)
146                EndIf
147        EndIf
148        If Not DirRemove("C:\temp\" & $temp, 1) Then
149                ConsoleWrite("Warning: Failed to delete C:\temp\" & $temp & " and contents" & @CRLF)
150        EndIf
151EndIf
152
153Exit