2017-05-25 40 views
0

我有一个简单的myscript.ps1从文件中提取的网址,从this tutorial采取:PowerShell和选择串访问文件 - 访问被拒绝

$input_path = 'd:\myfolder\*' 
$output_file = 'd:\extracted_URL_addresses.txt' 
$regex = '([a-zA-Z]{3,})://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)*?' 
select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file 

我运行PowerShell中以管理员身份,然后键入:

D:/myscript.ps1 

但对于大多数路径内d:\myfolder我得到:

select-string : The file D:\myfolder\templates cannot be read: Access to the path 'D:\myfolder\templates' is denied. 

使用WinSCP从FTP服务器复制文件夹。我试图去文件夹属性和勾选“只读”框比应用,但每次我重新输入属性它是“只读”(我不知道如果这是有关的问题)。

我在Windows 10

+2

看起来像'd:\ MyFolder文件\ templates'是一个文件夹不是一个文件选择字符串可以工作。 – LotPings

+0

您是否可以浏览以查看D:\ myfolder \ templates中的文件,并且如果您看到文件,您是否可以打开它们?这听起来像是一个ACL问题。 – TheMadTechnician

+0

@TheMadTechnician是的,我可以打开和浏览这些文件夹没有任何问题。 – PolGraphic

回答

0

工作要在意见扩大从@LotPings你可以通过使用-File参数从Get-ChildItem得到的只是在D:\myfolder的文件。这样你就不会将目录传到Select-String

$input_path = 'd:\myfolder' 
$Files = Get-ChildItem $input_path -File | Select-Object -ExpandProperty FullName 
Foreach ($File in $Files) { 
    $output_file = 'd:\extracted_URL_addresses.txt' 
    $regex = '([a-zA-Z]{3,})://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)*?' 
    select-string -Path $file -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file 
} 
0
  • 作为$inputautomatic variable我不会用它 - 甚至还不如一个变量名的一部分。
  • 你不需要两个堆叠ForEach-Object使用$_.Matches.Values代替
  • 使用的路径下的文件扩展名可能最终避免错误
  • 在这个网页的副本使用folllowing脚本的作品完美,但有不少受骗者的,所以我会追加一个|Sort-Object -Unique

$FilePath = '.\*.html' 
$OutputFile = '.\extracted_URL_addresses.txt' 
$regex = '([a-zA-Z]{3,})://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)*?' 
Get-ChildItem -File $FilePath | 
    Select-String -Pattern $regex -AllMatches | 
    ForEach-Object { $_.Matches.Value } |Sort -Unique > $OutputFile