在 CMD 中,连接行并添加空格来选择字符串并不总是有效

在 CMD 中,连接行并添加空格来选择字符串并不总是有效

这是一个普通的 .bat,它连接所有行并在结果字符串之间留下空格。

@echo off
setlocal EnableDelayedExpansion
set row=
@For /F "EOL=|" %%x In ('^""%__AppDir__%find.exe" "."^<"%userprofile%\i.txt"^"') Do @Set "row=!row! %%x"
echo %row% >%userprofile%\o.txt
echo %row%

这是该脚本的测试输入:

PDF

wikkipedia.ord
notavalidURL
snapfish.com
.tunnelbear.com
mail.google.com/mail/u/0/#inbox

它的输出非常可预测:

wikkipedia.ord snapfish.com .tunnelbear.com mail.google.com/mail/u/0/#inbox!

但这里有另一个输入,其行为方式不同:

PDF
Tech
TORONTO
>
SOME PERSON
Some Company
BusinessName

User Name
Users
Default User
Default Filter Profile
Web Exceptions
BackWeb Exceptions
Applied URL Lists
Attach List
Individual Exceptions
Applied individually to User and can be modified

Individual List32 URLs
BBC Weather

Shared ListTechloq7 URLs
Bing Search
Allows "bing" search

Shared ListTechloq45 URLs
Business Essential Sites
pre-approved business sites, (from reference, Technology, File sharing, shopping etc).

Shared ListTechloq167 URLs
Email [Gmail]
Allow Gmail Yahoo and AOL mail.

Shared ListTechloq77 URLs
Google Drive

Shared ListTechloq35 URLs
Google hangouts

Shared ListTechloq8 URLs
Google Maps

Shared ListTechloq33 URLs
Google Photos

Shared ListTechloq5 URLs
Jewish Music

Shared ListTechloq89 URLs
Jewish Websites
Allows all jewish websites.

Shared ListTechloq1183 URLs
Microsoft [and google] Office blacklist
Blocks in app browsing for Microsoft apps [office 365, start menu etc.] and for google docs.

Shared ListTechloq18 URLs
Microsoft Classroom

Shared ListTechloq25 URLs
Microsoft maps app
Allows the Microsoft map app

Shared ListTechloq1 URLs
Microsoft Online

Shared ListTechloq27 URLs
Microsoft Teams

Shared ListTechloq28 URLs
Mostlymusic.com

Shared ListTechloq17 URLs
Office Activate
Activate Microsoft office

Shared ListTechloq7 URLs
Religious Blacklist
Blocks ëôéøä websites.

Shared ListTechloq23 URLs
Techloq Global List
For back end and content servers [recaptcha etc.]

Shared ListTechloq261 URLs
Torah Websites

Shared ListTechloq115 URLs
Windows Store blacklist

Shared ListTechloq12 URLs
Windows Update
You might need to add .microsoft.com to the ssl list.

Shared ListTechloq16 URLs
Yahoo Search
Allows Yahoo search

Shared ListTechloq42 URLs
Individual Exceptions
Applied individually to User and can be modified

Individual ListLast changed Dec 26, 2019 09:35 AM
URLs Included
Access  
Offensive Text Filter
Images Filter
Video Blocking
Type URL and press Enter
On
On
On
wikkipedia.ord
Time left 13d 34m
Go to Temporary Access
youtubedownload.video
On
On
Off
.slack.com
Off
Off
Off
slack.com
Off
Off
Off
snapfish.com
On
Off
On
.tunnelbear.com
On
On
Off
mail.google.com/mail/u/0/#inbox
On
Off
Off
google.com/url?q=https://connect.cloudberrylab.com?i%3D448781782&source=gmail&ust=1573146628917000&usg=AFQjCNF14Y-dux0_bNSOc-jOfjLgHy-3fw
On
On
On
connect.cloudberrylab.com?i=448781782
On
Off
Off
golantelecom.co.il
On
On
Off
msn.com
citibank.com
On
On
Off
youtube.com/watch?v=TsBcex3M2Xo&list=RDTsBcex3M2Xo&start_radio=1&t=0
On
Off
Off
youtube.com/watch?v=TsBcex3M2Xo&list=PL8CN8XjdSyu0TdNKo9U9sF1RL7NB3_0mh
On
Off
Off
ontimestudios.com
On
Off
Off
ontimerovideo.com
On
Off
Off
badchen.com
On
Off
Off
ebay.com/
On
On
On
offshorecompany.com
On
On
On
offshorecompany.com/company/israel-llc/
On
On
On
wikipedia.org
On
On
On
ravkavonline.co.il
On
On
On
tunnelbear.com
On
On
On
citiprivatepass.com
On
On
On
docs.google.com
On
On
On
play.google.com
On
On
On
myjewishcolorado.org
On
On
On
hillelacademyofdenver.com/
On
On
On
wikkipedia.com
On
On
On
productforums.google.com
On
On
On
ultrasurf.us
On
On
On
stackoverflow.com
On
On
On
tutorialpoint.com
On
On
On
Dec 29, 2019 12:23 AM

