这会记住被复制批处理文件 [英] batch file that remembers what was copied

查看:385
本文介绍了这会记住被复制批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我需要被扩展最初的问题
<一href=\"https://stackoverflow.com/questions/33150045/creating-folder-from-file-copy-initial-file-into-folder-and-add-$p$pfix?answertab=active#tab-top\">Creating从文件夹,初始文件复制到文件夹并添加preFIX

This was the initial question which I need to be "extended" Creating folder from file, copy initial file into folder and add prefix

这是输出批次

for /f "tokens=*" %%A in ('dir /b *.mp4') do (
    md "%%~nA"
    copy "%%~fA" "%%~nA\prefix_%%~nxA"
)

(如果有.MP4,创建一个文件夹的文件名和复制.MP4到该文件夹​​中)

(if there is a .mp4, create a folder with that files name and copy the .mp4 into that folder)

现在该批处理文件应记住什么文件夹的创建,以便它不会重新启动相同的操作时,每一个新创建的文件夹被删除的时间。
我在想,如写名字入.txt然后从上面的蝙蝠进行休息每一批新的任务可能检查名称存在。如果没有=拷贝,如果​​是=什么也不做。

Now that batch file should "remember" what folder was created so it wont restart the same operation every time when the newly created folder is deleted. I was thinking like "write the name into a .txt then perform rest from above bat" every new batch task could check if the name exists. if no = copy, if yes = do nothing.

推荐答案

= http://stackoverflow.com/users/778560/aacini\">Aacini 是好的,是由我投票了,但可能会导致错误的MP4文件的处理在一定条件下。

The answer written by Aacini is good and was voted up by me, but could result in wrong MP4 file processing under certain conditions.

在其上发生错误处理的条件是:

The conditions on which wrong processing occurs are:


  1. 中的* .mp4文件存储在,而不是NTFS驱动器FAT,FAT32或exFAT的。

  1. The *.mp4 files are stored on a FAT, FAT32 or exFAT instead of an NTFS drive.

在经常在SD卡和USB使用FAT驱动器粘文件系统会根据文件分配表项(通常是最早的第一)返回文件匹配模式,并且根据名称为NTFS文件系统总是不按字母顺序排序。

On FAT drives as often used on SD cards and USB sticks the file system returns files matching a pattern according to file allocation table entry (usually oldest first) and not sorted alphabetically according to name as NTFS file system always does.

有MP4文件,这些文件名的其他MP4文件子具有更长的名称。

There are MP4 files which names are substrings of other MP4 files with longer names.

例如有是第一次创建一个FAT32驱动器(先在文件分配表)上的文件 Test1.mp4 ,也是一个文件 Test.mp4 后来被创建,因此,新的(第二次在文件分配表)。

For example there is a file Test1.mp4 which was created first on a FAT32 drive (first in file allocation table) and also a file Test.mp4 which was created later and is therefore newer (second in file allocation table).

Aacini 的结果与旧 Test1.mp4 和新的 Test.mp4 在FAT32驱动器上

The usually working code written by Aacini results now with older Test1.mp4 and newer Test.mp4 on FAT32 drive in


  • 创建目录测试1

  • 复制 Test1.mp4 进入目录测试1

  • 测试1 到文本文件中,

  • 跳过处理 Test.mp4 ,因为字符串测试在文本文件中已经找到作为是<$的一个子C $ C>测试1 。

  • creating directory Test1,
  • copying Test1.mp4 into directory Test1,
  • writing Test1 into the text file,
  • skipping processing for Test.mp4 because string Test already found in text file as being a substring of Test1.

以下增强功能,以避免这些条件下,这种有害的行为:

Following enhancements could be made to avoid this unwanted behavior under these conditions:


  1. 使用参数 / ON 对命令的 DIR 以获得按名称的字母顺序排序作为NTFS驱动器列表中。

  1. Use parameter /ON on command DIR to get the list sorted alphabetically by name as on NTFS drives.

确认文本没有找到一个较长的文件/目录名内提交当前文件/目录名。

Make sure not finding in text file the current file/directory name within a longer file/directory name.

和文件/目录名在Windows下不区分大小写这也应考虑使用选项在文本文件中搜索/ I

And file/directory names are case insensitive on Windows which should be also taken into account on searching in text file using option /I.

