■このサイトはPowerShell(MSH/Monad)奮闘記です
管理人「newpops(吉岡洋)」が「PowerShell(旧名:MSH/Monad)」の研究結果を日々綴っていきます。
【お知らせ】
この日記からPowerShellのTipsを抽出し「PowerShell FAQ」として整理しました。
2005-12-26
■[MSH]ファイルの読み込み処理
ファイルを行単位で処理する
「get-contents」はファイル内容を取得するCmdletです。
「get-contents」をパイプ経由でforeachに渡すと、ファイル内容を行単位で処理できます。
テキストファイルに書かれたURLを読み込んでファイルをダウンロードする
URLを書いたテキストファイル(URL.txt)
MSH C:\> get-content .\URL.txt http://www.google.co.jp/intl/ja_jp/images/logo.gif http://rss.rssad.jp/rss/itm/rss.xml
URLを読み込んでダウンロードする
MSH C:\> $webClient = new-object System.Net.WebClient
MSH C:\> $saveDir = "C:\Download\"
MSH C:\> get-content .\URL.txt | foreach{
>> $fileName = $_.SubString($_.LastIndexOf("/") + 1);
>> $webClient.DownloadFile($_, $saveDir + $fileName);
>> }
>>
ダウンロードしたファイルを確認する
MSH C:\> ls C:\Download
Directory: FileSystem::C:\Download
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2005/12/27 1:49 8754 logo.gif
-a--- 2005/12/27 1:49 13093 rss.xml



