bat如何實現圖片與名字匹配重命名


背景:有一批圖片按順序截取,需要按照規定的名稱進行重名命。

問題:用批處理怎么實現呢?(公司電腦手動重名時,卡的不要不要的)

No1:解決:將規定的名稱放入criterion.txt中,將批處理Rename.bat文件、criterion.txt以及要處理的圖片放到同一目錄下,運行bat文件即可。

步驟:1,先創建一個criterion.txt,將規定的名稱放入文本中

2,將以下代碼放入Rename.bat文件中

@echo off&setlocal enabledelayedexpansion
title ::rename tool
color 0a
dir /b *.png >png.txt
(for /f %%i in (png.txt) do (
   rem echo %%~ni
    set /p newName=
    echo  %%~ni.png !newName!.png
))<criterion.txt

pause

 3,將需要重命名的圖片放到該目錄下。運行bat文件。

注:若重命名時,命名出現空格,需使用雙引號引起來。

==================================分割線===================================

 No2:以上是最初的一個需求。重命名的規范中前綴是需要變動的(按不同的code命名),而且要將改好的放入文件夾中。直接上代碼:

@echo off&setlocal enabledelayedexpansion
title ::rename tool
color 0a
rem +++++++++++++++++++++++++++++++++++++++++++
rem   OneKeyRename.bat
rem          By zhzw @2017/5/4
rem   
rem            Version: 1.2
rem +++++++++++++++++++++++++++++++++++++++++++

set /p folderName=InputFolderName:
set /p code=InputCountryCode:
for /f "delims=- tokens=2" %%i in (criterion.txt) do (
    set "update=%code%_%%i"
    echo !update!>>temp.txt
)
rem 也可以這樣寫
rem for /f "delims=" %%a in ('dir /b "%~dp0*.png"') do (echo %%a)
(for /r %cd% %%j in (*.png) do (
    set /p newName=
    ren "%%~nj.png" "!newName!.png"
))<temp.txt

del %cd%\temp.txt
rem md or mkdir
mkdir "%cd%\%folderName%"
move "%cd%\*.png" "%cd%\%folderName%" 
echo OK !
pause

 ==================================分割線===================================

No3:在上次的代碼上進行優化,文件夾的名字也需要按照規定的命名(name.txt)。且名字中含有code.

@echo off&setlocal enabledelayedexpansion
title ::rename tool
color 0a
rem +++++++++++++++++++++++++++++++++++++++++++
rem   OneKeyRename.bat
rem          By zhzw @2017/5/20
rem   
rem            Version: 1.3
rem +++++++++++++++++++++++++++++++++++++++++++

set /p code=InputCountryCode:
rem ping 127.0.0.1 -n 2 >nul
rem findstr "CN" name.txt | cmd /v /c "set /p var=&echo !var!"
:search
findstr "/c:-%code%" name.txt 
rem 簡單理解為 0表示成功,1表示失敗
if not %errorlevel%== 0 (
    echo Not exist ! &goto :end
) else (
    for /f %%i in ('findstr "/c:-%code%" name.txt') do (
        echo Create folder ...
        if exist %%i (
            echo The folder exist !
        ) else (
            mkdir "%cd%\%%i"
        )
        echo Rename files ...
        rem call調用后,%%i處於關閉狀態,先保存%%i再使用%1調用
        rem 或者在call前面set一個變量來保存%%i,然后再來調用這個變量  
        call :rename %%i
        :move
        echo Move files ...
        del "%cd%\temp.txt"
        move "%cd%\*.png" "%cd%\%1" 1>nul        
        echo OK
        goto :end
    )
)
:rename
for /f "delims=- tokens=2" %%j in (criterion.txt) do (
    set "update=%code%_%%j"
    echo !update!>>temp.txt
)
( for /r %cd% %%k in (*.png) do (
    rem 使用call時,需要注意循環。可能會出現死循環,需要跳出for循環 
    set /a a+=1
    set /p newName=
    ren "%%~nk.png" "!newName!.png"
    if !a! == 11 goto :move    
))<temp.txt
:end
timeout /t 2 &exit
pause  

  ==================================分割線===================================

No4.來個最終版本,從android設備中提取圖片(存在9.10.11張圖才會進行重命名),從name.txt中獲取folder名字並創建folder,且把重命名的文件放入folder中.當然相應的也需要稍微修改criterion.txt中的數據.(  ^S*.*g   注意不需要加雙引號,匹配Screenshots.png 或者Screenshots.jpg等的圖片)

修改后的criterion.txt:

 

