简体   繁体   English

DIR命令的输出与批处理文件中的预期不符

[英]Output of DIR command not as expected in batch file

I have a large collection of television series sitting on a NAS box, that shares media via samba and dlna throughout my home. 我坐在NAS盒上有很多电视连续剧,它们通过samba和dlna在我家里共享媒体。 To enable a random feature, I created a batch script as follows to make a playlist file: 为了启用随机功能,我创建了如下的批处理脚本来制作播放列表文件:

    @Echo Off
    color 0e
    Echo PLEASE WAIT, BUILDING PLAYLIST FILE, EXCLUDING ALL BAT, TXT, M3U, SRT and JPG FILES
    del "playlist.m3u"
    Setlocal EnableDelayedExpansion
    set _tmp=%1
    Set _t0=1
    If ["%CD%"]==["%CD:~0,3%"] Set _t0=0
    For /F "tokens=*" %%A In ('Dir "%*" /a-d /b /on /s ^|find /v ".bat"^|  find /v ".m3u"^|find /v ".txt"^| find /v ".srt"^|find /v ".jpg"') Do (
   Set _t1=%%A
   Set _t2=!_t1:%CD%=!
   Echo !_t2:~%_t0%!)>>playlist.m3u

So, I have one of these batch files in each directory (one per TV show) as well as the parent folder (TV) and my users can enable random in WMP, VLC or w/e player they like, and just click the playlist to watch random anything, or more specifically a random episode of a specific show. 因此,我在每个目录(每个电视节目一个)和父文件夹(TV)中都有这些批处理文件之一,我的用户可以在喜欢的WMP,VLC或w / e播放器中启用随机播放功能,只需单击播放列表观看随机的任何内容,或更具体地说,观看特定节目的随机片段。

So, on to my question: why is my piped output not alphabetical? 因此,我的问题是:为什么我的管道输出不按字母顺序排列? I really want to accomplish this in MS-DOS / CMD as programming is not my expertise, and this is pretty much the limit of my capability with scripting or anything else. 我真的想在MS-DOS / CMD中完成此任务,因为编程不是我的专长,而这几乎是我使用脚本或其他任何功能的限制。 I imagine the FIND command could be used more elegantly to filter out specific file types, and I suspect therein lies my issues with trying to sort the output. 我想可以更优雅地使用FIND命令来过滤出特定的文件类型,并且我怀疑其中存在尝试对输出进行排序的问题。 Even the show-specific playlists appear to have a random sort order (files within seasons are alphabetical, but the season order appears random) I was also wondering if someone could shed some light on getting folders with "&" in the name not breaking the output procedure. 即使是特定节目的播放列表似乎也具有随机的排序顺序(季节中的文件是字母顺序的,但是季节顺序是随机的),我也想知道是否有人可以找到一些名称为“&”的文件夹而不会破坏输出过程。

Any help would be appreciated. 任何帮助,将不胜感激。

-TJ -TJ

Hmm.. 嗯..

First point - why do you need alphabetical order if your intention is to do some variety of "random" selection? 第一点-如果您打算进行各种“随机”选择,为什么需要按字母顺序排列?

Next - why are you setting _tmp - you aren't using it. 接下来-为什么要设置_tmp您没有使用它。
Next - the brackets in your IF "%CD%"... are redundant - the quotes are there to tell batch that the string may contain separators 接下来-您的IF "%CD%"...的括号是多余的-用引号告诉批次字符串可能包含分隔符
Next - the /on switch tells DIR to output the SELECTED directory in alphabetical order. 下一步- /on开关告诉DIR以字母顺序输出SELECTED目录。
Next - _t0 appears to be intended to select 'lop 0' or 'lop 1' characters, but a DIR /s /b output will be X:\\dir...\\filename - doesn't sem particularly sensible... 接下来_t0似乎旨在选择'lop 0'或'lop 1'字符,但是DIR /s /b输出将为X:\\dir...\\filename并不特别明智。
Next - you seem to have omitted the /i switch from your FIND commands to make the find case-insensitive. 下一步-您似乎已经从FIND命令中省略了/i开关,以使find不区分大小写。

You've not shown your directory structure, so "season order" is nebulous. 您尚未显示目录结构,因此“季节顺序”很模糊。 Presumably you've got ..\\showname\\season\\episode. 大概您有.. \\ showname \\ season \\ episode。 Consider what happens when you have season 1,2..10,11. 考虑当您有第1,2..10,11季时会发生什么。 If sorted, you'd get 1,10,11,2.. Same comment may apply to your episode name. 如果排序,您将得到1,10,11,2.。同样的注释可能适用于您的剧集名称。

