0

我正在尝试创建一个批处理文件,以将我的 \iTunes\Movies\ 文件夹中的目录备份到外部驱动器(如果外部驱动器上尚不存在):

@echo off 
cls
SetLocal EnableDelayedExpansion

for /f "delims=" %%F in ('dir /ad /b') do (
if not exist g:\movies\%%F      (
        echo Copying %%F to G:\Movies\%%F
rem         md G:\Movies\%%F
rem         copy M:\iTunes\Movies\%%F\*.* G:\Movies\%%F
    )
)

这是输出:

Code:
Copying The Princess Bride to G:\Movies\The Princess Bride
Copying Green to G:\Movies\Green
Copying Red to G:\Movies\Red
Copying Princess to G:\Movies\Princess
Copying Bride to G:\Movies\Bride
Copying The Green Mile to G:\Movies\The Green Mile
Copying The Shawshank Redemption to G:\Movies\The Shawshank Redemption
Copying Grape to G:\Movies\Grape
Copying Roof to G:\Movies\Roof
Copying On to G:\Movies\On
Copying Fiddler On the Roof to G:\Movies\Fiddler On the Roof
Copying Brief to G:\Movies\Brief
Copying Number to G:\Movies\Number
Copying to to G:\Movies\to
Copying the to G:\Movies\the
Copying Gilbert Grape to G:\Movies\Gilbert Grape
Copying Torino to G:\Movies\Torino
Copying Sherlock Holmes to G:\Movies\Sherlock Holmes
Copying Gran Torino to G:\Movies\Gran Torino
Copying I Am Number Four to G:\Movies\I Am Number Four
Copying Too Big to Fail to G:\Movies\Too Big to Fail
Copying Pelican to G:\Movies\Pelican
Copying The Shunning to G:\Movies\The Shunning
Copying Inside Job to G:\Movies\Inside Job
Copying Shawshank to G:\Movies\Shawshank
Copying Holmes to G:\Movies\Holmes
Copying Four to G:\Movies\Four
Copying Am to G:\Movies\Am
Copying Big to G:\Movies\Big
Copying Job to G:\Movies\Job
Copying Mile to G:\Movies\Mile
Copying Fail to G:\Movies\Fail
Copying The Pelican Brief to G:\Movies\The Pelican Brief
Copying Shunning to G:\Movies\Shunning
Copying Redemption to G:\Movies\Redemption
Copying Labyrinth to G:\Movies\Labyrinth
Copying The Hunger Games to G:\Movies\The Hunger Games

M:\iTunes\Movies>

它不仅正确地使用了完整的目录名称,比如“The Green Mile”,而且对于存在于 iTunes\Movies 文件夹中但不存在于外部驱动器。

我已经尝试过使用和不使用 EnableDelayedExpansion 并尝试了各种类型的变量分配无济于事。

我只是不明白,我已经编程了“delims =”并且我已经在 %%F 周围用双引号尝试过它,那么为什么它将表达式分解为单个元素呢?

4

2 回答 2

1

您需要在命令本身的名称周围加上引号,并且需要 tokens=* 部分(如此处所述“在 FOR 循环中使用什么分隔符来读取行?

@echo off 
cls
SetLocal EnableDelayedExpansion

for /f "tokens=*" %%F in ('dir /ad /b') do (
if not exist "g:\movies\%%F"      (
        echo Copying "%%F" to "G:\Movies\%%F"
rem         md "G:\Movies\%%F"
rem         copy "M:\iTunes\Movies\%%F\*.*" "G:\Movies\%%F"
    )
)

只要您在 Windows 命令中有一个参数,该参数是从可能包含空格的批处理变量构建的(就像文件名一样),就需要引号

于 2012-12-31T19:38:47.860 回答
1

这是我的演绎。由于有一些增强功能将使您的脚本受益。不过,它唯一需要修复的是存在、md 和复制路径周围的一些引号。

  1. 使用简单的目录进行循环。
  2. ~nx如果命令行解析器决定更改它们返回值的方式,则使用选项删除周围的引号并仅使用文件夹的名称。(未来证明)
  3. 添加\到存在和复制路径的末尾以指定它是一个目录并且不匹配任何可能具有相同名称的文件。
  4. 放在copy后面的md意思&&是只有在成功创建目录时才会发生副本。

代码:

@echo off 
cls
SetLocal EnableDelayedExpansion

for /d %%D in (*) do if not exist "G:\Movies\%%~nxD\" (
    echo Copying %%~fD to G:\Movies\%%~nxD
    rem md "G:\Movies\%%~nxD" && copy "%%~fD\*.*" "G:\Movies\%%~nxD\"
)

如果您需要更多选项,我还建议您查看xcopyand命令。robocopy

于 2012-12-31T21:45:10.830 回答