Batch file Per loop over the list of file extensions with exceptions

Say I have the following files in a directory

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

how can I write a package for the loop so that all the ".js" files are compiled, but the ".min.js" are not, and the output of the .js filename can be changed to add .min.js

eg:

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

would ideally give the following and note that file2.min.js does not appear on the left.

  • file.js β†’ file.min.js
  • file1.js β†’ file1.min.js

Thank you for your help.

+7
source share
3 answers

Just see if 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 ) 
+10
source

Not that I did not agree with @Joey's solution, but I thought that would not 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" ) ) 
+1
source

In the past, when I wanted to do something like this, I used a renamex script. Here are some 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
source

All Articles