Last active
December 26, 2017 09:21
-
-
Save jiro4989/bb9f3ccfcafd873fb19c2456bfb926a2 to your computer and use it in GitHub Desktop.
編集対象を一時的に別の外部コマンドで編集して処理するスクリプト(powershell, python, go)
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
rm dummy/* | |
1..120 | % { $num="dummy\" + ([string]$_) + ".zip"; echo "" > $num} |
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
package main | |
import ( | |
"fmt" | |
"io" | |
"io/ioutil" | |
"log" | |
"os" | |
"os/exec" | |
"strings" | |
) | |
func init() { | |
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) | |
} | |
func main() { | |
dirName := "dummy" | |
targetListFile := "target_list_file.txt" | |
// 一覧ファイルの削除 | |
if err := os.Remove(targetListFile); err != nil { | |
log.Fatal(err) | |
} | |
// 空の一覧ファイルの生成 | |
var b []byte | |
if err := ioutil.WriteFile(targetListFile, b, os.ModePerm); err != nil { | |
log.Fatal(err) | |
} | |
// ファイル一覧の取得 | |
files, err := ioutil.ReadDir(dirName) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// ファイル一覧をファイルに書き込み | |
for _, file := range files { | |
f, err := os.OpenFile(targetListFile, os.O_WRONLY|os.O_APPEND, os.ModePerm) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fp := dirName + "/" + file.Name() + "\r\n" | |
_, err = io.WriteString(f, fp) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
fileCnt := len(files) | |
// メモ帳の呼び出し | |
// TODO: プロセスをロックしてしまって上書き保存できない | |
cmd := exec.Command("notepad.exe", targetListFile) | |
if err = cmd.Run(); err != nil { | |
log.Fatal(err) | |
} | |
// メモ帳で編集したファイルの中身を読み込む | |
b, err = ioutil.ReadFile(targetListFile) | |
if err != nil { | |
log.Fatal(err) | |
} | |
newFilesText := fmt.Sprintf("%s", b) | |
newFiles := strings.Split(newFilesText, "\n") | |
newFilesCnt := len(newFiles) | |
fmt.Println(newFilesText) | |
fmt.Sprintf("old file count is %d.\n", fileCnt) | |
fmt.Sprintf("new file count is %d.\n", newFilesCnt) | |
} |
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
$dirName = "dummy" | |
$targetListFile = "target_list_file.txt" | |
# 空の一覧ファイルを先に生成 | |
rm $targetListFile | |
New-Item -ItemType file $targetListFile | |
# 編集対象一覧を取得してファイルに書き込む | |
ls $dirName | % { echo $("$dirName\" + $_.Name) >> $targetListFile } | |
$fileCount = (ls $dirName).Count | |
# メモ帳で編集 | |
Start-Process -Wait notepad $targetListFile | |
# 編集後確認 | |
cat $targetListFile | |
$newFileCount = (cat $targetListFile).Count | |
echo "old file count is $fileCount" | |
echo "new file count is $newFileCount" | |
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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import os, os.path, sys | |
import subprocess as sub | |
def main(): | |
dirname = "dummy" | |
target_list_file = "target_list_file.txt" | |
# 編集対象のファイル一覧を取得 | |
files = os.listdir(dirname) | |
# 編集対象の一覧ファイルの作成 | |
with open(target_list_file, "w") as outfile: | |
for f in files: | |
f = os.path.join(dirname, f) | |
print(f) | |
outfile.write(f) | |
outfile.write("\n") | |
# メモ帳で一覧を開く | |
print("open notepad") | |
sub.call("notepad.exe " + target_list_file) | |
print("notepad is closed.") | |
# メモ帳で編集したあとのファイルを読み込む | |
with open(target_list_file) as infile: | |
# ファイル数が100万とか行くことは | |
# (多分)ないのでもう一気に全部読み込んじゃう | |
lines = infile.readlines() | |
for line in lines: | |
sys.stdout.write(line) | |
print("old file count is", len(files)) | |
print("new file count is", len(lines)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment