排序网址的批处理文件

时间:2017-10-24 05:52:54

标签: sorting batch-file url

我需要创建批处理文件,如下例所示:

▬Google.com▬

▬Yahoo.com▬

▬Bing.com▬

此外,我在link.txt

中有更多关于此网址的链接
http://drive.google.com
http://gallery.bing.com
http://mail.yahoo.com
http://video.google.com
http://brb.yahoo.com
http:/map.bing.com

我希望After Run Batch文件在下面的标题中显示如下链接:

▬ Google.com ▬
http://drive.google.com
http://video.google.com

▬ Yahoo.com ▬
http://mail.yahoo.com
http://brb.yahoo.com

▬ Bing.com ▬
http://gallery.bing.com
http:/map.bing.com

2 个答案:

答案 0 :(得分:1)

以下示例脚本怎么样?

流程:

  1. 从“link.txt”中检索包含google.com,yahoo.com和bing.com的行,并将其导入数组。
    • if not "!val:google.com=!" == "!val!"表示当一行(val)包含google.com时,会运行设置命令。
  2. 显示每个数组中的元素。
  3. 示例脚本:

    @echo off
    setlocal enabledelayedexpansion
    
    set g=0
    set y=0
    set b=0
    for /f "delims=" %%a in (link.txt) do (
        set val=%%a
        if not "!val:google.com=!" == "!val!" (
            set google[!g!]=!val!
            set /a g+=1
        )
        if not "!val:yahoo.com=!" == "!val!" (
            set yahoo[!y!]=!val!
            set /a y+=1
        )
        if not "!val:bing.com=!" == "!val!" (
            set bing[!b!]=!val!
            set /a b+=1
        )
    )
    set /a g-=1
    set /a y-=1
    set /a b-=1
    
    echo - Google.com -
    for /L %%i in (0,1,!g!) do echo !google[%%i]!
    echo.
    echo - Yahoo.com -
    for /L %%i in (0,1,!y!) do echo !yahoo[%%i]!
    echo.
    echo - Bing.com -
    for /L %%i in (0,1,!b!) do echo !bing[%%i]!
    

    结果:

    - Google.com -
    http://drive.google.com
    http://video.google.com
    
    - Yahoo.com -
    http://mail.yahoo.com
    http://brb.yahoo.com
    
    - Bing.com -
    http://gallery.bing.com
    http:/map.bing.com
    

    注意:

    1. 此示例脚本可用于包含google.comyahoo.combing.com的网址的情况。因此,如果网址为http://google.com.yahoo.com,则结果会同时显示在google.comyahoo.com
    2. 按照link.txt。
    3. 的顺序,显示每个结果的顺序

      如果我误解了你的问题,我很抱歉。

      编辑:

      如果要使用批处理文件将结果保存为文本数据,请按如下方式修改。 完成此修改后,结果将覆盖link.txt。所以请小心。如果您想将结果添加到link.txt,请从echo - Google.com - > %outputfile%更改为echo - Google.com - >> %outputfile%

      来自:

      echo - Google.com -
      for /L %%i in (0,1,!g!) do echo !google[%%i]!
      echo.
      echo - Yahoo.com -
      for /L %%i in (0,1,!y!) do echo !yahoo[%%i]!
      echo.
      echo - Bing.com -
      for /L %%i in (0,1,!b!) do echo !bing[%%i]!
      

      致:

      set outputfile=link.txt
      echo - Google.com - > %outputfile%
      for /L %%i in (0,1,!g!) do echo !google[%%i]! >> %outputfile%
      echo. >> %outputfile%
      echo - Yahoo.com - >> %outputfile%
      for /L %%i in (0,1,!y!) do echo !yahoo[%%i]! >> %outputfile%
      echo. >> %outputfile%
      echo - Bing.com - >> %outputfile%
      for /L %%i in (0,1,!b!) do echo !bing[%%i]! >> %outputfile%
      

答案 1 :(得分:1)

这个更简单的批处理文件不需要显式写入目标站点;它适用于放置在文件中的任何网址,其格式与示例数据相同

@echo off
setlocal EnableDelayedExpansion

rem Sort and store urls
for /F "tokens=1* delims=." %%a in (link.txt) do (
   set "name[%%b]=!name[%%b]! %%a.%%b"
)

rem Output sorted urls
(for /F "tokens=2* delims=[]=" %%a in ('set name[') do (
   echo - %%a -
   for %%c in (%%b) do echo %%c
   echo/
)) > link.txt

结果:

- bing.com -
http://gallery.bing.com
http:/map.bing.com

- google.com -
http://drive.google.com
http://video.google.com

- yahoo.com -
http://mail.yahoo.com
http://brb.yahoo.com