advanced help
per page, with , order by , clip by
Results of 1 - 1 of about 1215 for (Mook) (0.001 sec.)
[[20150501080731]]
#score: 9211
@digest: dd85c953175a3c984bca51073157bf23
@id: 67902
@mdate: 2015-05-02T14:05:46Z
@size: 7919
@type: text/plain
#keywords: alltxtmei (84670), txtmei (37464), alltxtpath (36340), newtxtpath (27999), myprocess (27628), echo (26199), 4444444444444444 (22494), myexitcode (22494), getexitcodeprocess (15241), pending (14536), openprocess (13180), closehandle (13013), all (10885), チフ (9114), process (8427), txt (7821), fol (5523), cmd (4252), kernel32 (3905), ッチ (3779), 改行 (3699), バッ (3238), コマ (2750), ファ (2398), 結合 (2382), マン (2321), declare (2171), copy (2065), ンド (1872), ァイ (1856), 後半 (1669), テキ (1658)
『コマンドラインで複数のテキストファイルを改行しながら結合』(のらじろう)
エクセルとは直接関係ないのですが、↓のスレッドに関しての質問です。 [[20150427163551]] 『複数のテキストファイルを1つにする』(TOTO) たとえば、下記のような4つのテキストファイルを結合する場合です。 ------------- 1.txt 1111 1111 ------------ 2.txt 222 22 ------------- 3.txt 3333 33333 ------------- 4.txt 44444444444444 4444444444444444 ------------- コマンドラインやバッチファイルで結合したら↓のようになると思います。 Mookさんご呈示のコマンドです。 Copy %1 + %2 + %3 + %4 All.txt ------------------ All.txt 1111 1111222 223333 3333344444444444444 4444444444444444 ---------------- これを↓のようにファイルとファイルの間に改行を挟みながら結合したい場合 はどのようにすればいいでしょうか? ---------------- All.txt 1111 1111 222 22 3333 33333 44444444444444 4444444444444444 --------------- VBAなどでテキストファイルの内容を読み込み、改行を挟みながら結合する、 という方法はわかっています。 これをコマンドラインで出来たら便利だな、と考えています。 方法がありましたらご教示お願いいたします。 < 使用 Excel:Excel2007、使用 OS:WindowsVista > ---- 検索で↓の記述がありました。 http://cmd-pro.com/m_com.html >つまり、結合したからと言って、改行を自動的に付与してくれるわけでは無いため、 >結合される側のファイルの最後部には、改行を意識していれる必要がある。 解決方法は書かれてないのでコマンドでは用意されてない、ということなのでしょうか。 (のらじろう) 2015/05/01(金) 08:47 ---- 完全にかぶりましたが、メモしましたので。 まったく素人ですぺっている公算大ですが。 元ネタのテキストファイルの最後の行も改行した上で作成しておくことが必要なのでは? 自動的に改行を挿入してくれそうもないようですから。 http://cmd-pro.com/m_com.html 逆に、改行で終わっていなければ連続して1行にしたいなんて人も、中にはいるかも(まず、いないでしょうけど) (β) 2015/05/01(金) 09:02 ---- βさん、ご回答ありがとうございます。 やはり自分で改行のみのファイルを間に挟む、などと するしか方法がないようです。 http://aver-ats.com/wordPress/cmdprompt/copy/ ありがとうございました。 (のらじろう) 2015/05/01(金) 09:20 ---- echoコマンドを使う方法もある。 (改行のみのファイルを用意する必要がなくなるが手順は増える。バッチファイルなどでは有効かもしれない) copy 1.txt all.txt echo.>> all.txt copy all.txt + 2.txt echo.>> all.txt copy all.txt + 3.txt echo.>> all.txt copy all.txt + 4.txt バッチファイルにする場合は copy %1 all.txt echo.>> all.txt copy all.txt + %2 echo.>> all.txt copy all.txt + %3 echo.>> all.txt copy all.txt + %4 (ねむねむ) 2015/05/01(金) 10:02 ---- ねむねむさんの案を使って、バッチにするならこんなんでも。 引数の数(結合するファイル)はいくつでも構いません。 @echo off del all.txt 2> NUL for %%f in ( %* ) do type %%f >> all.txt && echo.>>all.txt (Mook) 2015/05/01(金) 10:30 ---- ねむねむさん、ご回答ありがとうございます。 ↓は改行して結合来ました。 >copy 1.txt all.txt >echo.>> all.txt >copy all.txt + 2.txt >echo.>> all.txt >copy all.txt + 3.txt >echo.>> all.txt >copy all.txt + 4.txt 下記のコードでVBAからコマンドラインを起動して実行しました。 Sub test() Dim fol As String Dim txtmei As String Dim dt As String Dim cmd As String Dim cnt As Integer Dim alltxtmei As String alltxtmei = "all.txt" cnt = 0 fol = "G:¥hoge" cmd = "cd/d " & """" & fol & """" txtmei = Dir(fol & "¥*.txt") Do While txtmei <> "" cnt = cnt + 1 If cnt = 1 Then cmd = cmd & " & copy " & """" & txtmei & """" & " " & alltxtmei Else cmd = cmd & " & echo.>> " & alltxtmei & " & copy " & alltxtmei & " + " & """" & txtmei & """" End If txtmei = Dir() Loop Call Shell(Environ$("ComSpec") & " /c" & cmd) End Sub 下記のバッチファイルはall.txtは生成されるのですが、内容は4行の改行のみでした。 (テキストファイルが4つ(1.txt 2.txt 3.txt 4.txt)で試しました) >copy %1 all.txt >echo.>> all.txt >copy all.txt + %2 >echo.>> all.txt >copy all.txt + %3 >echo.>> all.txt >copy all.txt + %4 Mookさん、ご回答ありがとうございます。 >@echo off >del all.txt 2> NUL >for %%f in ( %* ) do type %%f >> all.txt && echo.>>all.txt バッチ化してフォルダの中において実行したのですが、all.txtは生成されませんでした。 (のらじろう) 2015/05/02(土) 00:18 ---- 後半の二つは引数を処理するバッチなので、バッチだけ実行してもなにも処理されませんよ。 結合.bat a.txt b.txt c.txt d.txt e.txt f.txt のように実行したでしょうか。 (あるいはバッチに処理するファイル(a.txt〜f.txt)をドロップする) 結合.bat ?.txt のように1文字のファイルを処理するなどという指定もできます。 *.txt は all.txt が含まれてしまうので、他のフォルダを指定するか、拡張子を変えて おかないとうまくいかないです。 (Mook) 2015/05/02(土) 09:30 ---- Mookさん、再度のご教示ありがとうございます。 >後半の二つは引数を処理するバッチなので、バッチだけ実行してもなにも処理されませんよ。 >あるいはバッチに処理するファイル(a.txt〜f.txt)をドロップする 二つともテキストファイルのドラッグアンドドロップで作動しました。ありがとうございました。 コマンドラインをVBAから起動する方は下記のように生成したall.txtをデスクトップに移動するようにしてみました。 コマンドラインの処理終了を待つのは下記のURLの記述を参考にさせていただきました。 http://www.moug.net/tech/acvba/0010013.html Option Explicit Private Declare Function OpenProcess _ Lib "kernel32" _ (ByVal dwDesiredAccess As Long _ , ByVal bInheritHandle As Long _ , ByVal dwProcessId As Long) As Long Private Declare Function CloseHandle _ Lib "kernel32" _ (ByVal hObject As Long) As Long Private Declare Function GetExitCodeProcess _ Lib "kernel32" _ (ByVal hProcess As Long _ , lpExitCode As Long) As Long Private Const STATUS_PENDING As Long = &H103 Private Const STILL_ACTIVE As Long = STATUS_PENDING Private Const PROCESS_QUERY_INFORMATION As Long = &H400 Sub test() Dim FSO As Object Dim myId As Long Dim myProcess As Long Dim myExitCode As Long Dim fol As String Dim txtmei As String Dim cmd As String Dim cnt As Integer Dim alltxtmei As String Dim alltxtpath As String Dim newtxtpath As String Set FSO = CreateObject("Scripting.FileSystemObject") cnt = 0 fol = "G:¥hoge" alltxtmei = "all.txt" alltxtpath = fol & "¥" & alltxtmei If FSO.fileexists(alltxtpath) Then AppActivate Application.Caption MsgBox alltxtpath & vbCrLf & "は既に存在するファイル名です。" Set FSO = Nothing Exit Sub End If newtxtpath = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "¥" & alltxtmei cmd = "cd/d " & """" & fol & """" txtmei = Dir(fol & "¥*.txt") Do While txtmei <> "" cnt = cnt + 1 If cnt = 1 Then cmd = cmd & " & copy " & """" & txtmei & """" & " " & """" & alltxtmei & """" Else cmd = cmd & " & echo.>> " & """" & alltxtmei & """" & " & copy " & """" & alltxtmei & """" & " + " & """" & txtmei & """" End If txtmei = Dir() Loop myId = Shell(Environ$("ComSpec") & " /c" & cmd) 'プロセスオブジェクトハンドルの取得 myProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 1, myId) '終了コードの監視 Do GetExitCodeProcess myProcess, myExitCode DoEvents Loop While myExitCode = STILL_ACTIVE 'オブジェクトハンドルの解放 CloseHandle myProcess 'メッセージを表示 'MsgBox "終了。", vbInformation If FSO.fileexists(newtxtpath) Then AppActivate Application.Caption MsgBox newtxtpath & vbCrLf & "は既に存在するファイル名です。" Else FSO.MoveFile alltxtpath, newtxtpath End If Set FSO = Nothing End Sub (のらじろう) 2015/05/02(土) 23:04 ...
http://www.excel.studio-kazu.jp/wiki/kazuwiki/201505/20150501080731.txt - [detail] - similar
PREV NEXT
Powered by Hyper Estraier 1.4.13, with 97017 documents and 608140 words.

訪問者:カウンタValid HTML 4.01 Transitional