0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ファイルをバックアップ(履歴ツール)

Last updated at Posted at 2023-02-05

Something went wrong
uploading...0
Something went wrong
FileBack


package jp.co.main;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;

import org.apache.commons.io.FilenameUtils;
import org.junit.Test;

public class FileBack {

    public static void main(String[] args) {

        String filepath = null;
        for (String string : args) {
            File file = new File(string);

            if (filepath == null) {
                filepath = file.getParent();
            }

            String fileName = file.getName();

            String fileSuffix = FilenameUtils.getExtension(fileName).toLowerCase();

            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            String systemDate = formatter.format(calendar.getTime());

            String replasString = "_" + systemDate + "." + fileSuffix;

            String targetPath = null;
            String bakfolder = null;

            try {
                Properties prop = redProperties();
                targetPath = prop.getProperty("historyOutputPath");
                bakfolder = prop.getProperty("bakfolder");
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            if (targetPath == null) {
                bakfolder = bakfolder == null || bakfolder == "" ? "/bak/" : "/" + bakfolder + "/";
                targetPath = filepath + bakfolder;
            }

            File targetPathfile = new File(targetPath);

            if (!targetPathfile.exists()) {
                targetPathfile.mkdirs();
            }

            String targetFile = targetPath + fileName.replace("." + fileSuffix, replasString);

            try {
                System.out.println(targetFile);
                Files.copy(Paths.get(string), Paths.get(targetFile), StandardCopyOption.REPLACE_EXISTING);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        System.out.println("完了");
    }

    public static Properties redProperties() throws IOException {
        String currentpath = System.getProperty("user.dir");
        String filepath = currentpath + "\\appmanager.properties";
        InputStream inputStream = new FileInputStream(filepath);
        InputStreamReader sfr = new InputStreamReader(inputStream, "utf-8");
        Properties properties = new Properties();
        properties.load(sfr);

        return properties;
    }

    @Test
    public void testName() throws Exception {
        redProperties();
    }
}


pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>lession</groupId>
  <artifactId>HistoryBaK</artifactId>
  <version>0.0.1-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
    </dependencies>
</project>

excutehistoryBck.bat
*excutehistoryBck.batをSendToに置きます。
%APPDATA%\Microsoft\Windows\SendTo

java -jar historyBck.jar %*

appmanager.properties

historyOutputPath=
bakfolder=履歴
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?