在Windows中从文件名中删除最后一个字符 [英] Remove Last Characters from my filenames in windows

查看:165
本文介绍了在Windows中从文件名中删除最后一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始批量编程,我想删除文件名中的最后一个字符.

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

我想删除文件名中的最后4位数字吗?

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

我已经尝试过了

@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

推荐答案

最近的澄清-我将执行以下操作.

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

这将改变您的示例

10_myfile_12345_6789.txt
11_myfile_12345_0987.txt

进入

10_myfile_12345_.txt
11_myfile_12345_.txt

如果要删除结尾的_,只需将!filename:~0,-4!更改为!filename:~0,-5!.这很简单字符串操作.

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

这篇关于在Windows中从文件名中删除最后一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