1

I have some files in the form:

filename1 1 extra1.ext
filename1 2.ext
filename1 3 extra2.ext
...
filename2 1.ext
filename2 100 extra3.ext
...
filename20 1.ext
filename20 15 extra100.ext
(etc.)

...where filename1, filename2, etc., can contain spaces, symbol ' but not numbers. And extra1, extra2, etc, can contain anything. The number in the file name enclosed by spaces does not repeat per same filename1, filename2, etc.

What i want is to remove the extra things of the files that contain it. That is, to get from filename20 15 extra100.ext to filename20 15.ext

My first attempt is this:

@echo off
setlocal EnableExtensions EnableDelayedExpansion

set "FILE=file name 11 con sosas extras 2.txt"
set "ext=txt"
set "folder=."

for /F "tokens=1,* delims=0123456789" %%A in ("!FILE!") do (set "EXTRA=%%B")
set "FIRST=!FILE:%EXTRA%=!"
set "filename=!FIRST!.!ext!"
echo !EXTRA!
echo !filename!
echo rename "!folder!\!FILE!" "!filename!"

that seems to work, but if i change it to receive parameters, it doesn't:

@echo off
setlocal EnableExtensions EnableDelayedExpansion

set "FILE=%1"
set "ext=%2"
set "folder=%3"

for /F "tokens=1,* delims=0123456789" %%A in ("!FILE!") do (set "EXTRA=%%B")
set "FIRST=!FILE:%EXTRA%=!"
set "filename=!FIRST!.!ext!"
echo !EXTRA!
echo !filename!
echo rename "!folder!\!FILE!" "!filename!"

where %1 is the filename, %2 is the extension and %3 is the folder in which the files are. Probably, the extension can be extracted inside the batch, but i don't know how to do it.

On another hand, i plan to use this batch into another one. There, there will be a for loop in (*.txt) and i don't know how to differentiate between files that have extra things (and then call this batch) from files that doesn't (and then not call this batch).

Regards,

2
  • So the final file names should look like filename1.ext, according to your examples, right? so does the number enclosed within spaces need to be removed too? Commented Jan 18, 2017 at 17:37
  • it should be from filename1 15 extra100.ext to filename1 15.ext Commented Jan 18, 2017 at 18:31

2 Answers 2

2

use your method to extract the "extra-portion". In a second step, remove that extra-portion:

@echo off
setlocal enabledelayedexpansion

set "FILE=file name 11 con sosas extras 2.txt"

for /f "tokens=1,* delims=1234567890" %%a in ("%file%") do set new=!file:%%b=!%%~xb
echo %new%

%%~xb gives you the extension.

Sign up to request clarification or add additional context in comments.

1 Comment

I am afraid this fails in case the extra portion appears in the preceding file name part as well; in addition, some characters cause trouble: = and ~ (when at the beginning)...
1

Here is a batch script that seeks the first purely numeric string portion enclosed within SPACEs, or in case it appears at the end, preceded by a SPACE, that occurs after some other text not consisting of SPACEs only. The part in front of the found number followed by a SPACE followed by the number itself are used for building the new file name.

This approach handles all valid characters for file names properly, even ^, &, %, !, ( and ).

So here is the code:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_SOURCE=.\test"

for /F "eol=| delims=" %%F in ('
    dir /B "%_SOURCE%\*.ext" ^| findstr /R /I ^
        /C:"^..* [0123456789][0123456789]*\.ext$" ^
        /C:"^..* [0123456789][0123456789]* .*\.ext$"
') do (
    set "FILE=%%F"
    call :SPLIT FIRST NUM REST "%%~nF"
    if defined NUM (
        setlocal EnableDelayedExpansion
        ECHO rename "!_SOURCE!\!FILE!" "!FIRST! !NUM!%%~xF"
        endlocal
    )
)

endlocal
exit /B


:SPLIT  rtn_first  rtn_num  rtn_rest  val_string
    setlocal DisableDelayedExpansion
    set "RMD=" & set "NUM=" & set "STR=%~4"
    :LOOP
    for /F "tokens=1,2,* delims= " %%I in ("%STR%") do (
        if not "%%J"=="" (
            (for /F "delims=0123456789" %%L in ("%%J") do rem/) && (
                if not "%%K"=="" (
                    set "STR=%%J %%K"
                    goto :LOOP
                )
            ) || (
                set "NUM=%%J"
                if not "%%K"=="" (
                    set "RMD=%%K"
                )
            )
        )
    )
    set "STR=%~4"
    if not defined NUM goto :QUIT
    set "STR=%STR% "
    call set "STR=%%STR: %NUM% =|%%"
    for /F "delims=|" %%L in ("%STR:^^=^%") do set "STR=%%L"
    :QUIT
    (
        endlocal
        set "%~1=%STR%"
        set "%~2=%NUM%"
        set "%~3=%RMD%"
    )
    exit /B

After having tested the script, remove the upper-case ECHO command to actually rename any files.

2 Comments

thanks!! I modify it by adding the _SOURCE and extension as entry parameters. Can i run this script inside another one without issues? I prefer to keep this one in a separate file...
To have this script in a separate file, use the call command, then it should work just fine...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.