它输出一些相当烦人的东西,例如:

 pre-approved business sites, (from reference, Technology, File sharing, shopping etc). Allow Gmail Yahoo and AOL mail. Allows all jewish websites. Blocks in app browsing for Microsoft apps [office 365, start menu etc.] and for google docs. Mostlymusic.com Blocks δ⌠Θ°Σ websites. For back end and content servers [recaptcha etc.] You might need to add .microsoft.com to the ssl list. wikkipedia.ord youtubedownload.video .slack.com slack.com snapfish.com .tunnelbear.com mail.google.com/mail/u/0/#inbox google.com/url?q=https://connect.cloudberrylab.com?i%3D448781782
'source' is not recognized as an internal or external command,
operable program or batch file.
'ust' is not recognized as an internal or external command,
operable program or batch file.
'usg' is not recognized as an internal or external command,
operable program or batch file.
'list' is not recognized as an internal or external command,
operable program or batch file.
'start_radio' is not recognized as an internal or external command,
operable program or batch file.
'1' is not recognized as an internal or external command,
operable program or batch file.
'list' is not recognized as an internal or external command,
operable program or batch file.

为什么这个字符串不同,我怎样才能改变我的.bat 以获得与第一次尝试相同类型的输出?

以下是似乎与错误相关的刺痛:

'source' is not recognized as an internal or external command
operable program or batch file.
google.com/url?q=https://connect.cloudberrylab.com?i%3D448781782&source=gmail&ust=1573146628917000&usg=AFQjCNF14Y-dux0_bNSOc-jOfjLgHy-3fw

'ust' is not recognized as an internal or external command
operable program or batch file.
google.com/url?q=https://connect.cloudberrylab.com?i%3D448781782&source=gmail&ust=1573146628917000&usg=AFQjCNF14Y-dux0_bNSOc-jOfjLgHy-3fw
'usg' is not recognized as an internal or external command
operable program or batch file.
google.com/url?q=https://connect.cloudberrylab.com?i%3D448781782&source=gmail&ust=1573146628917000&usg=AFQjCNF14Y-dux0_bNSOc-jOfjLgHy-3fw
'list' is not recognized as an internal or external command,
operable program or batch file.
youtube.com/watch?v=TsBcex3M2Xo&list=RDTsBcex3M2Xo&start_radio=1&t=0
'start_radio' is not recognized as an internal or external command,
operable program or batch file.

youtube.com/watch?v=TsBcex3M2Xo&list=RDTsBcex3M2Xo&start_radio=1&t=0
'1' is not recognized as an internal or external command,
operable program or batch file.

youtube.com/watch?v=TsBcex3M2Xo&list=RDTsBcex3M2Xo&start_radio=1&t=0
'list' is not recognized as an internal or external command,
operable program or batch file.
youtube.com/watch?v=TsBcex3M2Xo&list=PL8CN8XjdSyu0TdNKo9U9sF1RL7NB3_0mh

查看输出,我可以看到所有引发错误的单词都位于一个&字符旁边(如果这对任何人来说都有意义的话)。

更令人沮丧的是,我专门编写了此代码来处理来自某个网​​站的文本。每次我将该网站的页面复制到记事本并运行我的代码时,都会发生同样的事情,所以在我解决这个问题之前我什么也做不了 在此处输入图片描述

我试图避免使用任何非 CMD 原生的代码(例如 powershell、cygwin、paste.exe)

答案1


  • 您忘记了 bat/cmd 作业中的字符串操作中的重要内容......

    您需要对特殊字符使用转义..., &, ?, >, etc,...

  • 您的文件出现多次


您可以使用set /p "'="在文件中获取此输出而不必转义特殊字符,因为此命令使用引号:set /p"'=!row!"


