[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文件操作] 批处理如何重命名文件夹?

假设有 文件夹“bbb” 和 批处理文件“aaa.bat”
在注册表里添加了文件夹右键菜单,值为“aaa.bat %1”
想右击文件夹“bbb”,通过这个右键菜单,将“bbb”重命名为“bbb.txt”
还有,如果反过来,想右击文件夹“bbb.txt”(不是.txt文档,是文件夹),通过这个右键菜单,将“bbb.txt”重命名为“bbb”
这个aaa.bat应该如何写呢??

[ 本帖最后由 next 于 2008-7-30 20:17 编辑 ]

试试这个:
  1. @echo off
  2. for /f "delims=" %%i in ("%~1") do (
  3.     if /i "~x1"==".txt" (
  4.         ren "%~1" "%~n1"
  5.     ) else ren "%~1" "%~n1.txt"
  6. )
复制代码
尺有所短寸有所长,学好批处理没商量;
考虑问题复杂化,解决问题简洁化。

心在天山,身老沧州。

TOP

干吗这么麻烦?直接把ren命令写到注册表里面不就行了?

TOP

不行的
因为“ren %1 %1.txt ”中的“%1”包含了全路径,重命名失败

刚才找了这一段:
@echo off
cd %1
set "var=%cd%"
:loop
set "var1=%var:*\=%"
set "var=%var1%"
if not "%var1%"=="%var:\=%" goto loop
cd..
ren %var1% %var1%.txt
可以将“bbb”重命名成“bbb.txt”

但如果是“bbb.txt”重命名成“bbb”却不知道该怎么写~

TOP

不信你试一下“ren d:\abc d:\abc.txt”(abc是文件夹),因为包含全路径,所以失败了

TOP

多看看帮助,你就知道怎样处理全路径的问题了^_^
for /?
In addition, substitution of FOR variable references has been enhanced
You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line

TOP

返回列表