
标题: [文件操作] [已解决]批处理如何去除文件名中的指定字符? [打印本页]
作者: temp 时间: 2010-12-17 12:50 标题: [已解决]批处理如何去除文件名中的指定字符?
最近经常把*.reg文件转换成*.inf文件,生成的新文件名是*.reg.inf,过去是手工去掉.inf前面的.reg,现在希望能用批处理的办法解决
如:“禁止运行.reg.inf”,用批处理删除其中的“.reg”,最后变成“禁止运行.inf”
从网上搜了好几天了,一直没找到解决方案,特来此请教(不要用第三方软件,也不要用vbs)
谢谢!
[ 本帖最后由 temp 于 2010-12-18 23:27 编辑 ]
作者: Batcher 时间: 2010-12-17 13:24
你是用哪个工具把reg转换成inf的?那个工具里面不能设置新文件名?
作者: temp 时间: 2010-12-17 18:32
原帖由 Batcher 于 2010-12-17 13:24 发表 
你是用哪个工具把reg转换成inf的?那个工具里面不能设置新文件名?
我用的是reg2inf.com,用批处理调用,这几天改进了调用代码,不用输入文件名了- @echo off
- title= 测试,去处文件名中的特定字符
- %~d1
- cd %~p1
- set "file=%~nx1%"
- set "hole=%~x1%"
- if 0%hole% equ 0.reg goto run
- :run
- "%~dp0reg2inf.com" "%file%">nul
复制代码
作者: Batcher 时间: 2010-12-17 19:07
- @echo off
- for %%a in (*.reg.inf) do (
- for %%h in ("%%~na") do (
- ren "%%a" "%%~nh.inf"
- )
- )
复制代码
作者: temp 时间: 2010-12-17 19:28
原帖由 Batcher 于 2010-12-17 19:07 发表 
@echo off
for %%a in (*.reg.inf) do (
for %%h in ("%%~na") do (
ren "%%a" "%%~nh.inf"
)
)
太感谢了,谢谢!
经测试达到了要求,更名成功!
待改日在我个人的系统中测试成功后再标注已解决字样
作者: temp 时间: 2010-12-18 23:27
原帖由 Batcher 于 2010-12-17 19:07 发表 
@echo off
for %%a in (*.reg.inf) do (
for %%h in ("%%~na") do (
ren "%%a" "%%~nh.inf"
)
)
谢谢!
在xp sp3 和2k3 sp2 环境下测试成功
作者: hanyeguxing 时间: 2010-12-19 00:52
- @echo off&setlocal enabledelayedexpansion
- for %%a in (*.reg.inf) do (
- set a=%%a
- ren "%%a" "!a:~0,-7!inf"
- )
复制代码
[ 本帖最后由 hanyeguxing 于 2010-12-19 00:55 编辑 ]
作者: temp 时间: 2010-12-19 01:08
原帖由 hanyeguxing 于 2010-12-19 00:52 发表 
@echo off&setlocal enabledelayedexpansion
for %%a in (*.reg.inf) do (
set a=%%a
ren "%%a" "!a:~0,-7!inf"
)
谢谢!
改名成功~~~
作者: vigiles 时间: 2010-12-20 18:28 标题: 请出个清晰地教程
各位老师,一帖又一帖,么意思啊?讲明白点...
作者: temp 时间: 2010-12-20 21:41
原帖由 vigiles 于 2010-12-20 18:28 发表 
一帖又一帖,么意思啊?讲明白点...
举个转移"我的文档"到非系统盘的例子,注册表文件如下(已经改为d盘),导入到注册表后需要重启系统才能生效:
-
- Windows Registry Editor Version 5.00
-
- [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders]
- "Personal"=hex(2):64,00,3a,00,5c,00,44,00,6f,00,63,00,75,00,6d,00,65,00,6e,00,\
- 74,00,73,00,20,00,61,00,6e,00,64,00,20,00,53,00,65,00,74,00,74,00,69,00,6e,\
- 00,67,00,73,00,5c,00,41,00,64,00,6d,00,69,00,6e,00,69,00,73,00,74,00,72,00,\
- 61,00,74,00,6f,00,72,00,5c,00,4d,00,79,00,20,00,44,00,6f,00,63,00,75,00,6d,\
- 00,65,00,6e,00,74,00,73,00,00,00
复制代码
把reg文件转换成inf类型的文件后,内容如下,简单明了
:- [Version]
- Signature = "$Windows NT$"
-
- [DefaultInstall_Modift]
- AddReg=_AddReg
- [_AddReg]
- HKCU,"%Modift%","Personal",0x20000,"d:\Documents and Settings\%USERNAME%\My Documents"
- [Strings]
- Modift=Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
复制代码
inf最大的用途是做绿色软件,以下是office2003绿色版绿化的inf文件(不运行它系统无法识别)
-
- [Version]
- Signature="$CHICAGO$"
- [DefaultInstall]
- ; DelReg=_DelReg
- AddReg=_AddReg
- [_DelReg]
- [_AddReg]
- ;===================取消工具栏延迟==============================
- HKCU,"Software\Microsoft\Office\11.0\Common\Toolbars","AdaptiveMenus",0x10001,00,00,00,00
- ;=========================================================================
- HKCU,"Software\Microsoft\Office\11.0\PowerPoint\Options","StartupDialog",0x10001,00,00,00,00
- HKCU,"Software\Microsoft\Office\11.0\PowerPoint\First Run","FirstRun",0x10001,01,00,00,00
- HKCU,"Software\Microsoft\Office\11.0\Word\Options","FirstRun",0x10001,01,00,00,00
- HKCU,"Software\Microsoft\Office\11.0\Word\Options","StartupDialog",0x10001,00,00,00,00
- HKCU,"Software\Microsoft\Office\11.0\Excel\Options","FirstRun",0x10001,01,00,00,00
- HKCU,"Software\Microsoft\Office\11.0\Excel\Options","StartupDialog",0x10001,00,00,00,00
- HKLM,"SOFTWARE\Microsoft\Office\11.0\Common\InstallRoot","InstallCount",0x10001,01,00,00,00
- HKLM,"SOFTWARE\Microsoft\Office\11.0\Common\InstallRoot","Path",,"%01%\"
- HKLM,"SOFTWARE\Microsoft\Office\11.0\Common\FilesPaths","mso.dll",,"%01%\MSO.DLL"
- HKLM,"SOFTWARE\Microsoft\Office\11.0\Registration\{90110804-6000-11D3-8CFE-0150048383C9}","DigitalProductID",0x1,a4,00,00,00,03,00,00,00,37,33,39,34,31,2d,36,34,30,2d,\
- 30,30,30,30,31,30,36,2d,35,37,39,39,35,00,72,00,00,00,31,32,33,2d,31,32,33,\
- 34,35,00,00,00,00,00,00,00,00,35,4e,11,05,b8,18,0a,8a,53,ae,4c,0c,2e,01,00,\
- 00,00,00,00,bb,5d,71,45,41,46,0d,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\
- 00,00,00,00,00,00,00,00,00,00,00,32,34,38,30,30,00,00,00,00,00,00,00,00,00,\
- 00,00,ec,11,16,44,00,02,00,00,fa,17,00,00,00,00,00,00,00,00,00,00,00,00,00,\
- 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,de,8f,3a,91
-
- ;===================word================================================
- HKCU,"Software\Microsoft\Office\11.0\Common\Licensing","1EBDE4BC9A514630B5412561FA45CCC5",0x1,01,00,00,00,27,00,00,00,7b,39,30,31,31,\
- 30,38,30,34,2d,36,30,30,30,2d,31,31,44,33,2d,38,43,46,45,2d,30,31,35,30,30,\
- 34,38,33,38,33,43,39,7d,00,56,00,00,00,4d,00,69,00,63,00,72,00,6f,00,73,00,\
- 6f,00,66,00,74,00,20,00,4f,00,66,00,66,00,69,00,63,00,65,00,20,00,50,00,72,\
- 00,6f,00,66,00,65,00,73,00,73,00,69,00,6f,00,6e,00,61,00,6c,00,20,00,45,00,\
- 64,00,69,00,74,00,69,00,6f,00,6e,00,20,00,32,00,30,00,30,00,33,00,00,00
- ;=========================================================================
- ;===================excel================================================
- HKCU,"Software\Microsoft\Office\11.0\Common\Licensing","A2B280D420FB472099F740C09FBCE10A",0x1,01,00,00,00,27,00,00,00,7b,39,30,31,31,\
- 30,38,30,34,2d,36,30,30,30,2d,31,31,44,33,2d,38,43,46,45,2d,30,31,35,30,30,\
- 34,38,33,38,33,43,39,7d,00,56,00,00,00,4d,00,69,00,63,00,72,00,6f,00,73,00,\
- 6f,00,66,00,74,00,20,00,4f,00,66,00,66,00,69,00,63,00,65,00,20,00,50,00,72,\
- 00,6f,00,66,00,65,00,73,00,73,00,69,00,6f,00,6e,00,61,00,6c,00,20,00,45,00,\
- 64,00,69,00,74,00,69,00,6f,00,6e,00,20,00,32,00,30,00,30,00,33,00,00,00
- ;=========================================================================
- ;===================powerpoint=============================================
- HKCU,"Software\Microsoft\Office\11.0\Common\Licensing","C86C0B9263C04E358605281275C21F97",0x1,01,00,00,00,27,00,00,00,7b,39,30,31,31,\
- 30,38,30,34,2d,36,30,30,30,2d,31,31,44,33,2d,38,43,46,45,2d,30,31,35,30,30,\
- 34,38,33,38,33,43,39,7d,00,56,00,00,00,4d,00,69,00,63,00,72,00,6f,00,73,00,\
- 6f,00,66,00,74,00,20,00,4f,00,66,00,66,00,69,00,63,00,65,00,20,00,50,00,72,\
- 00,6f,00,66,00,65,00,73,00,73,00,69,00,6f,00,6e,00,61,00,6c,00,20,00,45,00,\
- 64,00,69,00,74,00,69,00,6f,00,6e,00,20,00,32,00,30,00,30,00,33,00,00,00
- ;=========================================================================
-
- ;====================== 注册 word 文件关联 =======================
- HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Winword.exe","Path",,"%01%\"
- HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Winword.exe","useURL",,"1"
- HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Winword.exe",,,"%01%\WinWord.exe"
- HKCU,"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.doc","Application",,"WinWord.exe"
- HKCU,"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.doc\OpenWithList","b",,"WORDPAD.EXE"
- HKCU,"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.doc\OpenWithList","MRUList",,"ab"
- HKCU,"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.doc\OpenWithList","a",,"WinWord.exe"
- HKCR,"Applications\WINWORD.EXE\shell",,,"open"
- HKCR,"Applications\WINWORD.EXE\shell","FriendlyCacheCTime",0x1,00,B9,DD,34,87,8D,BE,01
- HKCR,"Applications\WINWORD.EXE\shell","FriendlyCache",,"Microsoft Word for Windows"
- HKCR,"Applications\WINWORD.EXE\shell\open\command",,,"%01%\WinWord.exe ""%%1"""
- HKCR,"Applications\WINWORD.EXE\DefaultIcon",,,"%01%\WinWord.exe,1"
- HKCR,"CLSID\{00020906-0000-0000-C000-000000000046}",,,"Microsoft Word 文档"
- HKCR,"CLSID\{00020906-0000-0000-C000-000000000046}\DefaultExtension",,,".doc,Word Document (.doc)"
- HKCR,"CLSID\{00020906-0000-0000-C000-000000000046}\DefaultIcon",,,"%01%\WinWord.exe,1"
- HKCR,"CLSID\{00020906-0000-0000-C000-000000000046}\ProgID",,,"Word.Document.8"
- HKCR,"CLSID\{00020906-0000-0000-C000-000000000046}\Version",,,"9"
- HKCR,"CLSID\{00020906-0000-0000-C000-000000000046}\VersionIndependentProgID",,,"Word.Document"
- HKCR,"CLSID\{00020906-0000-0000-C000-000000000046}\LocalServer32",,,"%01%\WinWord.exe"
- HKCU,"Software\Netscape\Netscape Navigator\Suffixes","application/rtf",,"RTF"
- HKCU,"Software\Netscape\Netscape Navigator\Suffixes","application/msword",,"DOC"
- HKCU,"Software\Netscape\Netscape Navigator\Viewers","TYPE4",,"application/msword"
- HKCU,"Software\Netscape\Netscape Navigator\Viewers","TYPE5",,"application/rtf"
- HKCU,"Software\Netscape\Netscape Navigator\Viewers","application/rtf",,"%01%\WinWord.exe"
- HKCU,"Software\Netscape\Netscape Navigator\Viewers","application/msword",,"%01%\WinWord.exe"
- HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Accepted Documents","*",,"application/msword"
- HKCR,"MIME\Database\Content Type\application/msword","Extension",,".doc"
- HKCR,".rtf",,,"Word.RTF.8"
- HKCR,".rtf","Content Type",,"application/msword"
- HKCR,"Word.RTF.8","EditFlags",0x1,00,00,01,00
- HKCR,"Word.RTF.8",,,"RTF 格式"
- HKCR,"Word.RTF.8\shell",,,"Open"
- HKCR,"Word.RTF.8\shell\Open",,,"打开(&O)"
- HKCR,"Word.RTF.8\shell\Open\command",,,"""%01%\WinWord.exe"" ""%%1"""
- HKCR,"Word.RTF.8\DefaultIcon",,,"%01%\WinWord.exe,1"
- HKCR,"Word.RTF.8\CLSID",,,"{00020906-0000-0000-C000-000000000046}"
- HKCR,".doc",,,"Word.Document.8"
- HKCR,".doc","Content Type",,"application/msword"
- HKCR,".doc\Word.Document.8\ShellNew","FileName",,"winword8.doc"
- HKCR,"Word.Document",,,"Microsoft Word 文档"
- HKCR,"Word.Document\CurVer",,,"Word.Document.8"
- HKCR,"Word.Document\CLSID",,,"{00020906-0000-0000-C000-000000000046}"
- HKCR,"Word.Document.8","EditFlags",0x1,00,00,01,00
- HKCR,"Word.Document.8",,,"Microsoft Word 文档"
- HKCR,"Word.Document.8\shell",,,"Open"
- HKCR,"Word.Document.8\shell\Open",,,"打开(&O)"
- HKCR,"Word.Document.8\shell\Open\command",,,"""%01%\WinWord.exe"" ""%%1"""
- HKCR,"Word.Document.8\protocol\StdFileEditing\server",,,"%01%\WinWord.exe"
- HKCR,"Word.Document.8\DefaultIcon",,,"%01%\WinWord.exe,1"
- HKCR,"Word.Document.8\CLSID",,,"{00020906-0000-0000-C000-000000000046}"
- HKCR,"Word.Document\CurVer",,,"Word.Document.8"
- HKCR,".dot",,,"Word.Template.8"
- HKCR,".dot","Content Type",,"application/msword"
- HKCR,"Word.Template.8","EditFlags",0x1,00,00,01,00
- HKCR,"Word.Template.8",,,"Microsoft Word 模板"
- HKCR,"Word.Template.8\shell",,,"Open"
- HKCR,"Word.Template.8\shell\Open",,,"打开(&O)"
- HKCR,"Word.Template.8\shell\Open\command",,,"""%01%\WinWord.exe"" ""%%1"""
- HKCR,"Word.Template.8\DefaultIcon",,,"%01%\WinWord.exe,2"
- HKCR,"Word.Template.8\CLSID",,,"{00020906-0000-0000-C000-000000000046}"
- ;=========================================================================
- ;====================== 注册 excel 文件关联 =======================
- HKCR,"Applications\Excel.exe\shell",,,"open"
- HKCR,"Applications\Excel.exe\shell","FriendlyCacheCTime",0x1,00,13,1B,B5,B5,8D,BE,01
- HKCR,"Applications\Excel.exe\shell","FriendlyCache",,"Microsoft Excel for Windows"
- HKCR,"Applications\Excel.exe\shell\open\command",,,"%01%\Excel.exe ""%%1"""
- HKCR,"Applications\Excel.exe\DefaultIcon",,,"%01%\Excel.exe,1"
- HKCR,".xls",,,"Excel.Chart.8"
- HKCR,".xls","Content Type",,"application/vnd.ms-excel"
- HKCR,"Excel.Chart.8","EditFlags",0x1,00,00,01,00
- HKCR,"Excel.Chart.8",,,"Microsoft Excel 工作簿"
- HKCR,"Excel.Chart.8\shell",,,"Open"
- HKCR,"Excel.Chart.8\shell\Open",,,"打开(&O)"
- HKCR,"Excel.Chart.8\shell\Open\command",,,"""%01%\Excel.exe"" ""%%1"""
- HKCR,"Excel.Chart.8\DefaultIcon",,,"%01%\Excel.exe,1"
- ;=====================================================================
- ;====================== 注册 PowerPoint 文件关联 =======================
- HKCR,"Applications\PowerPnt.exe\shell",,,"open"
- HKCR,"Applications\PowerPnt.exe\shell","FriendlyCacheCTime",0x1,00,13,1B,B5,B5,8D,BE,01
- HKCR,"Applications\PowerPnt.exe\shell","FriendlyCache",,"Microsoft PowerPoint for Windows"
- HKCR,"Applications\PowerPnt.exe\shell\open\command",,,"%01%\PowerPnt.exe ""%%1"""
- HKCR,"Applications\PowerPnt.exe\DefaultIcon",,,"%01%\PowerPnt.exe,1"
- HKCR,".ppt",,,"PowerPoint.Show.8"
- HKCR,".ppt","Content Type",,"application/vnd.ms-powerpoint"
- HKCR,"PowerPoint.Show.8","EditFlags",0x1,00,00,01,00
- HKCR,"PowerPoint.Show.8",,,"Microsoft PowerPoint 演示文稿"
- HKCR,"PowerPoint.Show.8\shell",,,"Open"
- HKCR,"PowerPoint.Show.8\shell\Open",,,"打开(&O)"
- HKCR,"PowerPoint.Show.8\shell\Open\command",,,"""%01%\PowerPnt.exe"" ""%%1"""
- HKCR,"PowerPoint.Show.8\shell\Show",,,"放映(&H)"
- HKCR,"PowerPoint.Show.8\shell\Show\command",,,"""%01%\PowerPnt.exe"" /s ""%%1"""
- HKCR,"PowerPoint.Show.8\DefaultIcon",,,"%01%\PowerPnt.exe,1"
- ;=====================================================================
- [Strings]
复制代码
作者: temp 时间: 2010-12-20 22:16 标题: 回复 9楼 的帖子
我用的转换文件是dos版的reg2inf,经改造代码后只需要输入完整的文件名,含扩展名(必须在同一目录中,代码如下:- @echo off
- title= "*.reg"转换"*.inf"
- Set Var0=%0
- Set OP=
- for /f "delims=" %%I in ("%Var0%") do Set OP=%%~dpI
-
- cd /d "%OP%"
- :start
- cls
- color 1f
- echo =========================================
- echo 请输入"*.reg"文件路径和文件名,然后按回车
- echo ────────────────────
- echo.
- Set Choice=
- rem set /p Choice=请输入"*.reg"文件路径和文件名:
- set /p Choice= 输入:
-
- if not exist "%Choice%" goto error
- goto run
-
- :error
- echo.
- echo 输入的路径或文件名不存在!
- echo 任意键返回重新输入……
- pause>nul
- goto start
-
- :run
- cls
- color 1f
- "%OP%reg2inf.com" "%Choice%">nul
- ::".\reg2inf.com" "%Choice%">nul
- echo.
- echo 转换完成!!!
- echo.
- echo 请到reg源文件目录中查看同名的inf文件!
- echo 任意键退出……
- exit
复制代码
最近又改良了一下,只需把注册表文件拖上来即可:- @echo off
- title= 注册表文件自动为转换"*.inf"
-
- %~d1
- cd %~p1
- set "file=%~nx1%"
- set "hole=%~x1%"
- rem 后缀名前面加0 防止后缀名为空的情况。也可以加其他的
- if 0%hole% equ 0.reg goto run
- :run
- "%~dp0reg2inf.com" "%file%">nul
复制代码
不用输入文件名是简单了,但它们都存在一个同样的问题,生成的文件名中都包含注册表的扩展名,本贴就是为简化而发
本贴问题解决了,又想更上一层楼,精简生成文件中包含的无用代码,于是一个问题解决了又提出另一个问题,直至所有问题全部解决了,又想写出更简单的批(如果把解决每一个问题的代码全写进去,代码臃肿,重复,效率太差),于是趁热打铁,又发了一个终极贴
感谢hanyeguxing帮忙给写了一个短小精悍的帖子,至此该贴的所有问题终结了~
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |