开发者

Batch File For loop over a list of file extensions with exclusions

Say i have the following files in a directory

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

how can i write a batch for loop such that all ".js" files are picked up but ".min.js" are not and the output of the .js filename ca开发者_StackOverflow社区n be changed to append .min.js

eg:

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

would ideally produce the following, and note the file2.min.js is not displayed to the left.

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

Thanks for your help.


Just look whether it already contains .min.js:

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


Not that I disagree with @Joey's solution, but I thought it wouldn't hurt if I posted an alternative:

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


In the past, when I wanted to do something similar I used the renamex script. Here it is with examples:

        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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