Remove Last Characters from my filenames in windows

后端 未结 1 840
傲寒
傲寒 2021-01-14 11:18

Im quite new to batch programming and i wanted to remove the last characters on my filename.

10_myfile_12345_6789.txt
11_myfile_12345_0987.txt
1条回答
  •  感情败类
    2021-01-14 11:54

    With your recent clarification - I would do the following.

    @echo off
    setlocal enabledelayedexpansion
    set FOLDER_PATH=C:\Some\Path\
    for %%f in (%FOLDER_PATH%*) do if %%f neq %~nx0 (
        set "filename=%%~nf"
        ren "%%f" "!filename:~0,-4!%%~xf"
    )
    PAUSE
    

    This will change your examples

    10_myfile_12345_6789.txt
    11_myfile_12345_0987.txt
    

    Into

    10_myfile_12345_.txt
    11_myfile_12345_.txt
    

    If you want to remove the trailing _ simply change !filename:~0,-4! to !filename:~0,-5!. This is simple string manipulation.

    0 讨论(0)
提交回复
热议问题