Remove Last Characters from my filenames in windows

☆樱花仙子☆ 提交于 2019-12-30 10:33:48

问题


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

I want to remove the last 4 digits on my filename how i could do that?

I have tried this

@echo off
setlocal enabledelayedexpansion
set X=3
set FOLDER_PATH=
pushd %FOLDER_PATH%
for %%f in (*) do if %%f neq %~nx0 (
    set "filename=%%~nf"
    ren "%%f" "!filename!%%~xf"
)
popd
PAUSE

but it removes on first and last characters, i only saw this here too, im still quite confused how this works


回答1:


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.



来源:https://stackoverflow.com/questions/27931611/remove-last-characters-from-my-filenames-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!