commit 2d70878d6b0359335e91f7a9c6ba099d624c571f Author: Bene Date: Wed Nov 22 16:19:04 2023 +0100 initial commit diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..1acf042 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..206ff41 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/SatReceiverUmbenenner.iml b/SatReceiverUmbenenner.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/SatReceiverUmbenenner.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..ebe21d5 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,187 @@ +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Files; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.Set; + +// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`, +// then press Enter. You can now see whitespace characters in your code. +public class Main { + public static void main(String[] args) { + // /media/ubuntu/BACKUP/toRename" + File f = new File(args[0]); + if (f.isDirectory()) { + File[] list = f.listFiles(); + for (File file : list) { + //System.out.println(file); + benenneUm(file); + } + } + else + System.out.println("Angegebener Pfad ist nicht vorhanden oder kein Verzeichnis!\n"+args[0]); + + } + + public static String getCharFromByte(byte b) + { String preview = String.valueOf((char)b); + if(b > 32 && b < 48 || b > 57 && b < 65 || b > 90 && b < 97 || b > 122) + return " "; + return switch (Byte.toUnsignedInt(b)) { + case 246 -> "ö"; + case 228 -> "ä"; + case 223 -> "ß"; + case 252 -> "ü"; + case 196 -> "Ä"; + default -> String.valueOf((char)b); + }; + } + public static void benenneUm(File f) { + String[] l = f.list(); + for (File content : f.listFiles()) { + String name = content.getName(); + if (name.equals("REC.TS")) { + File newFile = new File(content.getParentFile().getPath()+"/REC.00"); + boolean success = content.renameTo(newFile); + if(!success) + System.out.println("konnte datei nicht umbenennen"); + } + + if (content.getName().equals("META.DAT")) { + try { + byte[] fileContent = Files.readAllBytes(content.toPath()); + ArrayList parts = splitByteArray(fileContent); + StringBuilder sender = new StringBuilder(); + StringBuilder programm = new StringBuilder(); + for (byte b : parts.get(0)) { + sender.append(getCharFromByte(b)); + } + for (byte b : parts.get(1)) { + programm.append(getCharFromByte(b)); + } + String pattern = "yyyy-MM-dd"; + SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); + if(programm.toString().length() < 2) + programm = new StringBuilder(); + else + programm.insert(0,"_"); + String date = simpleDateFormat.format(new Date(content.lastModified())); + System.out.println(f.getName() + ": " + date +"_"+sender+programm ); + File oldFile = content.getParentFile(); + String newFileName = oldFile.getParentFile().getPath()+"/"+date +"_"+sender+programm; + newFileName = newFileName.replace(" {2}", " ").trim(); + File newFile = new File(newFileName); + + boolean success = oldFile.renameTo(newFile); + if(!success) + System.out.println("konnte datei nicht umbenennen"); + + /* + BufferedReader reader = new BufferedReader(new FileReader(content)); + String currentLine = ""; + String output = ""; + currentLine = reader.readLine(); + while (currentLine != null) { + output += currentLine; + currentLine = reader.readLine(); + } + + reader.close(); + */ + //System.out.println(f.getName() + ": " + f.lastModified() + ": " + output.replaceAll("\\p{C}", "")); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + } + + public static ArrayList splitByteArray(byte[] input) + { + input = getPartOfArray(input, 13, input.length); + int start = 0; + int end = 0; + byte[] sender = new byte[0]; + byte[] programm = new byte[0]; + boolean hasFoundProgramm = false; + StringBuilder textOutput = new StringBuilder(); + byte lastByte = Byte.MIN_VALUE; + for (byte b : input) { + + + if(b==0) + { + //System.out.print(Byte.toUnsignedLong(b)); + if(sender.length == 0) { + sender = getPartOfArray(input, start, end); + start = end; + } + else if(hasFoundProgramm) + { + programm = getPartOfArray(input, start, end); + break; + } + textOutput.append(" "); + }else if(sender.length > 0 && b == 10)//Zeilenumbruch + { + start = end+1; + hasFoundProgramm = true; + } + else { + if(lastByte == 0) { + //System.out.print(" "); + textOutput.append(" "); + } + //System.out.printf("%02x", b); + //System.out.print(" "); + if(Byte.toUnsignedInt(b) == 246) + textOutput.append(" ö ");//148 + else if(Byte.toUnsignedInt(b) == 228) + textOutput.append(" ä ");//132 + else if(Byte.toUnsignedInt(b) == 223) + textOutput.append(" ß ");//225 + else if(Byte.toUnsignedInt(b) == 252) + textOutput.append(" ü ");//129 + else if(Byte.toUnsignedInt(b) == 196) + textOutput.append(" Ä "); + else if(Byte.toUnsignedInt(b) == 32) + textOutput.append(" _ "); + else if(b != 10) + textOutput.append(" ").append((char) b).append(" "); + else + textOutput.append(" "); + } + end++; + lastByte = b; + + } + //System.out.println(); + //System.out.println(textOutput); + String humanText = textOutput.toString().replaceAll("\\p{C}", ""); + humanText = humanText.replaceAll("\\s+", ""); + humanText = humanText.replaceAll("_", " "); + + //System.out.println(humanText); + + //System.out.println(); + ArrayList result = new ArrayList<>(); + result.add(sender); + result.add(programm); + return result; + + } + + public static byte[] getPartOfArray(byte[] array, int start, int end) + { + byte[] result = new byte[end-start]; + int index = 0; + for (int i = start; i < end; i++) { + result[index] = array[i]; + index++; + } + return result; + } +}