Total Commander Forum Index Total Commander
Форум поддержки пользователей Total Commander
Сайты: Все о Total Commander | Totalcmd.net | Ghisler.com | RU.TCKB
 
 RulesRules   SearchSearch   FAQFAQ   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Single Post  Topic: Авторедактирование списка url-ов на основе HTTP статус кодов 
Author Message
MVV



PostPosted: Fri Jan 27, 2017 01:57    Post subject: Reply with quote

Может, MVV устал и ушел спать? Rolling Eyes

С асинхронностью будет как-то так:
Code:
@echo off
set "ARG_0=%~dpnx0"
powershell.exe -NoProfile -STA "iex (([IO.File]::ReadAllText($Env:ARG_0)) -replace '^(.*\n)*?.*<::::>.*\n', '')" & goto :EOF


Add-Type -Assembly System.Windows.Forms;


function testUrls($urls, $timeout, $workerCount = 2) {
   $job = @{
      queue = [Collections.Queue]::Synchronized((New-Object Collections.Queue (, $urls)));
      list = [Collections.ArrayList]::Synchronized((New-Object Collections.ArrayList));
      timeout = $timeout;
   };
   Write-Host "Checking $($urls.Length) urls using $workerCount workers...";
   $workerScript = {
      $job = $args[0];
      try {
         while (1) {
            $url = $job.queue.Dequeue();
            try {
               $webReq = [Net.WebRequest]::Create($url);
               $webReq.Method = 'HEAD';
               $webReq.Timeout = $job.timeout;
               $webRsp = $webReq.GetResponse();
               $webRsp.Close();
               $null = $job.list.Add($url);
               "$url`: $($webRsp.StatusCode)";
            }
            catch {
               "$url`: $_";
            }
         }
      }
      catch {}
   };
   $workers = @((1 .. $workerCount) | % { $w = [PowerShell]::Create().AddScript($workerScript).AddArgument($job); @{ w = $w; r = $w.BeginInvoke() } });
   $workers | % { Write-Host ($_.w.EndInvoke($_.r) -join "`n") };
   $job.list;
}

$timeout = 10000;
$workerCount = 10;
$urls = @([Windows.Forms.Clipboard]::GetText() -split '\r?\n' | ? { $_ -match '^\w+://' });
$startTime = [DateTime]::Now;
$goodUrls = @(testUrls $urls $timeout $workerCount);
$workTime = [int]([DateTime]::Now - $startTime).TotalSeconds;
if ([Windows.Forms.MessageBox]::Show("Approved $($goodUrls.Length) of $($urls.Length) urls in $workTime seconds.`nPlease click OK to copy urls to clipboard.", 'Url Filter', 1) -eq 1) {
   [Windows.Forms.Clipboard]::SetText(($goodUrls -join "`n") + "`n");
}


Таймаут и число потоков можно поменять, но если скрипт исчерпает лимит полуоткрытых соединений, могут быть проблемы.
_________________
TCFS2 + TCFS2Tools: Полноэкранный режим и многое другое (обсуждение)
WINCMD.RU: AskParam, CopyTree, NTLinks, Sudo, VirtualPanel…
View user's profile Send private message ICQ Number


Powered by phpBB © 2001, 2005 phpBB Group