Here's a method of producing a sorted list. 这是一种产生排序列表的方法。 In this case, it'll detect files containing with in all subdirectories from the current, minus the selected extensions in any case - because I have a reasonable number of filenames containing with and & (also ! and % ) in my batch-test directory. 在这种情况下,它会检测包含文件with从当前所有子目录,减去选定扩展在任何情况下-因为我有文件名的合理数量的含有with& (也!%在我的批处理-test目录) 。 Note also that a new file is created without the delete and append method. 另请注意,将创建一个没有delete and append方法的新文件。 also the output filename is different - and DELAYEDEXPANSION is not invoked. 且输出文件名也不同-并且不会调用DELAYEDEXPANSION

@ECHO OFF
SETLOCAL 
(
 For /F "tokens=*" %%A In ('Dir "*with*" /a-d /b /s ^|find /i /v ".bat"^|  find /i /v ".m3u"^|find /i /v ".txt"^| find /i /v ".srt"^|find /i /v ".jpg"^|sort') Do (
ECHO %%A
 )
)>newfile.txt
GOTO :EOF

You don't say where the & is breaking your code. 您不会说&在哪里破坏了您的代码。 This structure doesn't care - the & and other difficult characters act quite happily. 这种结构无关紧要- &和其他困难人物的举止很愉快。 Perhaps your difficulies are with the code section you have shown - perhaps they are later on... 也许您对显示的代码部分感到困惑-也许稍后再说...

Finally, DIR is relatively simple and doesn't allow exclusion-by-name. 最后, DIR比较简单,并且不允许按名称进行排除。 Possibly a more elegant method would be by using 可能更优雅的方法是使用

xcopy /y /L /s sourcedir x:\nul\

a matter for experimentation. 一个实验的问题。 XCOPY /? from the prompt for documentation - which will show how to use the /exclude: switch. 从文档提示中-将显示如何使用/exclude:开关。 Two cautions though - the xcopy approach will select directory names and the last line will be a count-of-files-in-list. 但是有两个注意事项xcopy方法将选择目录名称,最后一行将是列表中的文件计数。

 if not exist "%%A\." if exist "%%A" ECHO %%A

should overcome these two problems. 应该克服这两个问题。

See if this does what you need. 看看这是否满足您的需求。 Give an example playlist if it needs tweaking. 如果需要调整,请举一个播放列表示例。

@Echo Off
color 0e
Echo PLEASE WAIT, BUILDING PLAYLIST FILE, EXCLUDING ALL BAT, TXT, M3U, SRT and JPG FILES
del "playlist.m3u" 2>nul
For /F "delims=" %%A In (' Dir "%*" /a-d /b /on /s ^|findstr /v /i ".bat .m3u .txt .srt .jpg" ^|sort ') Do (
Echo %%~pnxA
>>playlist.m3u Echo %%~pnxA
)

I know it's been a while, but here is the final batch script that generates my desired output: 我知道已经有一段时间了,但是这是生成所需输出的最终批处理脚本:

@Echo Off
color 0e
Echo PLEASE WAIT, BUILDING PLAYLIST FILE, EXCLUDING BAT, TXT, M3U, SRT and JPG FILES
del "playlist.m3u"
Setlocal EnableDelayedExpansion
Set _t0=1
If ["%CD%"]==["%CD:~0,3%"] Set _t0=0
For /F "tokens=*" %%A In ('Dir "%*" /a-d /b /on /s ^|findstr /v /i ".bat .m3u .txt .srt .jpg"^| sort') Do (
Set _t1=%%A
Set _t2=!_t1:%CD%=!
Echo !_t2:~%_t0%!)>>playlist.m3u

I'm sorry I took so long to get back here, but you know, life gets in the way sometimes. 对不起,我花了很长时间才回到这里,但您知道,生活有时会受到阻碍。 Please find below a sample of my desired output. 请在下面找到我所需输出的样本。 A file list in this manner works as a .m3u playlists that will play off of a network location whether the folder is mapped to a drive letter or not. 以这种方式的文件列表用作.m3u播放列表,无论文件夹是否映射到驱动器号,该播放列表都会从网络位置播放。 It also works regardless of where in the folder hierarchy a user decides to map. 无论用户决定在文件夹层次结构中的哪个位置,它都可以工作。 Example: 例:

Z:\md0\media\tv\30 rock\season 1\s01e01.avi
or
y:\tv\30 rock...
or
r:\30 rock...
or even
\\tjmediaserver\video\tv\30 rock\...

are all valid ways for users to access my open network share at this location. 是用户在此位置访问我的开放网络共享的所有有效方法。 Sample batch output: 样品批输出:

Season 1\30 Rock S01E01 Pilot.avi
Season 1\30 Rock S01E02 The Aftermath.avi
Season 1\30 Rock S01E03 Blind Date.avi

Both Windows Media Player and VLC (only players we use) assume the files are within the subdirectories of where the .m3u is stored. Windows Media Player和VLC(仅我们使用的播放器)均假定文件位于.m3u存储位置的子目录中。 Handy if I want to watch show "X" from the beginning, or enable shuffle in my media player and watch random "x" or random "x" and "y". 如果我想从头开始观看节目“ X”,或者在媒体播放器中启用随机播放,并观看随机的“ x”或随机的“ x”和“ y”,则非常方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM