试图找出批处理文件运行两次的原因

发布于 2024-10-05 11:57:35 字数 836 浏览 0 评论 0原文

我确信这是显而易见的,但我似乎无法弄清楚,出于某种原因,我在下面粘贴的批处理文件在点击:重命名时总是运行两次而不是一次。有人可以告诉我这里的问题是什么吗?这与其他两个问题有关 - 寻找一种在文件夹达到 10 个文件后执行批处理文件的方法通过批处理文件复制并重命名某个扩展名的文件

这是批处理文件 --- >

rem Counting files...
set /a count = 0
for /f "tokens=*" %%P IN ('dir "H:\" /A /b') do (set /a count += 1)



rem 5 or more files?



if %count% GEQ 5 call :rename



:rename
SET count=1
FOR /f "tokens=*" %%G IN ('dir /b *.jpg') DO (call :rename_next "%%G")

goto:copy

:rename_next
ren "%1" %count%.jpg

Pause
set /a count+=1

goto:eof

:copy
xcopy c:\photo\*.jpg c:\photo\files /Y
Pause

i am sure this is something obvious, but i cant seem to figure it out, for some reason, the batch file i have pasted below always runs twice instead of once when it hits :rename. Could someone tell me what the issue here is? This is related to the 2 other questions - Looking for a way to execute a batch file once a folder hits 10 files and copy and rename files of a certain extension via batch file

Here is the batch file --- >

rem Counting files...
set /a count = 0
for /f "tokens=*" %%P IN ('dir "H:\" /A /b') do (set /a count += 1)



rem 5 or more files?



if %count% GEQ 5 call :rename



:rename
SET count=1
FOR /f "tokens=*" %%G IN ('dir /b *.jpg') DO (call :rename_next "%%G")

goto:copy

:rename_next
ren "%1" %count%.jpg

Pause
set /a count+=1

goto:eof

:copy
xcopy c:\photo\*.jpg c:\photo\files /Y
Pause

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灵芸 2024-10-12 11:57:35

这一行:

if %count% GEQ 5 call :rename

正在调用 :rename。 rename 返回后,代码在此 if 后继续,恰好又是 :rename

替换为以下代码以查看发生了什么:

echo before call
if %count% GEQ 5 call :rename
echo after call

This line:

if %count% GEQ 5 call :rename

is calling :rename. After rename returns, the code continues after this if, which happens to be :rename again.

Replace with this code to see what's happening:

echo before call
if %count% GEQ 5 call :rename
echo after call
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文