如何在命令提示符下修剪字符串?

问题描述

我在目录中有很多快捷方式网址。

C:\Users\Owner\Desktop\ReadItLaters>dir *.url /b
aaa.url
bbb.url
ccc.url

...

zzz.url

我想提取那些网址。 所以我写了这样的命令。

for %i in (*.url) do @type "%i" | find "URL=" 

这样输出

URL=https://www.example.com/aaa.html
URL=https://www.example.com/bbb.html
URL=https://www.example.com/ccc.html

...

URL=https://www.example.com/zzz.html

味道很好。但我想获取没有“ URL =”的网址字符串。

我希望这样输出

https://www.example.com/aaa.html
https://www.example.com/bbb.html
https://www.example.com/ccc.html

...

https://www.example.com/zzz.html

如何将“ URL =“替换为空?

解决方法

使用['a1','a2','a3','a4'] ['b1','b2','b3','b4'] ['c1','c2','c3','c4'] 循环处理行:

for /f

或节省一点点(如果URL包含for /f "tokens=2 delims==" %%a in ('type *.url 2^>nul^|find "URL="') do @echo %%a

=

有关详细信息,请参见for /f "tokens=1,* delims==" %%a in ('type *.url 2^>nul^|findstr /b "URL="') do @echo %%b

(注意:这是批处理文件语法。如果要直接在命令行上使用它,请用单个for /?替换每个%%

,

您可以使用子字符串,跳过字符串的前4个字符: %_url:~4%

/

for /f %%i in ('type *.url^|find "URL="')do set "_url=%%~i" && call echo/%_url:~4%

for /f %i in ('type *.url^|find "URL="')do set "_url=%~i" && call echo/%_url:~4%

,

您应该能够获取多个URL文件的信息,而无需嵌套两个for循环。您可以利用FindStr的功能直接搜索多个文件。

@For /F "Tokens=1*Delims==" %%G In ('""%__AppDir__%findstr.exe" /IR "^URL=http" "*.url" 2>NUL"')Do @Echo %%H

For /F "Tokens=1*Delims==" %G In ('""%__AppDir__%findstr.exe" /IR "^URL=http" "*.url" 2>NUL"')Do @Echo %H