Batch Rename Script Help!


  1. Posts : 1
    Windows 10
       #1

    Batch Rename Script Help!


    Hello!

    I am very new to this and need a little help. I am trying to create a batch file that will rename files that are called png_X.png (the X being the number page it is). The code I have is below. It works fine with out the pipe sort command but it sorts the by the ones place first and then by the tens. I tried putting the pipe sort command in to tell it how to see the file names but it doesn’t run at all with that command. Is my line wrong somehow with the pipe command?

    @echo off
    SETLOCAL ENABLEDELAYEDEXPANSION
    set i=1
    cd C:\Users\Desktop\Visio Stuff\File Rename\Temp Picture File
    for /f %%f in ('dir /b .\ | sort /+10') do (
    echo renaming "%%f" to "!i!.jpg"
    ren "%%f" "!i!.jpg"
    set /A i=!i!+1
    )
    ENDLOCAL
    set "i="
      My Computer


  2. Posts : 989
    Microsoft Windows 10 Home
       #2

    I've always avoided batch like the plague. Did my XP automation in VBS. With Win 10, I finally learned PowerShell and that's what you should be using for WIndows 10 automation.

    It would be helpful if you edited your question to include sample data. I'm guessing the problem lies in the numberical portion varies in width, lacking leading zeros and thus sorting correctlly in Explorer but not in a pure ascii sort. If that's not correct, please clarify with examples.

    If I undersand correctly, the pseudo-code is:
    1. Get files matching pattern "png_#.png", "png_##.png", "png_###.png", etc. where # is a digit.
    2. Sort by the number found in the filename.
    3. Rename with a digit corresponding to its sort position.


    In PowerShell, that would look like this:
    Code:
    [ref]$i = 1
    gci | ? Name -match '^png_(\d+)\.png' |
        Select @{ N = 'Path' ; E = { $_.FullName }},
                @{ N = 'Order' ; e = { [Int]$matches[1] }} |
     Sort Order  | Rename-Item -NewName { '{0}.jpg' -f $i.Value++ }
    gci is an alias for Get-ChildItem
    ? is an alias for Where-Object
    Select-Object
    Sort-Object
    Rename-Item
    About Ref
      My Computer


  3. Posts : 3,275
    Win10
       #3

    Just in case you still insist on using batch commands:

    Does escaping the pipe with ^ work ?
    for /f %%f in ('dir /b .\ ^| sort /+10') do (
    instead of
    for /f %%f in ('dir /b .\ | sort /+10') do (
      My Computers


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 13:50.
Find Us




Windows 10 Forums