1 |
package dk.daoas.sdplusfilenames;
|
2 |
|
3 |
import java.io.File;
|
4 |
import java.io.FileInputStream;
|
5 |
import java.io.FileOutputStream;
|
6 |
|
7 |
/* Notes -
|
8 |
* 1) first run the program with file deletion disabled to be sure the copy completes
|
9 |
* 2) Run program afterwards with deletion enabled
|
10 |
* 3) Run a query to update the attachment paths in database (table sdeskattachment):
|
11 |
|
12 |
-- Removes ':' from attachmentName and attachmentPath
|
13 |
update sdeskattachment
|
14 |
set attachmentPath=replace(attachmentPath,':',''),
|
15 |
attachmentName=replace(attachmentName,':','')
|
16 |
where attachmentname like 'Ordrebekræftelse PostDanmark kontonr.%';
|
17 |
*/
|
18 |
|
19 |
public class FilenamesMain {
|
20 |
|
21 |
static byte buffer[] = new byte[64*1024*1024];//64MiB
|
22 |
|
23 |
public static void copyFile(File input, File output) throws Exception {
|
24 |
|
25 |
FileInputStream fin = new FileInputStream(input);
|
26 |
int len = fin.read(buffer);
|
27 |
|
28 |
FileOutputStream fout = new FileOutputStream(output);
|
29 |
fout.write(buffer, 0, len);
|
30 |
|
31 |
fin.close();
|
32 |
fout.close();
|
33 |
|
34 |
output.setLastModified( input.lastModified() );
|
35 |
|
36 |
boolean res = input.delete();
|
37 |
if (res == false) {
|
38 |
System.out.println("Delete failed " + input.toString());
|
39 |
}
|
40 |
}
|
41 |
|
42 |
public static void handleRename(File file) throws Exception {
|
43 |
|
44 |
String sourceName = "Ordrebekræftelse PostDanmark kontonr.: 799317873.pdf";
|
45 |
String targetName = "Ordrebekræftelse PostDanmark kontonr. 799317873.pdf";
|
46 |
|
47 |
File source = new File(file.getParentFile(), sourceName);
|
48 |
File target = new File(file.getParentFile(), targetName);
|
49 |
try {
|
50 |
System.out.println( "Exists " + source.toString() + ": " + source.exists() );
|
51 |
copyFile(source,target);
|
52 |
} catch (Exception e) {
|
53 |
System.err.println(file.toString());
|
54 |
System.err.println(e.getMessage());
|
55 |
|
56 |
try {
|
57 |
|
58 |
deleteFile(file);
|
59 |
|
60 |
} catch (Exception e2) {
|
61 |
System.err.println("DEL " + file.toString());
|
62 |
System.err.println("DEL " + e2.getMessage());
|
63 |
}
|
64 |
}
|
65 |
}
|
66 |
|
67 |
static public void deleteFile(File file) throws Exception{
|
68 |
String cmd = "CMD /C DEL \"\\\\?\\" + file.toString() + "\"" ;
|
69 |
|
70 |
System.out.println(cmd);
|
71 |
Process p = Runtime.getRuntime().exec(cmd);
|
72 |
|
73 |
}
|
74 |
|
75 |
public static void recursiveRename(File folder) throws Exception {
|
76 |
File files[] = folder.listFiles();
|
77 |
|
78 |
for (File file : files) {
|
79 |
|
80 |
if (file.isDirectory() == true) {
|
81 |
recursiveRename(file);
|
82 |
} else { //ordinary file
|
83 |
|
84 |
if ( file.getName().equals("Ordrebekræftelse PostDanmark kontonr.") ) {
|
85 |
System.out.println( ">>" + file.toString() + "<<");
|
86 |
System.out.println( ">>" + file.getName() + "<<");
|
87 |
handleRename(file);
|
88 |
}
|
89 |
}
|
90 |
|
91 |
}
|
92 |
|
93 |
}
|
94 |
|
95 |
public static void main(String[] args) throws Exception {
|
96 |
File dir = new File("W:\\ManageEngine\\ServiceDesk\\fileAttachments");
|
97 |
recursiveRename(dir);
|
98 |
/*
|
99 |
|
100 |
File dir = new File("W:\\ManageEngine\\ServiceDesk\\fileAttachments\\Request\\May2013\\2325");
|
101 |
|
102 |
System.out.println("dir.exists() " + dir.exists() ); //true
|
103 |
System.out.println("dir.canRead() " + dir.canRead() );//true
|
104 |
|
105 |
// Filename as reported by windows file system
|
106 |
File f1 = new File(dir, "Ordrebekræftelse PostDanmark kontonr." );
|
107 |
|
108 |
System.out.println("f1.exists() " + f1.exists() ); // false
|
109 |
System.out.println("f1.canRead() " + f1.canRead() ); //false
|
110 |
|
111 |
//filename as reported by sdeskattachment table in Postgresql
|
112 |
File f2 = new File(dir, "Ordrebekræftelse PostDanmark kontonr.: 799317873.pdf" );
|
113 |
System.out.println("f2.exists() " + f2.exists() ); //true
|
114 |
System.out.println("f2.canRead() " + f2.canRead() ); //true
|
115 |
*/
|
116 |
|
117 |
System.out.println("Done");
|
118 |
}
|
119 |
}
|