バッチファイルやシェルコマンドの実行結果は、標準出力(C言語でいうとstdout)だけでなく、ちゃんと標準エラー(同様にstderr)もリダイレクトすることができます。
つまり、hoge.txtが存在するとき、
1 |
dir /B hoge.txt > result.txt |
とすれば、問題ありませんが、hoge.txtが存在しない場合は、result.txtには何もリダイレクトされません。
そこで、
1 |
dir /B hoge.txt > result.txt 2>&1 |
とすることで、hoge.txtが存在しない場合、result.txtには、
「ファイルが見つかりません」
とリダイレクトされることがわかります。DOSもそこそこUNIXライクに設計&実装されているようです。
No comments yet. You should be kind and add one!
By submitting a comment you grant typista a perpetual license to reproduce your words and name/web site in attribution. Inappropriate and irrelevant comments will be removed at an admin’s discretion. Your email is used for verification purposes only, it will never be shared.