[JAVA] Automatically transfer animations for each title and store them in a folder

Preface

Since the draft is full, I will post it for the time being The credibility of the content may be a little because of what I did in the past. But it's a waste to erase it, so I posted it.

Main subject

After recording with chinachu, I downloaded it to Windows and encoded it to mp4. However, as the number of files increased, I wanted to organize them.

What to do with this

  1. Extract all mp4s in the specified folder ** ← bat file **
  2. Extract text before white space in file name ** ← java ** from here
  3. Search for the same folder as the text extracted in 2.
  4. Create if the same folder does not exist
  5. Move the file to the corresponding folder

code

animeSelection.bat

animeSelection.bat


@echo off

#Mp4 in the corresponding folder mp4 FileName.Store in txt
dir C:\ Users \ izumi \ Videos \ Anime \*.mp4 /a-d /b > mp4FileName.txt

# mp4FileName.Take txt line by line and pass it to java
for /f %%a in (mp4FileName.txt) do ( ^
  java animeSelection.animeBatch %%a
  timeout 3
)

animeSelection.java

animeSelection.java


package animeSelection;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class animeBatch {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String filename = args[0]; //Store arguments
		String title = null;
		System.out.println(filename);
		
		int i = filename.indexOf(" "); //Get the position of double-byte space
		if(i == 0){
			System.out.println("The corresponding character cannot be found.");
		} else {
			System.out.println("The end position is" + i + "is.");
			
			title = filename.substring(0, i); //Cut before the blank
			System.out.println(title);
		}
		
		
		File dir = new File("c://Users/izumi/Videos/Anime");
		File[] list = dir.listFiles();
		System.out.println(list.length);
		
		int ll = list.length;
		
		//Check the files in the folder
		for (int j = 0; j < ll; j++){
			if(list[j].isFile()){
				//System.out.println("[F] " + fn.getName());
			} else if(list[j].isDirectory()){
				//System.out.println("[D] " + fn.getName());
				String dname = list[j].getName();
				System.out.println("dname: " + dname);
				System.out.println("title: " + title);
				if(dname.equals(title)){
					System.out.println(title + "had.");
					moveDir(title, dir, filename); //If there is a folder, store the file with moveDir
					break;
				}
			}
			if(j == ll-1){
				makeDir(title, dir);
				moveDir(title, dir, filename);
			}
		}
	}


	private static void makeDir(String title, File dir) {
		//Create a folder
		File newdir = new File(dir + "/" + title);
		if(newdir.mkdir()){
			System.out.println("ooooo folder creation successful ooooo");
		} else {
			System.out.println("xxxxx Folder creation failed xxxxx");
		}
	}

	private static void moveDir(String title, File dir, String filename) {
		//Store the file in the corresponding folder
		System.out.println("I'll move the file.");
		try{
			Path sourcePath =Paths.get(dir + "/" + filename);
			Path tergetPath = Paths.get(dir + "/" + title + "/" + filename);
			System.out.println(sourcePath + "To" + tergetPath + "Go to.");
			Files.move(sourcePath, tergetPath);
			System.out.println("Successful move");
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Move failure");
		}
	}

}

Recommended Posts

Automatically transfer animations for each title and store them in a folder
[Java] for Each and sorted in Lambda
Store in Java 2D map and turn with for statement
[Java] Make variables in extended for statement and for Each statement immutable
[Learning record] I got the current time in Ruby and output a different greeting for each time.