批处理文件遍历与排除

问题描述:

文件扩展名的名单说,我有以下目录中的文件批处理文件遍历与排除

  • /file.js
  • /file2.min.js
  • /file1.js

我怎么能写循环批处理使得所有“的.js”文件回升,但“.min.js”不和的.js文件名的输出可以改为追加.min。 js

如:

for %%A IN (*.js) DO @echo %%A "->" %%~nA ".min.js" 

在理想情况下产生以下,并注意file2.min.js不显示在左边。

  • file.js - > file.min.js
  • file1.js - > file1.min.js

感谢您的帮助。

只要看看它是否已经包含.min.js

setlocal enabledelayedexpansion 
for %%f in (*.js) do (
    set "N=%%f" 
    if "!N:.min.js=!"=="!N!" echo %%f -^> %%~nf.min.js 
) 
+1

梦幻般的..工作的一种享受,谢谢! – Lukie

在过去,当我想要做类似的事情时,我使用了renamex脚本。这是例子:

 Usage: renamex [OPTIONS] EXTENSION1 EXTENSION2 

Renames a set of files ending with EXTENSION1 to end with EXTENSION2. 
If none of the following options are provided, renaming is done in the current 
directory for all files with EXTENSION1. 

    Where [OPTIONS] include: 
-v  --verbose print details of files being renamed 

-d  [directory] 
     --directory [full path] rename files specified in the directory 

-f  [filter] 
     --filter [wildcard] 
     use wildcard characters (* and ?) for renaming files 

-h  --help show this help 

    Examples: 
renamex htm html 
    (rename all .htm files to .html in the current directory) 
renamex -v log txt 
    (show verbose output while renaming all .log files to .txt) 
renamex -v -d "D:\images" JPG jpeg 
    (rename all .JPG files located in D:\images to .jpeg) 
renamex -v -d "D:\movies" -f *2011* MPG mpeg 
    (rename all .MPG files with 2007 in their names, in D:\movies to .mpeg) 
+0

在这种情况下,我不想重命名任何东西,我只是想根据我提供的输出回显列表文件到控制台。 – Lukie

+0

'renamex'不被识别为内部或外部命令,可操作程序或批处理文件。 – Joey

+0

@Joey - 哎呀,我添加了一个脚本的链接。 –

不,我不同意@乔伊的解决方案,但我认为它不会伤害,如果我发布了一个替代方案:

@ECHO OFF 
FOR %%f IN (*.js) DO (
    FOR %%g IN ("%%~nf") DO (
    IF NOT "%%~xg" == ".min" ECHO "%%f" -^> "%%~g.min.js" 
) 
)