批处理文件来找到双文件扩展名的文件,并删除最后一个 [英] batch file to find files with double file extensions and remove the last one

查看:226
本文介绍了批处理文件来找到双文件扩展名的文件,并删除最后一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有有与普通的文件扩展名一起双重文件扩展名的文件几个文件夹。我需要创建一个批处理脚本来搜索所有文件夹和与具有双扩展名的文件删除最后一个扩展。文件扩展名都不是一致的。

 下面是一个例子C:\\测试\\ regular.exe
C:\\测试\\ picture.jpg.doc
C:\\ newtest \\ document.doc.pdf最终的结果,我需要C:\\测试\\ regular.exe
C:\\测试\\ picture.jpg
C:\\ newtest \\ document.doc


解决方案

  @ECHO OFF
SETLOCALSET sourcedir = C:\\ sourcedir
FOR / R%sourcedir%%%我IN(*。*)DO(
 FOR %% N的(%%〜NI)DO IF NOT%%〜XN==如果不是存在%%〜DPNIECHO任志强%%〜科幻%%〜に
 FOR %% N的(%%〜NI)DO IF NOT%%〜XN==是否存在%%〜DPNIECHO CAN NOT仁%%〜科幻%%〜に
)GOTO:EOF

这一批应该完成的任务。

有关在 sourcedir 为根的树中的每个文件,如果名称本身包含一个扩展名的文件和没有原来的扩展名不存在,然后重命名文件。这样,如果 ... picture.jpg.doc 被发现,应该发生重命名只有当 ... picture.jpg 不存在。

要重命名的命令是简单的 ECHO 编辑。你需要删除 ECHO 关键字激活命名 - 验证这就是你想要做什么之后,

我添加了一个二线报告,重命名不能,因为现有的文件来完成。这可能是非常稍好完成,但它会奏效。


修订的情况下,简单的重命名修改名称不能完成。

注意 - 此版本将立即重命名 - 没有 ECHO ES先提供一个清单,因为这是胡说八道提供时,重命名文件可能会产生不同的结果,这样的列表主命名运行。

  @ECHO OFF
SETLOCALSET sourcedir = C:\\ sourcedir
FOR / R%sourcedir%%%我IN(*。*)DO(
 FOR %% N的(%%〜NI)DO IF NOT%%〜XN==是否存在%%〜DPNI(
  SET renreq = Y
  FOR %%一个IN(ALT新的额外的另1 2 3 4 5 6 7 8 9)DO如果定义renreq(
   IF NOT存在%%〜DPI %%〜NN _ %%一个%%〜XN(
    任志强%%〜科幻%%〜NN _ %%一个%%〜XN
    SETrenreq =
   )
  )
  如果定义renreq ECHO CAN NOT仁%%〜科幻
 )ELSE(
 任志强%%〜科幻%%〜に
 )
)GOTO:EOF

合理很明显,额外的名单,可如果需要延长。

I have several folders that have files with double file extensions along with regular file extensions. I need to create a batch script to search all the folders and remove the last extension with any files that have double extensions. None of the file extensions are consistent.

Here's an example

C:\test\regular.exe
C:\test\picture.jpg.doc
C:\newtest\document.doc.pdf

End Result I need

C:\test\regular.exe
C:\test\picture.jpg
C:\newtest\document.doc

解决方案

@ECHO OFF
SETLOCAL

SET sourcedir=c:\sourcedir
FOR /r "%sourcedir%" %%i IN (*.*) DO (
 FOR %%n IN ("%%~ni") DO IF NOT "%%~xn"=="" IF NOT EXIST "%%~dpni" ECHO REN "%%~fi" "%%~ni"
 FOR %%n IN ("%%~ni") DO IF NOT "%%~xn"=="" IF EXIST "%%~dpni" ECHO CAN NOT REN "%%~fi" "%%~ni"
)

GOTO :EOF

This batch should accomplish the task.

For each file in the tree rooted at sourcedir, if the NAME of the file itself contains an 'extension' and the filename without the original extension does not exist, then rename the file. That way, if ...picture.jpg.doc is found, the rename should occur only if ...picture.jpg does not exist.

The command to rename is simply ECHOed. You'd need to remove the ECHO keyword to activate the rename - after verifying that's what you want to do.

I've added a second line to report that a rename could not be done because of an existing file.. This could be done very slightly better, but it will work.


Revised to modify name in case simple rename can not be done.

Caution - this version will rename immediately - there are no ECHOes to provide a list first because it's nonsense to provide such a list when renaming a file may produce different results on the main rename run.

@ECHO OFF
SETLOCAL

SET sourcedir=c:\sourcedir
FOR /r "%sourcedir%" %%i IN (*.*) DO (
 FOR %%n IN ("%%~ni") DO IF NOT "%%~xn"=="" IF EXIST "%%~dpni" (
  SET renreq=Y
  FOR %%a IN (new alt extra another 1 2 3 4 5 6 7 8 9) DO IF DEFINED renreq (
   IF NOT EXIST "%%~dpi%%~nn_%%a%%~xn" (
    REN "%%~fi" "%%~nn_%%a%%~xn"
    SET "renreq="
   )
  )
  IF DEFINED renreq ECHO CAN NOT REN "%%~fi"
 ) ELSE (
 REN "%%~fi" "%%~ni"
 )
)

GOTO :EOF

Reasonably obviously, the list of "extras" can be extended if required.

这篇关于批处理文件来找到双文件扩展名的文件,并删除最后一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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