批处理文件移动不适用于某些特殊字符

Batch File Move Not Working For Some Special Characters

我有一个视频托管网站,其中的视频存储在年-月-日子文件夹中。我有第二个文件夹,其中包含第一个文件夹中所有视频的所有缩略图。

下面的脚本递归地获取第一个文件夹中视频文件的名称和目录,并使用这些变量将第二个文件夹中的缩略图移动到第三个文件夹。

@echo on

Set first_folder="G:\Site Backups\BM Backup\public_html\Videos\hold"
Set second_folder="C:\Users\Work\Desktop\Test Env\thumbs unorganized"
Set third_folder="C:\Users\Work\Desktop\Test Env\thumbs organized"

for /f "tokens=*" %%A in ('dir /b /a:d %first_folder%') do (

for /f "tokens=*" %%B in ('dir /b /a:d %first_folder%\%%A') do (

    for /f "tokens=*" %%C in ('dir /b /a:d %first_folder%\%%A\%%B') do (

        setlocal EnableDelayedExpansion

        for /f "delims= eol=" %%D in ('dir %first_folder%\%%A\%%B\%%C /b') do (

            for /f "delims=\ tokens=7-9" %%f in ("%first_folder%\%%A\%%B\%%C\%%D") do (

                    IF EXIST %third_folder%\%%f-%%g-%%h (

                        move %second_folder%\"%%~nD.jpg" %third_folder%\%%f-%%g-%%h\"%%~nD.jpg"

                    )
            )
        )
    )  
) 
)

@echo on

echo "image files have been translated successfully!"
pause

唯一的问题是如果视频名称有时出于奇怪的原因包含感叹号和括号等特殊字符,脚本会说:

The system cannot find the path specified

示例:名为 The King and the Frog!!.mp4 的视频的缩略图会给出错误,但 The King and the Frog 的缩略图.mp4 会动!

关于他们为什么不搬家有什么想法吗?

另一件事是,当脚本到达名称为 14th 和 15th 的子文件夹时,它不会在目录中选择它。

更新的工作代码:

@echo on

Set "first_folder=G:\Site Backups\BM Backup\public_html\Videos\hold"
Set "second_folder=C:\Users\Work\Desktop\Test Env\thumbs unorganized"
Set "third_folder=C:\Users\Work\Desktop\Test Env\thumbs organized"

for /f "tokens=*" %%A in ('dir /b /a:d "%first_folder%"') do (
 for /f "tokens=*" %%B in ('dir /b /a:d "%first_folder%\%%A"') do (
  for /f "tokens=*" %%C in ('dir /b /a:d "%first_folder%\%%A\%%B"') do (
    for /f "delims= eol=" %%D in ('dir "%first_folder%\%%A\%%B\%%C" /b') do (
      for /f "delims=\ tokens=7-9" %%f in ("%first_folder%\%%A\%%B\%%C\%%D") do (
                IF EXIST "%third_folder%\%%f-%%g-%%h" (
                    move "%second_folder%\%%~nD.jpg" "%third_folder%\%%f-%%g-%%h\%%~nD.jpg"
          )
       )
    )
 )  
) 
)

@echo on

echo "image files have been translated successfully!"
pause

好的,我重写了这个以删除一些冗余并使用一些更好的做法。

这应该可以满足您的需求,抱歉,我上午准备就绪,所以花了一些时间。

我更改了脚本以创建预期的文件夹(如果不存在可移动到的文件夹),并且我有脚本来检查并确保源 jpeg 存在,但它可能不存在,如果不存在,它会提醒您' t 而不是为移动抛出错误。

我还有 MOVE 文件覆盖现有文件而不是提示。

@(setlocal
  echo on
)

Set "first_folder=G:\Site Backups\BM Backup\public_html\Videos\hold"
Set "second_folder=C:\Users\Work\Desktop\Test Env\thumbs unorganized"
Set "third_folder=C:\Users\Work\Desktop\Test Env\thumbs organized"

