欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

文件名后缀添加删除.bat

程序员文章站 2022-07-12 23:48:57
...

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

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

::吃了没文化的亏。。。延迟环境变量%var%,实时环境变量!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

相关标签: littleTools batch