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 (↓).
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.
[Ctrl] + [C]
→ [Ctrl] + [V]
)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.
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.
The figure below shows the execution result. It's convenient because you can perform backup with just one click.
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
There was an incorrect description that "Box" is a "Microsoft service", so I corrected it. We apologize for the correction.
Recommended Posts