Search code examples
batch-filewindows-10

Batch remove part of string after “-1” is found or any other number “-[0-9]”


I got a file containing a string on each line like this:

fruit-apple-1.5.6
vegtable-sla-mc5-6.5-16515
extra-huh-9.5-511-515
extra-3.2

I am iterating over it and want it to remove the part of the string on the right after in find "-" + any number "-0","-1","-2","-9",...

so output should be

fruit-apple
vegtable-sla-mc5
extra-huh
extra

this is code i have but it only works with a "-" i cant combine it so it takes "-" + any number like "-1","-5","-2",...

for /f "delims=|" %%A in ("!fileNameCheck:-=|!") do (
echo stripped string = %%A
)

complete code not necessary i think but in case u need it below

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set "RawPath=%~dp0"


FOR /F "USEBACKQ TOKENS=*" %%M IN ("%RawPath%/mods") DO (
REM for %%f in (*.jar) do (
    Set "fileNameCheck=%%M"
    for /f "delims=|" %%A in ("!fileNameCheck:-=|!") do (
        Echo [46m%%A[0m
        if exist "%~dp0%%A*.jar" (
            REM echo [32mFound %%A "%~dp0%%A*.jar"[0m
            if exist "%~dp0%%M" (
                REM echo [42mUp to Date[0m [32m%%A "%~dp0%%M"[0m
            ) else (
                for %%j in (*.jar) do (
                    echo %%j |find "%%A" >nul
                    if not errorlevel 1 (
                        echo [41mDifferent Version[0m [31m%%j [0m[90mNewer version[0m [32m%%M[0m 
                    )
                    
                )
                
            )
        ) else (
            REM echo [31mMissing %%A[0m
        )
    )
)
pause

Solution

  • Given that the strings do not contain any of the caracters ?, *, < and >, the following script should do the trick:

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    
    rem // Define constants here:
    set "_ROOT=%~dp0."
    set "_FILE=mods.txt"
    
    rem // Read file line by line:
    for /F usebackq^ delims^=^ eol^= %%L in ("%_FILE%") do (
        rem // Store current line, initialise variables:
        set "LINE=%%L" & set "PREV=-" & set "COLL=-"
        setlocal EnableDelayedExpansion
        rem /* Split line string at every `-` and iterate through items;
        rem    (`?`, `*`, `<`, `>` must not occur in the string!): */
        for %%I in ("!LINE:-=" "!") do (
            endlocal & set "ITEM=%%~I"
            rem // Check whether item only consists of numerals and `.`:
            (
                rem // This loop executes when there are other characters:
                for /F "delims=0123456789. eol=." %%J in ("%%~I") do (
                    setlocal EnableDelayedExpansion
                    rem /* Rebuilt string with currently found items; if there
                    rem    are other items following numeric ones, keep them: */
                    for /F "delims=" %%K in ("!COLL!!PREV:~1!-!ITEM!") do (
                        endlocal & set "COLL=%%K" & set "PREV=-"
                    )
                )
            ) || (
                rem // This section runs when there are numerals and `.`:
                setlocal EnableDelayedExpansion
                for /F "delims=" %%K in ("!PREV!-!ITEM!") do (
                    rem /* Store numeric items in a separate buffer; if there
                    rem    are no other items following, this is dismissed: */
                    endlocal & set "PREV=%%K"
                )
            )
            setlocal EnableDelayedExpansion
        )
        rem // Return rebuilt line string:
        echo(!COLL:~2!
        endlocal
    )
    
    endlocal
    exit /B
    

    The example input file mods.txt:

    fruit-apple-1.5.6
    vegtable-sla-mc5-6.5
    extra-huh-9.5
    extra-3.2
    test-9.15.5
    keep-forge
    name-subname-10.5-55.5
    globalxp-1.16.5
    mix-1.2-string-10.5-55.5-more
    mix-1.2-string-10.5-55.5-more-3.4
    mix-1.2-string-10.5-55.5-more-3.4-5.6-7
    8.9
    1.2.3-lead
    1.2.3-4.5.6-7.8.9-lead-mult
    

    leads to the following output:

    fruit-apple
    vegtable-sla-mc5
    extra-huh
    extra
    test
    keep-forge
    name-subname
    globalxp
    mix-1.2-string-10.5-55.5-more
    mix-1.2-string-10.5-55.5-more
    mix-1.2-string-10.5-55.5-more
    
    1.2.3-lead
    1.2.3-4.5.6-7.8.9-lead-mult