批处理文件:移动文件,如果文件已经存在,则通过数字顺序(LOOP)重命名文件

时间:2018-11-23 06:43:58

标签: batch-file

我已经找到了解决方法,但是现在我需要找到最短的命令行。 情况:文件“ C:\ Image.jpg”移至“ D:\”。如果“ D:\”中存在“ Image.jpg”,则重命名并将其移动为“ Image01.jpg”。此命令循环。 这是我的批处理命令行:

:start
IF EXIST "D:\Users\00002829\Pictures\Image.jpg" IF EXIST "D:\Users\00002829\Pictures\SFSAPP\VChip_X-Ray\Image03.jpg" (
    move "D:\Users\00002829\Pictures\Image.jpg" "D:\Users\00002829\Pictures\SFSAPP\VChip_X-Ray\Image04.jpg"
)
IF EXIST "D:\Users\00002829\Pictures\Image.jpg" IF EXIST "D:\Users\00002829\Pictures\SFSAPP\VChip_X-Ray\Image02.jpg" (
    move "D:\Users\00002829\Pictures\Image.jpg" "D:\Users\00002829\Pictures\SFSAPP\VChip_X-Ray\Image03.jpg"
)
IF EXIST "D:\Users\00002829\Pictures\Image.jpg" IF EXIST "D:\Users\00002829\Pictures\SFSAPP\VChip_X-Ray\Image01.jpg" (
    move "D:\Users\00002829\Pictures\Image.jpg" "D:\Users\00002829\Pictures\SFSAPP\VChip_X-Ray\Image02.jpg"
)
IF EXIST "D:\Users\00002829\Pictures\Image.jpg" (
    move "D:\Users\00002829\Pictures\Image.jpg" "D:\Users\00002829\Pictures\SFSAPP\VChip_X-Ray\Image01.jpg"
)
TIMEOUT /t 3
goto start

此命令仅限于3次文件重命名和移动。 谢谢

1 个答案:

答案 0 :(得分:0)

只需循环查找所有文件,将其复制并重命名(如果存在)。

@echo off
setlocal disableDelayedExpansion
set "source=D:\Users\00002829\Pictures"
set "target=D:\Users\00002829\Pictures\SFSAPP\VChip_X-Ray"

md "%target%"
set /a cnt=0
for /r %%F %target% in (Image.jpg) do if "%%~dpF" neq "%target%\" (
  if exist "%%F" (
  if exist "%target%\%%~nxF" (
    set /a cnt+=1
    set "full=%%F"
    set "name=%%~nF"
    set "ext=%%~xF"
    setlocal enableDelayedExpansion
    copy "!full!" "!target!\!name!!cnt!!ext!" >nul
    endlocal
  ) else copy "%%F" "%target%" >nul
 )
)