路径正在扩展到个别元素?

时间:2012-12-31 19:22:07

标签: batch-file command dos

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

2 个答案:

答案 0 :(得分:1)

你需要命令本身名称周围的引号,你需要tokens = *部分(如“What delimiter to use in FOR loop for reading lines?”所述

@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命令中有一个参数从一个可能包含空格的批处理变量构建(如文件名那样),就需要引用这个引号

答案 1 :(得分:1)

这是我的演绎。由于有一些增强功能可以使您的脚本受益。它需要解决的唯一问题是存在,md和复制路径的一些引用。

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

    @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\"
    )
    

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