batch variable exclamation points used in dir | findstr

不想你离开。 提交于 2020-03-25 10:17:09

问题


I'm trying to find files that do not match (at the beginning of the filename) predefined formats contained in a .txt file.

I have the following:

@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).

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 (!).

Thx in advance.


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.


回答1:


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.

For example:

@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.



来源:https://stackoverflow.com/questions/60286676/batch-variable-exclamation-points-used-in-dir-findstr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!