我想创建一个文件,将所有图像复制到当前目录中的新文件夹中,但您可以编辑该文件以删除 AppData 或 Program Files 等目录。问题:它无法识别目录中是否有空格,并在空格字符所在的位置将其截断,最终导致代码中断。有人能帮忙解释如何修复它吗?
@echo off
cls
color 0a
:youtu.be/P7JVc22xVa8
set directory=%cd%
set /a namechooser=%random% * 20 / 32768 + 1
if %namechooser%==5 (set "name=booba") else (set "name=All Images")
:pt1
cls
echo Do you want to make every file unhidden before searching for these images?
choice /m ">"
if %errorlevel%==1 (attrib -h *.*)
:pt2
cls
echo Okay, searching for all common images now. Please wait . . .
for /r %%i in (*.bmp, *.gif, *.ico, *.jpg, *.jpe, *.jpeg, *.kra, *.png, *.svg, *.tif, *.tiff) do (echo %%i>>"allimages.log")
attrib +t allimages.log
cls
echo Alright, do you want to organize all of the located files into a folder in this current directory?
choice /m ">"
if %errorlevel%==2 (cls & echo Oh, okay. & pause & exit)
pause
:pt3
mkdir "%cd%/%name%"
echo Copying the files into the folder. Please wait . . .
for /f "%%r" in (allimages.log) do (echo %%r)
pause
del "allimages.log" >nul
cls
echo Done!
pause
exit
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Please don't question why I'm using so much extra unnecessary code just to do something that could be done :::::
:: in a few lines; this is an experiment that makes me use "for loops" in some sort of situation. :::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
答案1
如果我没有误解的话,您应该尝试添加"tokens=*"
到您的日志处理循环中:
for /f "tokens=*" %%r in (allimages.log) do (echo %%r)
请注意,使用"%%r"
可能会导致问题(所以不是"tokens=*" "%%r"
)。
类似地,为了复制项目,您可以执行以下操作:
for /f "tokens=*" %%r in (allimages.log) do (copy "%%r" "%cd%/%name%")
处理带有空格的路径时,上述命令""
中的是必需的。copy
边注
因此,您知道,在您的扩展读取循环中,*.jpe
和*.jpeg
都将匹配具有.jpeg
扩展名的文件。这意味着您的日志文件可能有一组或多组条目,例如:
C:\path\to\image.jpeg
C:\path\to\image.jpeg
在处理文件时这应该不会有太大影响(即,在复制时您可能只会处理任何重复的条目两次),但可能值得牢记。
您可以通过删除*.jpe
条目(仅匹配.jpeg
文件而不匹配.jpe
)或*.jpeg
条目(匹配.jpe
和.jpeg
文件)来消除此行为。