0

I have multiple files to rename

Eg:
     ERJP00300029 3.13.jpg
     SRKP00100002 12.04.jpg
     ERJP01500001 ERJP12 10.5.jpg

I want to rename above file as

Eg:
    ERJP00300029.jpg
    SRKP00100002.jpg
    ERJP01500001.jpg

How to keep first part name alone.

I used below code but I can't get my exact solution. Where I am mistake.

@ECHO ON

SETLOCAL ENABLEDELAYEDEXPANSION
SET SourceDir=F:\Ezhilarasan\Exibt\need_upload\ERHP\check
FOR /G "TOKENS=1-3 DELIMS=. " %%G IN ('DIR /B /A-D "%SourceDir%\*.jpg"') DO (
    SET "part1=%%~F" 
    SET "part2=%%~G" 
    SET "part3=%%~H"
    REN "!part1! \%SourceDir%!part2!.!part3!" "!part1!.!part3!"
)
GOTO EOF

2 Answers2

0

Well, there’s several things happening:

  1. Is your system set up to use a language other than English?  On my (English-based) system, the command that you are trying to do is FOR /F, not FOR /G.
  2. You’re extracting three (1-3) tokens starting with %%G, but then you’re using %%F, %%G, and %%H.  Nothing is being put into %%F.  Either change the FOR statement to refer to %%F, or change the loop body to use %%G, %%H, and %%I.  Except …
  3. … don’t do that.  Your approach is flawed.  Think about what it means to have period (.) and space as delimiters.  Your filenames get broken into tokens like this:

                                         #1              #2        #3    #4     #5
    ERJP00300029 3.13.jpg           →    ERJP00300029    3         13    jpg
    SRKP00100002 12.04.jpg          →    SRKP00100002    12        04    jpg
    ERJP01500001 ERJP12 10.5.jpg    →    ERJP01500001    ERJP12    10    5      jpg

    so,

    • the filename is not always equal to part1 part2.part3,
    • the extension not always the third token, and, in fact,
    • for your example filenames, it’s never the third token, and
    • it isn’t always the same token number.


    We’ll have to get back to this.

  4. What do you think you’re doing with the REN (RENAME) command?  Specifically, why are you putting \%SourceDir% between part1 and part2?  The only hope you have of making this work is to put %SourceDir%\ before part1.  And note that the backslash (\) needs to come after %SourceDir%.
  5. If this is the complete batch file, you don’t need to do anything special at the end.  The batch file will exit when it reaches the end.  But, if this is part of a larger system, you might want to have an explicit terminating command at the end.  That command should be GOTO :EOF, not GOTO EOF.

Not to pile on, but your approach of taking the output from DIR and immediately breaking it down in tokens is flawed.  Looking at your example names, the string ERJP12 10.5 gets broken down into three tokens: ERJP12, 10, and 5.  But ERJP12.10.5 would get broken down into the same tokens, and so would ERJP12 10 5 and ERJP12.10 5.  You can’t reconstruct the original (i.e., current, existing) filenames from the tokens; you need to capture the original filename intact, and then break it down.

I don’t see why you need to run DIR.  All you need is FOR %%F IN ("%SourceDir%\*.jpg") DO; it will give you the names of files only (no subdirectories), and, unlike the output of DIR, it will include the name of the source directory (the directory that contains the files).  Now that we have the complete current file pathname in %%F, we can start to break it down.  Now we use FOR /F.  So, take the file name portion of %%F, and pull out a token that is the first part alone.  Then rename the original full pathname to the first part plus the extension.  Here’s the complete batch file:

SET SourceDir=F:\Ezhilarasan\Exibt\need_upload\ERHP\check
FOR %%F IN ("%SourceDir%\*.jpg") DO (
    FOR /F "DELIMS=. " %%A IN ("%%~nF") DO (
        REN "%%F" "%%A%%~xF"
    )
)
0

While I agree with every of Scott's very detailed explanations
in this special case a simple

@Echo On
Pushd F:\Ezhilarasan\Exibt\need_upload\ERHP\check
FOR /F  %%G IN ('DIR /B /A-D "*.jpg"') DO REN "%%G*.jpg" %%G.jpg
PopD

would do equally.
The default is for /f "tokens=1 delims= " The dir returns only files with the ext jpg so we can use it literally in the rename.

LotPings
  • 7,391