行番号を付加するスクリプト
行番号を付加するスクリプトです。(ドロップの練習)
【特長】WSHなのでwindowsならソフト不要。
【使い方】スクリプト名を addLineNo.vbs にして
対象のファイルをドロップする。
【ソース】
'======================================================== '[ファイル名]addLineNo.vbs '[機能]行番号付加 ' Date Name Comment ' 2004/08/27 garyo New '======================================================== Set objArgs = WScript.Arguments For I = 0 to objArgs.Count - 1 OpenTextFileTest(objArgs(I)) Next WScript.Echo "終了" '======================================================== '[名称]OpenTextFileTest(path) '[機能]行番号付加 '[入力]path:ファイル名を含んだフルパス '[出力]ファイル名1.拡張子 のファイルに行番号を付加して出力 ' Date Name Comment ' 2004/08/27 garyo New '======================================================== Sub OpenTextFileTest(path) Const ForReading = 1, ForWriting = 2, ForAppending = 8 Dim fsoIn,fsoOut, fIn,fOut,rdLine Set fsoIn = CreateObject("Scripting.FileSystemObject") Set fsoOut = CreateObject("Scripting.FileSystemObject") Set fIn = fsoIn.OpenTextFile(path, ForReading, True) Set fOut = fsoOut.OpenTextFile(GetNewFileName(path), ForWriting, True) WScript.Echo GetNewFileName(path) count = 0 Do While fIn.AtEndOfLine <> True rdLine = fIn.ReadLine count = count + 1 fOut.WriteLine count & ":" & rdLine Loop fOut.Close fIn.Close End Sub '======================================================== '[名称]dir(filespec) '[機能]ファイルが存在するか確認 '[入力]filespec:ファイル名を含んだフルパス '[出力]1:ファイルがある。0:ファイルがない ' Date Name Comment ' 2004/08/27 garyo New '======================================================== Function dir(filespec) Dim fso, msg Set fso = CreateObject("Scripting.FileSystemObject") If (fso.FileExists(filespec)) Then msg = 1 '存在します。 Else msg = 0 '存在しません。 End If dir = msg End Function '======================================================== '[名称]GetTheBase(filespec) '[機能]ファイル名本体を得る ' 例:c:\test.vbs → test '[入力]filespec:ファイル名を含んだフルパス '[出力]ファイル名本体 ' Date Name Comment ' 2004/08/27 garyo New '======================================================== Function GetTheBase(filespec) Dim fso Set fso = CreateObject("Scripting.FileSystemObject") GetTheBase = fso.GetBaseName(filespec) End Function '======================================================== '[名称]GetAnExtension(DriveSpec) '[機能]拡張子を得る ' 例:c:\test.vbs → vbs '[入力] DriveSpec:ファイル名を含んだフルパス '[出力]拡張子 ' Date Name Comment ' 2004/08/27 garyo New '======================================================== Function GetAnExtension(DriveSpec) Dim fso Set fso = CreateObject("Scripting.FileSystemObject") GetAnExtension = fso.GetExtensionName(Drivespec) End Function '======================================================== '[名称]GetAName(DriveSpec) '[機能]ファイル名を得る ' 例:c:\test.vbs → test.vbs '[入力] path:ファイル名を含んだフルパス '[出力]ファイル名 ' Date Name Comment ' 2004/08/27 garyo New '======================================================== Function GetAName(DriveSpec) Dim fso Set fso = CreateObject("Scripting.FileSystemObject") GetAName = fso.GetFileName(DriveSpec) End Function '======================================================== '[名称]GetPath(path) '[機能]パス名を得る ' 例:c:\test.vbs → c:\ '[入力] path:ファイル名を含んだフルパス '[出力]パス名 ' Date Name Comment ' 2004/08/27 garyo New '======================================================== Function GetPath(path) GetPath=left(path,len(path)-len(GetAName(path))) End Function '======================================================== '[名称]GetBackupFileName(path) '[機能]バックアップ用のファイル名を作成する ' 例:c:\test.vbs → c:\test.bak '[入力] path:ファイル名を含んだフルパス '[出力]ファイル名の拡張子を「bak」に変えたもの ' Date Name Comment ' 2004/08/27 garyo New '======================================================== Function GetBackupFileName(path) GetBackupFileName=GetPath(path) & GetTheBase(path) & ".bak" End Function '======================================================== '[名称]GetNewFileName(path) '[機能]出力用の新しいファイル名を作成する ' 例:c:\test.vbs → c:\test1.vbs '[入力] path:ファイル名を含んだフルパス '[出力]ファイル名の本体に「1」をつけた名前 ' Date Name Comment ' 2004/08/27 garyo New '======================================================== Function GetNewFileName(path) GetNewFileName=GetPath(path) & GetTheBase(path) & "1." & GetAnExtension(path) End Function