对于在批处理文件中循环重命名一个文件两次 [英] For Loop in Batch File Renames One File Twice

查看:338
本文介绍了对于在批处理文件中循环重命名一个文件两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个批处理文件,将一些文本附加到一组jpeg文件的文件名中,可以在当前文件名之前或之后附加它们。



但是由于某些奇怪的原因,当在文件名之前附加时,一个文件被处理两次,结果作为new_new_FileName.jpg,而所有其他文件只得到new_FileName.jpg。



有趣的是,这个问题并不总是发生,以及在文件名之后添加它总是工作正常。



下面是整个代码,没有视觉差异之前或之后,但仍然产生不同。任何人都可以检查此文件并解释我在哪里错了?



帮助感谢。

  @ECHO off 
title重命名脚本
set / A count = 1
:开始
cls
set / p STR = string to append:
cls
echo 1.在
之前追加echo 2.追加在
后set / p choice = I choose(1,2):
if%选择%== 1 goto renameb
如果%choice%== 2 goto renamea
:renameB
cls
echo在当前文件名之前追加'%STR%'。
echo。
set / A count = 0
FOR %% a in(* .jpg)DO(
ren%%〜a%STR %%%〜na %%〜xa
echo Was:%%〜a Became:%STR %%%〜na %%〜xa
set / A count + = 1

goto end
: renameA
cls
echo在当前文件名后附加'%STR%'。
echo。
set / A count = 0
FOR %% a in(* .jpg)DO(
ren%%〜a%%〜na%STR %%%〜xa
echo Was:%%〜a Became:%%〜na%STR %%%〜xa
set / A count + = 1

goto end
: end
echo [
echo%count%文件已重命名。
echo [
echo现在完成的过程。
pause

/ strong>, for 循环不起作用:

 由于某些原因,旧的CP / M中的FOR %% a(* .jpg)DO(

天的 FCB a for / f 循环:

  for / fdelims =%% a in('dir / ad / b * .jpg' )do(


I wrote this batch file to append some text to the filenames of a set of jpeg files, giving the option to append them before or after the current filename.

But for some odd reason when appending before the filename, one file is being proccessed twice getting the result as new_new_FileName.jpg while all other files are getting just new_FileName.jpg.

Interesting enough, this problem isn't happening always, as well as when appending after the filename it is always working fine.

Following is the entire code, with no visual difference between the before or after, but still resulting differently. Can anyone please examine this file and explain me where I'm wrong?

Help is appreciated.

@ECHO off
title Rename Script
set /A count=1
:Start
cls
set /p STR=choose a string to append:
cls
echo 1. Append before
echo 2. Append after
set /p choice=I choose (1,2):
if %choice%==1 goto renameb
if %choice%==2 goto renamea
:renameB
cls
echo Appending '%STR%' before current file name.
echo.
set /A count=0
FOR %%a in (*.jpg) DO (
    ren "%%~a" "%STR%%%~na%%~xa"
    echo Was: %%~a Became: %STR%%%~na%%~xa
    set /A count+=1
)
goto end
:renameA
cls
echo Appending '%STR%' after current file name.
echo.
set /A count=0
FOR %%a in (*.jpg) DO (
    ren "%%~a" "%%~na%STR%%%~xa"
    echo Was: %%~a Became: %%~na%STR%%%~xa
    set /A count+=1
)
goto end
:end
echo[
echo %count% files were renamed.
echo[
echo The process in now done.
pause

解决方案

for renaming, the simple for loop doesn't work:

FOR %%a in (*.jpg) DO (

for some reasons from the old CP/M days with the FCB's a for /f loop is needed:

for /f "delims=" %%a in ('dir /a-d /b *.jpg') do (

这篇关于对于在批处理文件中循环重命名一个文件两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