第一个解决方案使用也指挥的查找不具有一个选项,以仅匹配整行。文件/目录名与周围的因此写:到文本文件,也搜寻周围冒号。冒号不能在一个文件或目录的名称使其成为一个很好的开始/结束符。

The first solution uses also command FIND which does not have an option to match entire lines only. The file/directory name is written therefore with surrounding : into the text file and also searched with surrounding colons. A colon can't be in name of a file or directory making it a good start/end delimiter.

@echo off

if not exist FoldersCreated.txt cd . > FoldersCreated.txt

for /F "tokens=*" %%A in ('dir /A-D /B /ON *.mp4') do (
    %SystemRoot%\System32\find.exe /I ":%%~nA:" FoldersCreated.txt >NUL
    if errorlevel 1 (
        md "%%~nA"
        copy "%%~fA" "%%~nA\prefix_%%~nxA"
        echo :%%~nA:>>FoldersCreated.txt
    )
)

另一种解决方案是使用 FINDSTR 与选项 / X 来获得只有当搜索字符串的整条生产线相匹配的阳性结果。

Another solution is using FINDSTR with option /X to get a positive result only if searched string matches an entire line.

@echo off

if not exist FoldersCreated.txt cd . > FoldersCreated.txt

for /F "tokens=*" %%A in ('dir /A-D /B /ON *.mp4') do (
    %SystemRoot%\System32\findstr.exe /I /L /M /X /C:"%%~nA" FoldersCreated.txt >NUL
    if errorlevel 1 (
        md "%%~nA"
        copy "%%~fA" "%%~nA\prefix_%%~nxA"
        echo %%~nA>>FoldersCreated.txt
    )
)

有关理解使用的命令以及它们如何工作,打开命令提示符窗口中,执行有下面的命令,并阅读完全针对每个命令显示的所有帮助页面非常谨慎。

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.


  • 复制/?

  • DIR /?

  • 回声/?

  • 查找/?

  • FINDSTR /?

  • 为/?

  • 如果/?

  • MD /?

  • copy /?
  • dir /?
  • echo /?
  • find /?
  • findstr /?
  • for /?
  • if /?
  • md /?

注意:

必须小心上线第二个解决方案回声%%〜nA的&GT;&GT; FoldersCreated.txt 关于尾随空格。不应该有尾随空格,否则 FINDSTR 从来没有退出0的整条生产线的匹配。

Care must be taken on second solution on line echo %%~nA>>FoldersCreated.txt regarding trailing spaces. There should be no trailing space as otherwise FINDSTR never exits with 0 for an entire line match.

发表 Aacini的code 的作为模板,这两种解决方案具有此行结束1空间,即有 foldersCreated.txt <大骨节病>空格在这条线。

The code posted by Aacini used as template for both solutions has 1 space at end of this line, i.e. there is foldersCreated.txtspace on this line.

这尾随空格间$ P $由命令处理器PTED为是通过命令文本输出的一部分回声并因此执行被命令处理器:

This trailing space is interpreted by command processor as being part of the text to output by command echo and therefore executed was by command processor:

echo Test1  1>>FoldersCreated.txt
echo Test  1>>FoldersCreated.txt

因此​​,在code行尾的空间文件名后移动,因此也写入到文本文件。由于没有在搜索字符串没有尾随空间调用的 FINDSTR ,从未有一整行的比赛。

So the space at end of code line is moved after file name and is therefore written also into the text file. As there is no trailing space in search string on calling FINDSTR, there is never an entire line match.

这是无解的周围使用的文件名,即

It is no solution to use double quotes around file name, i.e.

echo %%~nA>>"FoldersCreated.txt"

另外用双引号尾随空间被命令处理器通过命令移到文本的结束输出的回声

这也是使用无解

>>FoldersCreated.txt echo %%~nA

作为此行的后面加上一个空格必须仍然避免,因为命令处理器还执行这一行:

as a space at end of this line must be nevertheless avoided because command processor executes this line also as:

echo %%~nA 1>>FoldersCreated.txt 

所以经过任何空间%%〜nA的将是文本输出的一部分,但回声

So any space after %%~nA would be nevertheless part of the text to output by echo.

这篇关于这会记住被复制批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