Компьютерный форум OSzone.net  

Компьютерный форум OSzone.net (http://forum.oszone.net/index.php)
-   Скриптовые языки администрирования Windows (http://forum.oszone.net/forumdisplay.php?f=102)
-   -   [решено] Заменить ссылки в одном тхт файле, взяв их из другого (http://forum.oszone.net/showthread.php?t=311555)

Alexander_88 14-02-2016 07:58 2605872

Заменить ссылки в одном тхт файле, взяв их из другого
 
Здравствуйте, подскажите, пожалуйста, как можно заменить ссылки в одном тхт файле, на ссылки из другого, ориентируясь по имени файла?

Пример файла 1.txt, в котором нужно заменить ссылки:

Код:

<a href="http://google.com/file/8b3diflsghc0de/0001start.wmv" target="_blank">Download - 92.9 MB</a>
<a href="http://google.com/file/b7dikd67gl343ff1/0002start.wmv" target="_blank">Download - 31.0 MB</a>
<a href="http://google.com/file/addjkd743e09/0003start.wmv" target="_blank">Download - 163.5 MB</a>
<a href="http://google.com/file/2def75kf88d24c/0004start.wmv" target="_blank">Download - 73.3 MB</a>
<a href="http://google.com/file/82bfk548fk431c2c/0005start.wmv" target="_blank">Download - 82.4 MB</a>
<a href="http://google.com/file/03d3fi458fk5139/0006start.wmv" target="_blank">Download - 39.9 MB</a>

Пример файла 2.txt, из которого нужно брать ссылки и заменять по имени файла (0001start.wmv, 0002start.wmv, и т.д.)

Код:

http://yandex.ru/file/dd5dfh55hj458c40/0001start.wmv
http://yandex.ru/file/aahgj5656rhh02228e/0002start.wmv
http://google.com/file/afgh5674dfadfd310/0003start.wmv
http://yandex.ru/file/92fgh6574j1330899/0004start.wmv
http://google.com/file/afgh454h918235/0005start.wmv
http://yandex.ru/file/88efgh46hgf0e0ad6/0006start.wmv
http://google.com/file/f55fh451582/0007start.flv
http://yandex.ru/file/421afgh45h4c5e85/0008start.wmv
http://yandex.ru/file/ea77bfg5453330ce/0009start.wmv
http://google.com/file/2786544ghfa78dd5/0010start.wmv
http://yandex.ru/file/1e47456h5dda2f/0011start.wmv
http://yandex.ru/file/526154g33fdd09d/0012start.wmv
http://yandex.ru/file/fee956rthr45h5b9575/0013start.wmv
http://yandex.ru/file/ad5854h4h54c4a581/0014start.wmv
http://yandex.ru/file/de6bht45hrth807de3/0015start.wmv

Примечания:
Часть, которая должна замениться в файле 1.txt, выделена жирным (т.е. теги все должны остаться).
Необходимо заменять именно по имени файла, сами ссылки разные, домены тоже могут быть разные.
Каждая ссылка всегда начинается с новой строки в обоих тхт файлах.
Количество ссылок в двух тхт файлах всегда разное... после выполнения операции в файле 1.txt должно остаться тоже количество ссылок, только они должны быть заменены ссылками из файла 1.txt.

В примере, после выполнения операции, файл 1.txt должен стать таких:

Код:

<a href="http://yandex.ru/file/dd5dfh55hj458c40/0001start.wmv" target="_blank">Download - 92.9 MB</a>
<a href="http://yandex.ru/file/aahgj5656rhh02228e/0002start.wmv" target="_blank">Download - 31.0 MB</a>
<a href="http://google.com/file/afgh5674dfadfd310/0003start.wmv" target="_blank">Download - 163.5 MB</a>
<a href="http://yandex.ru/file/92fgh6574j1330899/0004start.wmv" target="_blank">Download - 73.3 MB</a>
<a href="http://google.com/file/afgh454h918235/0005start.wmv" target="_blank">Download - 82.4 MB</a>
<a href="http://yandex.ru/file/88efgh46hgf0e0ad6/0006start.wmv" target="_blank">Download - 39.9 MB</a>


Iska 14-02-2016 08:30 2605877

На WSH:
Скрытый текст
Код:

Option Explicit

Const ForWriting = 2

Dim strSourceFile
Dim strPatternFile

Dim strContent
Dim strLine

Dim objDictionary
Dim strKey


strSourceFile =  "C:\Мои проекты\36\1.txt"
strPatternFile = "C:\Мои проекты\36\2.txt"

With WScript.CreateObject("Scripting.FileSystemObject")
        If .FileExists(strSourceFile) Then
                If .FileExists(strPatternFile) Then
                        With .OpenTextFile(strPatternFile)
                                strContent = .ReadAll()
                                .Close
                        End With
                       
                        Set objDictionary = WScript.CreateObject("Scripting.Dictionary")
                       
                        For Each strLine In Split(strContent, vbCrLf)
                                strLine = Trim(strLine)
                               
                                If Len(strLine) > 0 Then
                                        objDictionary.Add .GetFileName(strLine), strLine
                                End If
                        Next
                       
                        With .OpenTextFile(strSourceFile)
                                strContent = .ReadAll()
                                .Close
                        End With
                       
                        With WScript.CreateObject("VBScript.RegExp")
                                .IgnoreCase = True
                                .Global    = True
                               
                                For Each strKey In objDictionary.Keys
                                        .Pattern = "(<a href="")(http://.*?/" & strKey & ")("".*?>.*?</a>)"
                                       
                                        If .Test(strContent) Then
                                                strContent = .Replace(strContent, "$1" & objDictionary.Item(strKey) & "$3")
                                        End If
                                Next
                        End With
                       
                        .CopyFile strSourceFile, .BuildPath(.GetParentFolderName(strSourceFile), .GetBaseName(strSourceFile) & ".bak"), True
                       
                        With .OpenTextFile(strSourceFile, ForWriting)
                                .Write strContent
                                .Close
                        End With
                       
                        objDictionary.RemoveAll
                        Set objDictionary = Nothing
                Else
                        WScript.Echo "Can't find pattern file [" & strPatternFile & "]."
                        WScript.Quit 2
                End If
        Else
                WScript.Echo "Can't find source file [" & strSourceFile & "]."
                WScript.Quit 1
        End If
End With

WScript.Quit 0


Alexander_88 14-02-2016 09:25 2605887

Спасибо, все работает :)


Время: 14:02.

Время: 14:02.
© OSzone.net 2001-