@echo off&setlocal enabledelayedexpansion
title Import-and-Rename
color 0a
@mode con lines=35 cols=75
:comment
set a=0
set b=0
set c=0
cls
color 0a
rem +++++++++++++++++++++++++++++++++++++++++++
rem   OneKeyImportRename.bat
rem          By zhzw @2017/5/22
rem   
rem            Version: 1.4
rem +++++++++++++++++++++++++++++++++++++++++++
echo.
echo                              注意事項!!!
echo.
echo 1.截圖必須為criterion.txt對應的順序
echo.
echo 2.打開USB,連接設備,勾選總是允許USB調試.輸入code(如:US)即可.
echo.
echo 3.若運行中出現異常請根據提示操作(不要操作criterion.txt,name.txt文件).
echo.
echo ==================================== end ================================

rem ======================================================================
:input
color 0a
set /p code=InputCountryCode:
:search
findstr "/c:-%code%" name.txt >nul
if not %errorlevel%== 0 (
    color 0d
    echo Not exist. Please reenter
    ping 127.0.0.1 -n 3 >nul
    goto :input
) else (
    for /f "delims=" %%i in ('findstr "/c:-%code%" name.txt') do (
        set "i=%%i"
        echo.
        if not exist %cd%\%%i (
            echo Create folder ...
            echo folder name = %%i
            mkdir "%cd%\%%i"
            echo Create successful!
        ) else ( 
            echo Create folder ...
            echo This folder already exists!
            echo Do you want to overwrite this folder ? 
            echo "Yes" Please press Enter Key to continue.
            echo "NO"  Please close this window.
            pause >nul
        ) 
        rem 因為%%i中存在空格.傳遞參數時,會以空格為准,出現多個參數.
        rem call :rename %%i
        rem :move
        rem echo %1
        rem ---------------------------------------------------
        call :rename 
        :move
        echo Rename successful!
        del "%cd%\temp.txt"
        echo Move files ...
        move "%cd%\*.png" "%cd%\!i!" 1>nul
        echo.
        echo It's OK
        goto :end1
    )
)
rem ========================================================================
:rename
adb devices|findstr "device$" 1>nul 2>nul
if not %errorlevel%== 0 (
    echo Devices not connected !
    echo Please check whether the USB is turned on.
    goto :end2
)
echo Import screenshots into the current folder ...
rem You need to pay attention to the version of adb
rem adb pull /sdcard/DCIM/Screenshots/ "%cd%" 1>nul 2>nul
adb pull /sdcard/Pictures/Screenshots/ "%cd%" 1>nul 2>nul
echo.
ping 127.0.0.1 -n 3 >nul
if not exist %cd%\^S*.*g (
    echo Import pictures failure !
    echo Please check the "allow the debug?" whether always allow?&goto :end2
) 
rem Count the number of pictures.
for /f %%i in ('dir /b %cd%\^S*.*g') do (
    set /a a+=1
)
if %a% equ 9 (
    echo Successfully import %a% pictures !
    rem Modify code and Transfer new names to temp.txt
    for /f "eol=. delims=- tokens=2" %%j in (criterion.txt) do (
        set "update=%code%_%%j"
        echo !update!>>temp.txt
    )
    echo Rename files ...
    rem Using adb pull pictures is ascending sort 
    rem Using /r or /f is descending sort (dir /b /od)
    ( for /r %cd% %%k in (^S*.*g) do ( 
        set /a b+=1
        set /p newName=
        echo %%~nxk --^>"!newName!.png"
        ren %%~nxk "!newName!.png"
        if !b! == %a% goto :move    
    ))<temp.txt    
) else (
    if %a% equ 10 (
        echo Successfully import %a% pictures !
        for /f "delims=- tokens=2" %%j in (criterion.txt) do (
            set /a c+=1
            if !c! == 11 (
                set update=""
            ) else (
                set update=%code%_%%j
                echo !update!>>temp.txt
            )            
        )
        echo Rename files ...
        ( for /r %cd% %%k in (^S*.*g) do ( 
            set /a b+=1
            set /p newName=
            echo %%~nxk --^>"!newName!.png"
            ren %%~nxk "!newName!.png"
            if !b! == %a% goto :move    
        ))<temp.txt    
    ) else (
        if %a% equ 11 (
            echo Successfully import %a% pictures !
            for /f "delims=- tokens=2" %%j in (criterion.txt) do (
                set update=%code%_%%j
                echo !update!>>temp.txt
            )
            echo Rename files ...
            ( for /r %cd% %%k in (^S*.*g) do (
                set /a b+=1
                set /p newName=
                echo %%~nxk --^>"!newName!.png"
                ren %%~nxk "!newName!.png"
                if !b! == %a% goto :move    
            ))<temp.txt
        ) else (
            color 0d
            echo Successfully import %a% pictures !
            echo The total number of pictures is error!
            echo Please check the total number of pictures.&goto :end2
        )
    )
)
rem =========================================================================
:end1
timeout /t 5 &goto :comment
:end2
echo Please operate according to the prompt.
timeout /t 10 &goto :comment
pause
View Code


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM