Skip to content

Instantly share code, notes, and snippets.

@KenjiOhtsuka
Last active May 21, 2018 02:25
Show Gist options
  • Save KenjiOhtsuka/7147806 to your computer and use it in GitHub Desktop.
Save KenjiOhtsuka/7147806 to your computer and use it in GitHub Desktop.
Script to split text file by some amount of lines. It works on Windows, using JScript. テキストファイルを行単位で分割するスクリプト。Windows で動きます。JScript です。いくつか使われない変数が宣言されてるっぽいですが・・・。
/*==========================================================
Declaration
==========================================================*/
// constants
var ForReading = 1; // 読み込み
var ForWriting = 2; // 書きこみ(上書きモード)
var ForAppending = 8; // 書きこみ(追記モード)
var fileName = "File Name";
/*==========================================================
実行部
==========================================================*/
splitFile();
ForReading = null;
ForWriting = null;
ForAppending = null;
/*==========================================================
Function Declaration
==========================================================*/
function splitFile() {
var strFolderPath;
var objFileSys;
var objInFile;
var objOutFile;
var strScriptPath;
var strRecord;
var index = 0;
objFileSys = new ActiveXObject("Scripting.FileSystemObject");
strScriptPath = String(WScript.ScriptFullName).replace(WScript.ScriptName,"");
objInFile = objFileSys.OpenTextFile(strScriptPath + fileName, ForReading);
try {
var i, writingSize;
do {
index++;
objOutFile = objFileSys.CreateTextFile(strScriptPath + "out" + index.toString() + ".txt", true);
for (i = 0; i < 1000000; i++) {
strRecord = objInFile.ReadLine();
objOutFile.WriteLine(strRecord);
if (objInFile.AtEndOfStream == true) break;
}
objOutFile.Close();
} while (objInFile.AtEndOfStream==false);
} catch(e) {
WScript.echo("Error!");
WScript.echo(strScriptPath + "out" + index.toString() + ".txt");
} finally {
objInFile.Close();
objOutFile.Close();
}
// discard objects
objFileSys = null;
objInFile = null;
objOutFile = null;
strScriptPath = null;
strRecord = null;
strFolderPath = null;
return 0;
}
@Makoyo
Copy link

Makoyo commented Aug 26, 2016

初めまして。
上記のスクリプトで、ヘッダーを1行必ず付けて分割するにはどうしたらいいでしょうか?

@KenjiOhtsuka
Copy link
Author

@Makoyo

各ファイルの先頭に1行入れたいなら
44 行目 に 下のように記述すればいいです。

objOutFile.WriteLine("HEADER");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment