文件名后缀添加删除.bat

这是一个批处理脚本,用于在当前目录及其子目录中批量处理文件名,可以添加或删除文件名末尾的指定字符。用户输入命令(添加或删除)和要操作的后缀名,脚本将执行对应操作。例如,可以将所有文件名后缀为'.txt'的文件更改为'.txt_copy'。请注意,此脚本可能存在bug,使用时需谨慎。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0. 功能:实现当前目录及子目录中文件名末尾字符的添加与删除

1. 提示:脚本仅供参考,已优化一版,应该仍有bug,谨慎使用!

::吃了没文化的亏。。。延迟环境变量!var!
@echo off
setlocal enabledelayedexpansion
goto start

::**********help start**********
:help
echo **********************************Help************************************** 
echo Lpaper		2021/08/29		v1.1		fixed bugs for file name space
echo Lpaper		2021/08/29		v1.0		created
echo this bat if for the postfix name of file (include which in subfolder.) handel
echo arg1 for cmd, arg2 for add or del postfix name
echo eg. 	there has a file named "example.txt" 
echo     	arg1=add, arg2=_copy will change the file name to "example.txt_copy"
echo=	 
echo any wrong format input will show this help, enjoy^^!
echo **************************************************************************** 
goto start
::**********help end**********

::**********start**********
:start
set /p cmd=command(add/del):
set /p tail_name=name(add/del):
set myName=%~f0
::set myName=!myName:~1,-1!
if %cmd%==add (
	goto add
) else (
	if %cmd%==del (
		goto delete
	) else (
		goto help
	)
)

::**********add start**********
:add
	for /f "delims=" %%i in ('dir /s /b /a-d') do (
		if not %%i==!myName! (
			set str=%%i%tail_name%
			move /y "%%i" "!str!"
		)
	)

goto end
::**********add end**********

::**********delete start**********
:delete
::count the lenth of %tail_name%
	:length
		set /a len+=1
		if not "!tail_name:~%len%!"=="" goto :length

	for /f "delims=" %%i in ('dir /s /b /a-d') do (
		set str=%%i
		if not %%i==!myName! (
			set tail=!str:~-%len%,%len%!
			if !tail!==!tail_name! (
				set str=!str:~0,-%len%!
				move /y "%%i" "!str!"
			)
		)
	)
::**********delete end**********

:end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值