dir中使用的批处理变量感叹号|findstr [英] batch variable exclamation points used in dir | findstr
问题描述
我正在尝试查找与.txt文件中包含的预定义格式不匹配(在文件名开头)的文件.
I'm trying to find files that do not match (at the beginning of the filename) predefined formats contained in a .txt file.
我有以下内容:
@Echo off
chcp 1254>nul
setlocal DisableDelayedExpansion
for /f "usebackq tokens=1,2,3* delims=~" %%f in ("%USERPROFILE%\Desktop\xref.txt") do (
set "DIRNAME=%%f"
set "DIRNAM2=^%%f"
set "PATHNAM=%%h"
set "ALBUMNM=%%g"
SETLOCAL EnableDelayedExpansion
IF EXIST !PATHNAM!!DIRNAME! (
PushD !PATHNAM!!DIRNAME!
dir /b /a-d "*" | findstr /v /r /c:"!DIRNAM2! -*"
)
ENDLOCAL
)
pause
EXIT /b
这很好用,除非文件名中包含刘海(感叹号).
This works great except with filenames containing bangs (exclamation points).
这是我的.txt文件(子目录〜相册名称〜路径)的示例,该文件是通过脚本生成的:
Here's a sampling of my .txt file (subdirectory~album name~path) which gets generated by a script:
12 Byzantine Rulers. The History of The Byzantine Empire~12 Byzantine Rulers. The History of The Byzantine Empire~g:\test\
17th Century Poetry~17th Century Poetry~g:\test\
1984 (George Orwell)~1984 (George Orwell)~g:\test\
1_2_1~1_2_1~g:\test\
21st Century American Foreign Policy~21st Century American Foreign Policy~g:\test\
99% Invisible~99% Invisible~g:\test\
Communication Matters. That’s Not What I Meant!~Communication Matters. That’s Not What I Meant!~g:\test\
有数百个目录,其中包含数百个文件(播客).我想修复此批次,以便它也可以处理刘海(!).
There are hundreds of directories containing hundreds of files (podcasts). I'd like to fix this batch so it can also handle bangs (!).
提前谢谢.
编辑.我的测试数据不够强大.findstr命令也不能(至少)与以下字符一起使用:é'»¿...也就是说,PushD将我带到了正确的目录,但是FindStr并未按预期进行剔除.
Edit. My test data wasn't robust enough. The findstr command also doesn't work with (at least) the following characters: é’»¿ ... that is to say PushD gets me to the right directory, but FindStr doesn't do it's culling as expected.
推荐答案
我认为问题不一定与代码页或编码相关,而感叹号(bangs)则更少.我看到的主要问题是您的文本文件内容使用智能引号(卷曲),而不是哑引号(直).此外,您有%
个字符,批处理文件中的这些字符通常需要加倍.出于这些原因,我首先建议您尝试替换这些字符.
I don't think that the issue is necessarily code page or encoding related, and less so exclamation marks, (bangs). The major issue I see is that your text file content uses smart quotes, (curly), instead of dumb quotes, (straight). Additionally you have %
characters which in batch files usually require doubling. For those reasons I would first suggest that you try to replace those characters.
例如:
@Echo Off
SetLocal DisableDelayedExpansion
For /F "UseBackQ Tokens=1-3 Delims=~" %%G In ("%USERPROFILE%\Desktop\xref.txt")Do (
Set "SUBDIRN=%%G"
Set "ALBUMNM=%%H"
Set "PATHNAM=%%I"
SetLocal EnableDelayedExpansion
Set SUBDIRN=!SUBDIRN:%%=%%%%!
Set ALBUMNM=!ALBUMNM:%%=%%%%!
Set PATHNAM=!PATHNAM:%%=%%%%!
Set SUBDIRN=!SUBDIRN:’='!
Set ALBUMNM=!ALBUMNM:’='!
Set PATHNAM=!PATHNAM:’='!
Set SUBDIRN=!SUBDIRN:"="!
Set ALBUMNM=!ALBUMNM:"="!
Set PATHNAM=!PATHNAM:"="!
Set SUBDIRN=!SUBDIRN:"="!
Set ALBUMNM=!ALBUMNM:"="!
Set PATHNAM=!PATHNAM:"="!
If Exist "!PATHNAM!!SUBDIRN!\" (
PushD "!PATHNAM!!SUBDIRN!"
Dir /B/A-D|FindStr /IVRC:"^!SUBDIRN! -"
)
EndLocal
)
Pause
Exit /B
我不确定代码框中的这段代码如何处理智能引号,但是我确定您会明白的.
I'm not sure how a copy of this code, within the code box will handle the smart quotes, but I'm sure you'll get the idea.
这篇关于dir中使用的批处理变量感叹号|findstr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!