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



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

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

@echo on
Set first_folder="G:Site BackupsBM Backuppublic_htmlVideoshold"
Set second_folder="C:UsersWorkDesktopTest Envthumbs unorganized"
Set third_folder="C:UsersWorkDesktopTest Envthumbs 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

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

系统找不到指定的路径

示例:名为《国王与青蛙!!》的视频的缩略图!!。mp4将给出错误,但国王和青蛙的缩略图。mp4移动!

他们为什么不动有什么想法吗?

另一件事是,当脚本到达一个名为14th&15th它不会在目录中拾取它。

更新的工作代码:

@echo on
Set "first_folder=G:Site BackupsBM Backuppublic_htmlVideoshold"
Set "second_folder=C:UsersWorkDesktopTest Envthumbs unorganized"
Set "third_folder=C:UsersWorkDesktopTest Envthumbs 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

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

Set "first_folder=G:Site BackupsBM Backuppublic_htmlVideoshold"
Set "second_folder=C:UsersWorkDesktopTest Envthumbs unorganized"
Set "third_folder=C:UsersWorkDesktopTest Envthumbs organized"

然后将它们用作:

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

setlocal enabledelayedexpansion应该在脚本中删除,因为您从未使用过它,但它会使用!,因此您会遇到问题。

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

这里的路径中有双引号,双引号现在构成了值的一部分:

set first_folder="G:Site BackupsBM Backuppublic_htmlVideoshold"

然后在循环中:

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

翻译过来就是:

"%first_folder%"somedir14th & 15th

所以你当前的代码(我没有增强(只是修复问题:

@echo on
Set "first_folder=G:Site BackupsBM Backuppublic_htmlVideoshold"
Set "second_folder=C:UsersWorkDesktopTest Envthumbs unorganized"
Set "third_folder="C:UsersWorkDesktopTest Envthumbs 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

好吧,我重新写这篇文章是为了消除一些冗余并使用一些更好的实践。

正如你所期望的那样,这应该是必要的。对不起,我在上午准备时花了一些时间。

如果不存在要移动到的文件夹,我更改了脚本以创建所需的文件夹,并且我要检查脚本以确保源jpeg存在(可能不存在(,如果不存在,它会通知你,而不是在移动时抛出错误。

我也有MOVE文件重写现有文件,而不是提示。

@(setlocal
echo on
)
Set "first_folder=G:Site BackupsBM Backuppublic_htmlVideoshold"
Set "second_folder=C:UsersWorkDesktopTest Envthumbs unorganized"
Set "third_folder=C:UsersWorkDesktopTest Envthumbs 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会导致脚本中不必要的内存膨胀,然后耗尽可克隆的内存空间,这可能会引发错误,因为您没有这样做;在同一块中使用成对的CCD_ 5;它也完全没有必要,因为它在代码中没有任何用途。

Enabledelayedexpansion导致的错误。感叹号等。在任何情况下,代码都不需要感叹号。

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

第四,你对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"作为后面的空白是不可能的。

最新更新