[JAVA] I want to easily back up files used at work

Problems in the company

At work, I often keep a backup of my materials. The main reason is file versioning.

The method is to copy the file on Explorer and enter the date and time in the file name so that you can see the modification date and time of each file before modifying the latest version. It looks like this (↓). pic-20190705T1416.png

There are many services in the world that can conveniently manage file versions, such as Box, and I think many people are using them. However, this method also has advantages.

--You can tell when it was updated just by looking at the file name. --Since the file name is different for each version, it is easy to distinguish (in Box etc., version control is done with the same file name). -** Obviously, but free **.

On the other hand, the ** troublesome point ** of this management method is that the procedure is complicated as follows.

  1. Copy and paste "Proposal.docx" ([Ctrl] + [C][Ctrl] + [V])
  2. You will have a file named "Proposal-Copy.docx".
  3. Change the "--copy" part of the file name to date and time. It is troublesome because you enter while checking the modification date of the file.

It doesn't bother me once, but as I repeat this every day and many times, it becomes quite annoying. Also, if you make a mistake in the date due to a typo, the update order will be broken when arranging by file name.

I made a tool so that I could back it up in an instant

So I decided to make a tool and make it easier. The condition is that the file named "-latest" immediately before the file extension, such as "Proposal -latest.docx".

If you drag and drop the file to the tool on Explorer, it will be automatically backed up and the file name will be updated. The batch file backup.bat is a tool, just drag and drop the target file into the batch file.

In the batch file, I am running a Java program. pic-20190705T1556.png

The figure below shows the execution result. It's convenient because you can perform backup with just one click. pic-20190705T1435.png

Source code

The source code looks like this. It works with a combination of Java and a batch file. Compile your Java program and put the Class and batch files in the same folder.

At runtime, drag and drop the file you want to back up to the batch file on Explorer as shown in the figure above. Make a backup in the same folder as the target file.

Also, in my case, I create a shortcut for the tool (batch file) on the desktop and drag and drop it on the shortcut.

CopyAndBackupFile.java


import java.net.*;
import java.nio.file.*;
import java.text.*;
import java.util.*;

public class CopyAndBackupFile {
  private static final String DEFAULT_PLACE_HOLDER = "latest";
  private static final SimpleDateFormat FORMAT_PREFIX = new SimpleDateFormat("YYYYMMdd");
  private static final SimpleDateFormat FORMAT_SUFFIX = new SimpleDateFormat("HHmm");

  public static void main(String[] args) throws Exception {
    int argc = 0;
    String placeHolder;
    
    if (args[argc].equals("-p")) {
      placeHolder = args[++argc];
      ++argc;
    } else {
      placeHolder = DEFAULT_PLACE_HOLDER;
    }

    assert placeHolder != null : "Placeholder is null";

    Path[] paths = new Path[args.length - argc];
    for (int i = 0; i < paths.length; i++, argc++) {
      paths[i] = Paths.get(args[argc]);
    }

  rename_loop:
    for (int i = 0; i < paths.length; i++) {
      Path pathOrg = Paths.get(paths[i].toString());
      Path dir = pathOrg.getParent();
      Path file = pathOrg.getFileName();

      String sFile = file.toString();
      
      int phStart = sFile.lastIndexOf(placeHolder);
      if (phStart < 0) {
        System.err.printf("ERROR: %s does not include \"%s\"%n", sFile, placeHolder);
        continue rename_loop;
      }
      int phEnd = phStart + placeHolder.length();
      
      long lastModified = pathOrg.toFile().lastModified();
      Date date = new Date(lastModified);
      
      String backupDate = FORMAT_PREFIX.format(date)
                                       .concat("T")
                                       .concat(FORMAT_SUFFIX.format(date));
      

      String sFileNew = sFile.substring(0, phStart)
                             .concat(backupDate)
                             .concat(sFile.substring(phEnd));
      
      Path pathNew = Paths.get(dir.toString(), sFileNew);
      
      System.out.printf("%s -> %s%n", pathOrg, pathNew);
      Files.copy(pathOrg, pathNew);
    }
  }
}

backup.bat


@echo off
setlocal

rem
rem The top directory of the tool
rem
set __tools_dir=%~d0%~p0
set __last_char=%__tools_dir:~-1%
if "%__last_char%"=="\" set __tools_dir=%__tools_dir:~0,-1%


rem
rem Environment variables
rem
set __java_bin_dir=C:\ProgramData\Oracle\Java\javapath
set __classpath=%__tools_dir%

if "%JAVA_HOME%"=="" (
  echo WARNING: JAVA_HOME is empty. Using %__java_bin_dir%
) else ( 
  set __java_bin_dir=%JAVA_HOME:"=%\bin
)

if not exist "%__java_bin_dir%\java.exe" (
  echo ERROR: %__java_bin_dir%\java.exe does not exist
  echo ERROR: set JAVA_HOME to the top directory of Java SE 7 or later
  goto :EOF
)

set __java_options=


rem 
rem Run
rem
@echo on
"%__java_bin_dir%\java.exe" %__java_options% -classpath "%__classpath%" CopyAndBackupFile %*
@echo off

@endlocal

Article correction (July 14, 2019)

There was an incorrect description that "Box" is a "Microsoft service", so I corrected it. We apologize for the correction.

Recommended Posts

I want to easily back up files used at work
I want to docker-compose up Next.js!
Links I want to help when JSF doesn't work at all
I want to RSpec even at Jest!
I want to delete files managed by Git
I want to convert characters ...
I want to recursively search for files under a specific directory
I want to avoid OutOfMemory when outputting large files with POI
Swift: I want to chain arrays
I want to use FormObject well
I want to convert InputStream to String
I want to write JSP in Emacs more easily than the default.
I want to develop a web application!
I tried to summarize the methods used
I want to write a nice build.gradle
I want to eliminate duplicate error messages
I want to make an ios.android app
I want to display background-ground-image on heroku.
I want to use DBViewer with Eclipse 2018-12! !!
I want to write a unit test!
I want to install PHP 7.2 on Ubuntu 20.04.
I want to stop Java updates altogether
I want to use @Autowired in Servlet
I want to target static fields to @Autowired
I want to do team development remotely
[Android] I want to create a ViewPager that can be used for tutorials