回显%row%>%userprofile%\o.txt
|| 而是:||
设置/p“'=!row!”>“%userprofile%\o.txt”

  • 选项1-find.exe "." == 搜索 . (文字点)


  • 选项 #2-findstr.exe . == 搜索任意(正则表达式任意字符)



  • 如果你需要选项 1

@echo off && setlocal enabledelayedexpansion

cls & title ..\%~nx0 && pushd %userprofile%
set "_row="<nul && for /f eol^=^| %%x in ('
^""%__appdir__%findstr.exe" . ^<".\i.txt"^"
')do set "_row=!_row! %%x"

>"%userprofile%\o.txt" set/p "'=!_row!"<nul
echo[!_row! && podp && endlocal && ;exit /b 

  • 选项 #1 的结果
pre-approved Allow Allows Blocks Mostlymusic.com Blocks For You wikkipedia.ord youtubedownload.video .slack.com slack.com snapfish.com .tunnelbear.com mail.google.com/mail/u/0/#inbox google.com/url?q=https://connect.cloudberrylab.com?i%3D448781782&source=gmail&ust=1573146628917000&usg=AFQjCNF14Y-dux0_bNSOc-jOfjLgHy-3fw connect.cloudberrylab.com?i=448781782 golantelecom.co.il msn.com citibank.com youtube.com/watch?v=TsBcex3M2Xo&list=RDTsBcex3M2Xo&start_radio=1&t=0 youtube.com/watch?v=TsBcex3M2Xo&list=PL8CN8XjdSyu0TdNKo9U9sF1RL7NB3_0mh ontimestudios.com ontimerovideo.com badchen.com ebay.com/ offshorecompany.com offshorecompany.com/company/israel-llc/ wikipedia.org ravkavonline.co.il tunnelbear.com citiprivatepass.com docs.google.com play.google.com myjewishcolorado.org hillelacademyofdenver.com/ wikkipedia.com productforums.google.com ultrasurf.us stackoverflow.com tutorialpoint.com


  • 如果你需要选项 #2
@echo off && setlocal enabledelayedexpansion

cls & title ..\%~nx0 && pushd %userprofile%
set "_row="<nul && for /f eol^=^| %%x in ('
^""%__appdir__%findstr.exe" . ^<".\i.txt"^"
')do set "_row=!_row! %%x"

>"%userprofile%\o.txt" set/p "'=!_row!"<nul
echo[!_row! && podp && endlocal && ;exit /b

  • 选项 #2 的结果
PDF TechLog TAG > SOME Some BusinessName User Users Default Default Web BackWeb Applied Attach Individual Applied Individual BBC Shared Bing Allows Shared Business pre-approved Shared Email Allow Shared Google Shared Google Shared Google Shared Google Shared Jewish Shared Jewish Allows Shared Microsoft Blocks Shared Microsoft Shared Microsoft Allows Shared Microsoft Shared Microsoft Shared Mostlymusic.com Shared Office Activate Shared Religious Blocks Shared Techloq For Shared Torah Shared Windows Shared Windows You Shared Yahoo Allows Shared Individual Applied Individual URLs Access Offensive Images Video Type On On On wikkipedia.ord Time Go youtubedownload.video On On Off .slack.com Off Off Off slack.com Off Off Off snapfish.com On Off On .tunnelbear.com On On Off mail.google.com/mail/u/0/#inbox On Off Off google.com/url?q=https://connect.cloudberrylab.com?i%3D448781782&source=gmail&ust=1573146628917000&usg=AFQjCNF14Y-dux0_bNSOc-jOfjLgHy-3fw On On On connect.cloudberrylab.com?i=448781782 On Off Off golantelecom.co.il On On Off msn.com citibank.com On On Off youtube.com/watch?v=TsBcex3M2Xo&list=RDTsBcex3M2Xo&start_radio=1&t=0 On Off Off youtube.com/watch?v=TsBcex3M2Xo&list=PL8CN8XjdSyu0TdNKo9U9sF1RL7NB3_0mh On Off Off ontimestudios.com On Off Off ontimerovideo.com On Off Off badchen.com On Off Off ebay.com/ On On On offshorecompany.com On On On offshorecompany.com/company/israel-llc/ On On On wikipedia.org On On On ravkavonline.co.il On On On tunnelbear.com On On On citiprivatepass.com On On On docs.google.com On On On play.google.com On On On myjewishcolorado.org On On On hillelacademyofdenver.com/ On On On wikkipedia.com On On On productforums.google.com On On On ultrasurf.us On On On stackoverflow.com On On On tutorialpoint.com On On On Dec


  • 文件“.\i.txt”内容
PDF
TechLog
TAG TORONTO
>
SOME PERSON
Some Company
BusinessName

User Name
Users
Default User
Default Filter Profile
Web Exceptions
BackWeb Exceptions
Applied URL Lists
Attach List
Individual Exceptions
Applied individually to User and can be modified

Individual List32 URLs
BBC Weather

Shared ListTechloq7 URLs
Bing Search
Allows "bing" search

Shared ListTechloq45 URLs
Business Essential Sites
pre-approved business sites, (from reference, Technology, File sharing, shopping etc).

Shared ListTechloq167 URLs
Email [Gmail]
Allow Gmail Yahoo and AOL mail.

Shared ListTechloq77 URLs
Google Drive

Shared ListTechloq35 URLs
Google hangouts

Shared ListTechloq8 URLs
Google Maps

Shared ListTechloq33 URLs
Google Photos

Shared ListTechloq5 URLs
Jewish Music

Shared ListTechloq89 URLs
Jewish Websites
Allows all jewish websites.

Shared ListTechloq1183 URLs
Microsoft [and google] Office blacklist
Blocks in app browsing for Microsoft apps [office 365, start menu etc.] and for google docs.

Shared ListTechloq18 URLs
Microsoft Classroom

Shared ListTechloq25 URLs
Microsoft maps app
Allows the Microsoft map app

Shared ListTechloq1 URLs
Microsoft Online

Shared ListTechloq27 URLs
Microsoft Teams

Shared ListTechloq28 URLs
Mostlymusic.com

Shared ListTechloq17 URLs
Office Activate
Activate Microsoft office

Shared ListTechloq7 URLs
Religious Blacklist
Blocks ëôéøä websites.

Shared ListTechloq23 URLs
Techloq Global List
For back end and content servers [recaptcha etc.]

Shared ListTechloq261 URLs
Torah Websites

Shared ListTechloq115 URLs
Windows Store blacklist

Shared ListTechloq12 URLs
Windows Update
You might need to add .microsoft.com to the ssl list.

Shared ListTechloq16 URLs
Yahoo Search
Allows Yahoo search

Shared ListTechloq42 URLs
Individual Exceptions
Applied individually to User and can be modified

Individual ListLast changed Dec 26, 2019 09:35 AM
URLs Included
Access  
Offensive Text Filter
Images Filter
Video Blocking
Type URL and press Enter
On
On
On
wikkipedia.ord
Time left 13d 34m
Go to Temporary Access
youtubedownload.video
On
On
Off
.slack.com
Off
Off
Off
slack.com
Off
Off
Off
snapfish.com
On
Off
On
.tunnelbear.com
On
On
Off
mail.google.com/mail/u/0/#inbox
On
Off
Off
google.com/url?q=https://connect.cloudberrylab.com?i%3D448781782&source=gmail&ust=1573146628917000&usg=AFQjCNF14Y-dux0_bNSOc-jOfjLgHy-3fw
On
On
On
connect.cloudberrylab.com?i=448781782
On
Off
Off
golantelecom.co.il
On
On
Off
msn.com
citibank.com
On
On
Off
youtube.com/watch?v=TsBcex3M2Xo&list=RDTsBcex3M2Xo&start_radio=1&t=0
On
Off
Off
youtube.com/watch?v=TsBcex3M2Xo&list=PL8CN8XjdSyu0TdNKo9U9sF1RL7NB3_0mh
On
Off
Off
ontimestudios.com
On
Off
Off
ontimerovideo.com
On
Off
Off
badchen.com
On
Off
Off
ebay.com/
On
On
On
offshorecompany.com
On
On
On
offshorecompany.com/company/israel-llc/
On
On
On
wikipedia.org
On
On
On
ravkavonline.co.il
On
On
On
tunnelbear.com
On
On
On
citiprivatepass.com
On
On
On
docs.google.com
On
On
On
play.google.com
On
On
On
myjewishcolorado.org
On
On
On
hillelacademyofdenver.com/
On
On
On
wikkipedia.com
On
On
On
productforums.google.com
On
On
On
ultrasurf.us
On
On
On
stackoverflow.com
On
On
On
tutorialpoint.com
On
On
On
Dec 29, 2019 12:23 AM

抱歉我的水平有限

相关内容