for /f "tokens=*" %%A in ('
  dir /b /ad "%first_folder%"') do (
  for /f "tokens=*" %%B in ('
    dir /b /ad "%first_folder%\%%A"') do (
    for /f "tokens=*" %%C in ('
      dir /b /ad "%first_folder%\%%A\%%B"') do (
      for %%_ IN (
        "%first_folder%\%%A\%%B\%%C\*") do (
        IF NOT EXIST "%third_folder%\%%A-%%B-%%C" (
          MD "%third_folder%\%%A-%%B-%%C" )
        IF EXIST "%second_folder%\%%~n_.jpg" (
          MOVE /Y "%second_folder%\%%~n_.jpg" "%third_folder%\%%A-%%B-%%C\%%~n_.jpg"
        ) ELSE (
          ECHO. The File "%second_folder%\%%~n_.jpg" DOES NOT EXIST!
        )
      )
    )
  )
)

@echo off

echo "image files have been translated successfully!"
pause

在循环中使用 Setlocal 将导致脚本中出现不必要的内存膨胀,然后 运行 超出可克隆的内存空间,这可能会引发错误,因为您不这样做;在同一块中使用成对的 endlocal;它也完全没有必要,因为它在您的代码中没有任何作用。

Enabledelayedexpansion 导致您出现错误。您的代码无论如何都不需要感叹号 etc.and。

此外,您的脚本不必要地将子文件夹名称的值重新转换为可以丢弃的新循环变量。

第四,你对 SETMOVE 命令的引用会给你带来问题。

我会在几分钟内重写这篇文章以删除一些冗余并修复这些部分以更好地练习。

然而,为了解释必要的内容,您应该将整个目录树和文件名用引号引起来,而不仅仅是 move 命令的文件名,以及您 set 早期创建的目录名所有人都在他们的变量中用引号括起来,这通常不是一件好事。

以这种方式设置变量可能会以空格结尾,最好使用 set "variable=value here without any unnecessary quotes "necessary quotes are okay though, so long as they are in pairs " and this is still in the variable" 因为尾随空格是不可能的。

设置变量路径时,需要用双引号将整个变量和值括起来:

Set "first_folder=G:\Site Backups\BM Backup\public_html\Videos\hold"
Set "second_folder=C:\Users\Work\Desktop\Test Env\thumbs unorganized"
Set "third_folder=C:\Users\Work\Desktop\Test Env\thumbs organized"

然后将它们用作:

move "%second_folder%\%%~nD.jpg" "%third_folder%\%%f-%%g-%%h\%%~nD.jpg"

setlocal enabledelayedexpansion 应该在脚本中删除,因为您从不使用它,但它会利用 ! 因此您的问题。

要了解引用问题,请查看您的 set 变量和 for 循环,如本例所示:

此处路径中有双引号,双引号现在构成值的一部分:

set first_folder="G:\Site Backups\BM Backup\public_html\Videos\hold"

然后在循环中:

for...("%first_folder%\%%A\%%B\%%C\%%D")

转换为:

"%first_folder%"\somedirth & 15th

所以你当前的代码(不是我增强的)只是解决问题:

@echo on

Set "first_folder=G:\Site Backups\BM Backup\public_html\Videos\hold"
Set "second_folder=C:\Users\Work\Desktop\Test Env\thumbs unorganized"
Set "third_folder="C:\Users\Work\Desktop\Test Env\thumbs organized"

for /f "tokens=*" %%A in ('dir /b /a:d "%first_folder%"') do (
 for /f "tokens=*" %%B in ('dir /b /a:d "%first_folder%\%%A"') do (
  for /f "tokens=*" %%C in ('dir /b /a:d "%first_folder%\%%A\%%B"') do (
    for /f "delims= eol=" %%D in ('dir /b "%first_folder%\%%A\%%B\%%C"') do (
      for /f "delims=\ tokens=7-9" %%f in ("%first_folder%\%%A\%%B\%%C\%%D") do (
                    IF EXIST "%third_folder%\%%f-%%g-%%h" (
                        move "%second_folder%\%%~nD.jpg" "%third_folder%\%%f-%%g-%%h\%%~nD.jpg"
              )
           )
        )
     )  
   ) 
)

@echo on

echo "image files have been translated successfully!"
pause