我目前正在编写一个程序,可以将所有图像编译到一个小目录中。我一直在尝试使用此代码,并弄清楚为什么它总是中断。在 CMD 中,它说“此时 %~xr 是意外的”,尽管我分配给这个 for 循环的变量是……“%%r”。我正在使用批处理文件,这意味着应该使用 %%r 而不是 %r。事实上,在代码中,我确实使用了“%%~xr”作为文件扩展名“%%r”,所以我不知道发生了什么。有人能帮我吗?
注意:我并不是很有经验,我只是在这个例子中使用我想出的东西,所以请忍受它的不稳定性。
@echo off
set /a counter=1
set name=All Images
mkdir "%cd%/%name%" >nul
rem This creates a directory called "%cd%/All Images." "%cd%" is the batch file's current directory.
for /f "tokens=*" %%r in (allimages.log) do (
set "extension=%%~xr"
rem Set the variable "extension" to whatever the file extension that was picked up from "allimages.log." This is important for the "call" command as it will break if given a for loop's variable.
call :isuckatcoding
if not exist "%cd%\%name%\%%~xr\%%~nxr" (
rem This is saying if not exist "%cd%\All Images\[file extension]\[file name and extension]"
copy "%file%" "%cd%\%name%\%%~xr"
) else (
set /a counter+=1
copy "%%r" "%cd%\%name%\%%~xr\%%~nr (%counter%)%%~xr"
)
rem This code is supposed to rename the file being copied if there already exists that file name in the directory being copied to. However, this code never seems to work for some reason.
)
:isuckatcoding
if not exist "%cd%\%name%\%extension%" (mkdir "%cd%/%name%/%extension%")
exit /b
rem Creates a ".jpg" directory if one doesn't exist.
以下是“allimages.log”的内容:
C:\Users\Rando\Desktop\Need to Sort\Batch Files\booba test time baby\New folder\test.jpg
C:\Users\Rando\Desktop\Need to Sort\Batch Files\booba test time baby\New folder (2)\testa.jpg
我已经验证这两个文件确实仍然存在。我还意识到重命名相关的代码似乎没有被使用,但大多数情况下文件的名称只是“test.jpg”。
答案1
批量循环无法识别其自身的变量
要在函数中使用一些值,这些值可以是一些文件、扩展名、文件夹、变量等等。有必要将此项传递给他,如果不传递要在函数内使用的东西,他将无法访问已经定义的变量:
:: call :label parameter1 parameter2 ... parameterN
call %:^) "%_dir%\%%~xr"
%:^)
echo\ heyo, be sure that I can use %~1 for do my job
exit /b
关于您的代码:
您可以使用或不使用标签/函数来完成此操作,但对于这两个选项,您都需要在运行时更新其内部计数器变量enabledelayedexpansion
的值。!
!
for()do if()else()
- 没有
:label
@echo off
set "_dir=%~dp0All Images"
setlocal enabledelayedexpansion
2>nul mkdir "%_dir%" & set "_cnt=1"
for /f usebackq^delims^= %%r in (.\allimages.log) do 1<nul 2>nul (
md "%_dir%\%%~xr" & if not exist "%_dir%\%%~xr\%%~nxr" >nul (
copy "%%~r" "%_dir%\%%~xr\.") else >nul (set /a "_cnt+=1"
copy "%%~r" "%_dir%\%%~xr\%%~nr (!_cnt!)%%~xr") )
endlocal & goto :eOf
- 相同的代码常规布局:
@echo off
setlocal enabledelayedexpansion
set "_cnt=1"
set "_dir=%~dp0All Images"
2>nul mkdir "%_dir%"
for /f "usebackq delims=" %%r in (allimages.log) do (
md "%_dir%\%%~xr" 2>nul
if not exist "%_dir%\%%~xr\%%~nxr" (
copy "%%~r" "%_dir%\%%~xr" >nul
) else (
set /a "_cnt=!_cnt! + 1"
copy "%%~r" "%_dir%\%%~xr\%%~nr (!_cnt!)%%~xr" >nul
)
)
endlocal & goto :eOf
- 和
:label
@echo off
setlocal enabledelayedexpansion
set "_cnt=1"
set "_dir=%~dp0All Images"
2>nul mkdir "%_dir%"
for /f "usebackq delims=" %%r in (allimages.log) do (
if not exist "%_dir%\%%~xr\%%~nxr" (
call %:^) "%_dir%\%%~xr"
copy "%%~r" "%_dir%\%%~xr" >nul
) else (
set /a "_cnt=!_cnt! + 1"
copy "%%~r" "%_dir%\%%~xr\%%~nr (!_cnt!)%%~xr" >nul
)
)
%:^)
if not "%~1" == "" (
if not exist "%~1" md "%~1"
exit /b
) else (
endlocal
)
:: "%~dp0" => Expand to "Drive:\Folder\Path" to yourfile.bat
if you current work directory is the same from your bat, you can also use:
set "_dir=%~dp0All Images"
:: 2>nul omit/suppress output to any warning/error
if folder does not exist it will be created, if it exists, omit the error...
2>nul mkdir "%_dir%"
- 如果您想将文件扩展名创建为不带点(.)的文件夹,我建议这样做:
@echo off
set "_dir=%~dp0All Images"
setlocal enabledelayedexpansion
2>nul md "%_dir%" & set "_cnt=1"
for /f usebackq^delims^= %%r in (
.\allimages.log) do 1<nul 2>nul (
;set "_eXt=%%~xr" & ;call md "%_dir%\!_eXt:.=!"
;if not exist "%_dir%\!_eXt:.=!\\%%~nxr" >nul (
;copy "%%~r" "%_dir%\!_eXt:.=!\\.") ;else (set/a "_cnt+=1"
<con: copy "%%~r" "%_dir%\!_eXt:.=!\%%~nr (!_cnt!)%%~xr"))
tree /a & set /p "= Files duplicated: !_cnt! | Files copied: " <nul
dir /a-d /b /s "All Images" | find /v /c "" & timeout -1 & endlocal
- 我的测试场景中的输出是:
Folder PATH listing
Volume serial number is 02DBAE18 9A8E:862E
C:.
\---All Images
+---gif
+---jpeg
+---jpg
\---png
Files duplicated: 34 | Files copied: 102
进一步阅读:
[√]如果 /?
[√]放 /?
[√]为了 /?
[√]对于/F
[√]延迟扩展(参考!Expand_RunTime_Variable!
:)