DOS批处理文件的首字母大写,并删除特殊字符 [英] Dos batch file titlecase and remove special characters

查看:205
本文介绍了DOS批处理文件的首字母大写,并删除特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Echo Off
SetLocal EnableDelayedExpansion
Set _ScrFldr=%~dp0
PushD %_ScrFldr%

:: You can add other extensions to do multiple file types
For /F "Delims=" %%A In ('Dir /A-D /B *.txt') Do (

:: The space after the = is needed so we can Proper Case the first word
Set _FileName= %%A

:: Remove (, replace ( with _, replace - with _-_ , 
:: replace _ with space, and a first pass at removing multiple spaces
For %%I In ("%%~xA=" ".= " ")=" "(=_" "-=_-_" "_= " "  = " **"!="**) Do Call Set 
"_FileName=%%_FileName:%%~I%%"

:: Now make it all lower case, and another pass at removing multiple spaces
For %%I In ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" "G=g" "H=h" "I=i" "J=j" "K=k" "L=l" "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r" "S=s" "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z" "  = ") Do Call Set "_FileName=%%_FileName:%%~I%%"

:: Proper Case and again remove multiple spaces
For %%I In (" a= A" " b= B" " c= C" " d= D" " e= E" " f= F" " g= G" " h= H" " i= I" " j= J" " k= K" " l= L" " m= M" " n= N" " o= O" " p= P" " q= Q" " r= R" " s= S" " t= T" " u= U" " v= V" " w= W" " x= X" " y= Y" " z= Z" "  = ") Do Call Set "_FileName=%%_FileName:%%~I%%"

:: Last check for multiple spaces
Call :RmLoop
Set _FileName=!_FileName: = !

:: Remove any trailing underscore
Set _FileName=!_FileName:_%%~xA=%%~xA!
Ren "%%A" "!_FileName:~1!%%~xA"
)
PopD

Goto :EOF

:::::::::::::::::::::::::::::::::::::::::::::::

:RmLoop
Call Set "_FileName=%%_FileName:  = %%"
Echo !_FileName!|Findstr /C:"  ">Nul
If !ErrorLevel!==0 Goto RmLoop


这是什么应该做的是从文件名和标题情况下,即文件


What this should do is remove certain characters from filenames and title case the files ie

some_filename = Some Filename
filename number 2 = Filename Number 2

它的大部分工作正常,如情况下更改和删除多余的空格和下划线

Most of it works fine such as case changing and removing extra spaces and underscores

但我有特殊字符,如麻烦!(10条线在code下)
如果一个文件名命名的文件,即!二是忽略

But i am having trouble with special characters such as ! (10 lines down in code) if a filename is named ie file! 2 it is ignored

使用^!= ^!=,但无济于事。

推荐答案

您不能推迟展开如果变量包含时启用展开像%%变量 ^ 。延迟的扩张将破坏的价值,因为一个被剥离,两个将间$ P $磅的字符作为一个变量并展开它(可能是没有),而 ^ 将被剥夺,因为它是用来逃跑!。

You cannot have Delayed Expansion enabled when you expand a FOR variable like %%A if the variable contains ! or ^. Delayed expansion will corrupt the value because a single ! is stripped, two ! will interpret the chars in between as a variable and expand it (probably to nothing), and ^ will be stripped because it is used to escape !.

您最简单的方法是顶级改为 SETLOCAL DisableDelayedExpansion ,并替换为 CALL指令%% VAR %% 的技巧,你已经使用在其他地方。该CALL诀窍是不是推迟扩张慢得多,所以通常我会建议延迟切换扩张。但我认为这种切换可能会在你的情况变得复杂。

Your easiest solution is change the top to SETLOCAL DisableDelayedExpansion, and replace all delayed expansion with the CALL command %%VAR%% trick that you are already using elsewhere. The CALL trick is much slower than delayed expansion, so normally I would recommend toggling delayed expansion. But I think the toggling could get complicated in your case.

我也建议你改变:RmLoop定义

I also suggest you change your :RmLoop definition

:RmLoop
Call Set "_FileName2=%%_FileName:  = %%"
If "%_FileName2%" neq "%_FileName%" (
  set "_FileName=%_FileName2%"
  goto :RmLoop
)

此外,我不明白为什么你本身你打电话后更换空间:RmLoop

Also I don't understand why you replace a space with itself after you call :RmLoop?

我不知道是什么 **!=** 替换正在尝试。也许这可以去掉?

I'm not sure what the **"!="** replacement is attempting. Perhaps that can be removed?

这篇关于DOS批处理文件的首字母大写,并删除特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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