Good day to all. I am trying to write a program that should bypass all the subdirectories in the folder and change the contents in the subdirectory. Essence: there is a folder, in it there are several hundreds / thousands of other folders, and in each of them there are already several files. It is necessary to bypass all of them, go into each, and depending on the extension, rename according to a template: go to the 1st folder - see blbl.json - rename to 1.json, the same with text, etc.

Code:

import java.io.File; public class Sorter{ //main procedure public static void main(String[] arg) { String Path = Utils.pathFileData; printTreeFiles(Path); } public static void printTreeFiles(String Path) { File Directory = new File(Path);//ccΡ‹Π»ΡŒ Π½Π° Π³Π»Π°Π²Π½ΡƒΡŽ ΠΏΠ°ΠΏΠΊΡƒ, Π²Π½ΡƒΡ‚Ρ€ΠΈ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ 1000 ΠΏΠ°ΠΏΠΎΠΊ File[] SubDirectory = Directory.listFiles(); getContent(SubDirectory); System.out.println(); } //recursive procedure for finding the contents of a directory public static void getContent(File[] SubDirectory) {//трясём ΠΏΠ°ΠΏΠΊΠΈ for (int i=0; i< SubDirectory.length; i++) {// // System.out.print("\t"); // System.out.println(Directory.getName()); //File[] SubDirectory = Directory.listFiles(); for (File SubWay:SubDirectory) {//для ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ Ρ„Π°ΠΉΠ»Π° ΠΈΠ· ΠΎΠ΄Π½ΠΎΠΉ ΠΈΠ· 1000 ΠΏΠ°ΠΏΠΎΠΊ // getContent(SubWay, i + 1); String buf = SubWay.getName(); // Ρ‡ΠΈΡ‚Π°Π΅ΠΌ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π΅ имя Ρ„Π°ΠΉΠ»Π° // System.out.println(buf); if (buf.contains("ARTICLE"))// { buf = buf.replace(".txt", "Article" + i + ".txt"); SubWay.renameTo(new File(Utils.pathFileData + buf)); } else if (buf.endsWith(".json")) { buf = buf.replaceFirst(".json", i + ".json"); SubWay.renameTo(new File(Utils.pathFileData + buf)); } else buf = buf.replaceFirst(".txt", i + ".txt"); SubWay.renameTo(new File(Utils.pathFileData + buf)); } } } } 

The code instead does the following: appends the name of the file with the extension .txt to the folder in which the file is located. Please help me figure out what needs to be changed in order to smell it not to askew.

Similar threads have already been read here, as the disc took the code from here: Recursive counting of files in the directory

  • one
    Utils.pathFileData + buf -> Utils.pathFileData + "/" + buf ? - user194374
  • @kff, tried, no difference, sort of. Most importantly, the files inside the folder still do not change. - TheDoctor

1 answer 1

It is not clear how the directories are numbered. If not, certain rules, then this should be the solution.

 public static void main(final String[] args) throws IOException { AtomicLong directoryNumber = new AtomicLong(0); Path start = Paths.get("/home/artem/"); Files.walkFileTree(start, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { String fileName = file.getFileName().toString(); String newFileName; int index = fileName.lastIndexOf('.'); if (index == -1) newFileName = fileName; else newFileName = directoryNumber.get() + fileName.substring(index); Path target = Paths.get(file.getParent().toString(), newFileName); Files.copy(file, target, StandardCopyOption.REPLACE_EXISTING); return FileVisitResult.CONTINUE; } @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { directoryNumber.incrementAndGet(); return super.preVisitDirectory(dir, attrs); } }); }