简体   繁体   English

批处理文件以查找具有双重文件扩展名的文件并删除最后一个文件

[英]batch file to find files with double file extensions and remove the last one

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

Try this and remove the echo , if the output is OK: 如果输出正常,请尝试此操作并删除echo

@echo off &setlocal
for /r \ %%i in (*) do (
    for %%j in ("%%~ni") do if "%%~xj" neq "" echo ren "%%~fi" "%%~nj"
)

Edit: added support for the entire HD. 编辑:增加了对整个高清的支持。

@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. 对于以sourcedir为根的树中的每个文件,如果文件的NAME本身包含“扩展名”,并且不存在没有原始扩展名的文件名,则重命名该文件。 That way, if ...picture.jpg.doc is found, the rename should occur only if ...picture.jpg does not exist. 这样,如果找到了...picture.jpg.doc ,只有在...picture.jpg不存在时才会重命名。

The command to rename is simply ECHO ed. 重命名命令只是ECHO ed。 You'd need to remove the ECHO keyword to activate the rename - after verifying that's what you want to do. 在验证您要执行的操作后,您需要删除ECHO关键字以激活重命名。

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 ECHO es 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 es首先提供列表,因为在重命名文件时可能会在主重命名运行中产生不同的结果时提供此类列表是无意义的。

@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. 合理地显然,如果需要,可以扩展“额外”列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM