package dk.daoas.sdplusfilenames; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; /* Notes - * 1) first run the program with file deletion disabled to be sure the copy completes * 2) Run program afterwards with deletion enabled * 3) Run a query to update the attachment paths in database (table sdeskattachment): -- Removes ':' from attachmentName and attachmentPath update sdeskattachment set attachmentPath=replace(attachmentPath,':',''), attachmentName=replace(attachmentName,':','') where attachmentname like 'Ordrebekræftelse PostDanmark kontonr.%'; */ public class FilenamesMain { static byte buffer[] = new byte[64*1024*1024];//64MiB public static void copyFile(File input, File output) throws Exception { FileInputStream fin = new FileInputStream(input); int len = fin.read(buffer); FileOutputStream fout = new FileOutputStream(output); fout.write(buffer, 0, len); fin.close(); fout.close(); output.setLastModified( input.lastModified() ); boolean res = input.delete(); if (res == false) { System.out.println("Delete failed " + input.toString()); } } public static void handleRename(File file) throws Exception { String sourceName = "Ordrebekræftelse PostDanmark kontonr.: 799317873.pdf"; String targetName = "Ordrebekræftelse PostDanmark kontonr. 799317873.pdf"; File source = new File(file.getParentFile(), sourceName); File target = new File(file.getParentFile(), targetName); try { System.out.println( "Exists " + source.toString() + ": " + source.exists() ); copyFile(source,target); } catch (Exception e) { System.err.println(file.toString()); System.err.println(e.getMessage()); try { deleteFile(file); } catch (Exception e2) { System.err.println("DEL " + file.toString()); System.err.println("DEL " + e2.getMessage()); } } } static public void deleteFile(File file) throws Exception{ String cmd = "CMD /C DEL \"\\\\?\\" + file.toString() + "\"" ; System.out.println(cmd); Process p = Runtime.getRuntime().exec(cmd); } public static void recursiveRename(File folder) throws Exception { File files[] = folder.listFiles(); for (File file : files) { if (file.isDirectory() == true) { recursiveRename(file); } else { //ordinary file if ( file.getName().equals("Ordrebekræftelse PostDanmark kontonr.") ) { System.out.println( ">>" + file.toString() + "<<"); System.out.println( ">>" + file.getName() + "<<"); handleRename(file); } } } } public static void main(String[] args) throws Exception { File dir = new File("W:\\ManageEngine\\ServiceDesk\\fileAttachments"); recursiveRename(dir); /* File dir = new File("W:\\ManageEngine\\ServiceDesk\\fileAttachments\\Request\\May2013\\2325"); System.out.println("dir.exists() " + dir.exists() ); //true System.out.println("dir.canRead() " + dir.canRead() );//true // Filename as reported by windows file system File f1 = new File(dir, "Ordrebekræftelse PostDanmark kontonr." ); System.out.println("f1.exists() " + f1.exists() ); // false System.out.println("f1.canRead() " + f1.canRead() ); //false //filename as reported by sdeskattachment table in Postgresql File f2 = new File(dir, "Ordrebekræftelse PostDanmark kontonr.: 799317873.pdf" ); System.out.println("f2.exists() " + f2.exists() ); //true System.out.println("f2.canRead() " + f2.canRead() ); //true */ System.out.println("Done"); } }