Created
February 22, 2015 02:07
-
-
Save ujiro99/0348e36d223ff47d3878 to your computer and use it in GitHub Desktop.
選択したファイルのリビジョン番号をインクリメントし、元のファイルのバックアップを行う、秀丸ファイラ用のスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FileOperator | |
# constant | |
BACKUP_DIR_NAME = "bak" | |
NO_OVERWRITE = "false" | |
PATH_SEPARATOR = "/" | |
EXTENSION_SEPARATOR = "." | |
REVISION_INIT = 2 | |
# fields | |
_fso = new ActiveXObject('Scripting.FileSystemObject') | |
_regRevision = /r(\d+)$/ | |
constructor: (@_selectedPath) -> | |
@_selectedBaseName = _fso.GetBaseName(_selectedPath) | |
@_selectedExtensionName = _fso.GetExtensionName(_selectedPath) | |
@_parentDir = _fso.GetParentFolderName(_selectedPath) | |
createBackUpDir: () -> | |
backUpDir = @_parentDir + PATH_SEPARATOR + BACKUP_DIR_NAME | |
# バックアップ先が存在しない場合は作成 | |
if not _fso.FolderExists(backUpDir) | |
_fso.CreateFolder(backUpDir) | |
backUpFile: () -> | |
selectedFileName = _fso.GetFileName(@_selectedPath) | |
backUpPath = @_parentDir + PATH_SEPARATOR + BACKUP_DIR_NAME + PATH_SEPARATOR + selectedFileName | |
if _fso.FileExists(backUpPath) | |
# バックアップ先に同名ファイルが存在する場合、 | |
# ファイル名末尾に".2"をつけてバックアップする | |
selectedFileName = @_selectedBaseName + ".2" + EXTENSION_SEPARATOR + @_selectedExtensionName | |
backUpPath = @_parentDir + PATH_SEPARATOR + BACKUP_DIR_NAME + PATH_SEPARATOR + selectedFileName | |
_fso.CopyFile(@_selectedPath, backUpPath, NO_OVERWRITE) | |
else | |
_fso.CopyFile(@_selectedPath, backUpPath, NO_OVERWRITE) | |
bumpUpFile: () -> | |
matched = @_selectedBaseName.match(_regRevision) | |
if matched && matched.length >= 2 | |
# ファイル名末尾にリビジョン番号が付いている場合は | |
# リビジョン番号をインクリメントする | |
newBaseName = @_selectedBaseName.replace(_regRevision, "r" + ++matched[1]) | |
else | |
# ファイル名末尾にリビジョン番号が付いていない場合は | |
# リビジョン番号を付加する | |
newBaseName = @_selectedBaseName + "_r" + REVISION_INIT | |
newpath = @_parentDir + PATH_SEPARATOR + newBaseName + EXTENSION_SEPARATOR + @_selectedExtensionName | |
_fso.MoveFile(@_selectedPath, newpath) | |
# 選択されているファイルを対象にする | |
# 選択されていない場合は終了する | |
selectedPath = GetItemPath(GetNextItem(-1,2)) | |
if not selectedPath then return | |
fo = new FileOperator(selectedPath) | |
fo.createBackUpDir() | |
fo.backUpFile() | |
fo.bumpUpFile() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment