Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 can 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.

like image 732
Lukie Avatar asked Jul 18 '11 13:07

Lukie


2 Answers

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
)
like image 72
Joey Avatar answered Nov 11 '22 19:11

Joey


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"
  )
)
like image 20
Andriy M Avatar answered Nov 11 '22 20:11

Andriy M