qimuxiaokai
 1 try{
 2 $source=$args[0]
 3 #"http://f1.xiami.net/23642/478839/03_1770634359_2878780.mp3";
 4 $nameindex=$source.LastIndexOf(\'/\')+1;
 5 $length=$source.Length
 6 
 7 $filename=$source.SubString($nameindex,$length-$nameindex);
 8 
 9 $destination="D:\temp\";
10 $destinationfile=$destination+$filename;
11 $filelist="D:\temp\playlist.txt";
12 set-location $destination
13 $files=get-childitem | where-object {$_.Name -eq $filename}
14 $isExist=!($files -eq $null);
15 
16 if(!$isExist){
17     $client=New-Object System.Net.WebClient;
18     $client.DownloadFile($source,$destinationfile);
19     [datetime]::Now.DateTime + "-----"+ $filename | out-file $filelist -append
20 }
21 
22 }
23 catch
24 {
25     write-host $Error[0]
26 }

在Powershell中运行以上脚本

ps1> Set-Location "D:\temp"
ps1> .\downloadFile.ps1 "http://f1.xiami.net/23642/478839/03_1770634359_2878780.mp3"

脚本中用到的知识点如下。

1。接收外界参数 $args. 这是个数组。

2。powershell 中string 函数跟C#中基本类似,使用方法也基本相同。

3。powershell中参数表达方式:$argument

4。 管道(Pipeline)。将上一个cmdlet的结果传给下一条cmdlet,用 | 隔开两个cmdlet。例如:$files=get-childitem | where-object {$_.Name -eq $filename}

5。new-object 用于生成.NET 和 COM 对象。

6。$NULL, -eq, -gt

分类:

技术点:

相关文章: