我正在尝试创建一个批处理文件,以将我的 \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 周围用双引号尝试过它,那么为什么它将表达式分解为单个元素呢?