Rize S. answered 03/23/23
Senior IT Certified Trainer, IT Developer & DBA Administrator
Yes, you can use the following batch file script to loop over all ".js" files in a directory, excluding the ones with ".min.js" extension and output their filenames with the desired format:
@echo off
setlocal enabledelayedexpansion
for %%A IN (*.js) DO (
set filename=%%A
set extension=!filename:~-7!
if not !extension! == .min.js (
set filename=!filename:.js=.min.js!
@echo %%A "->" !filename!
)
)
Explanation:
- setlocal enabledelayedexpansion is used to enable the delayed expansion of variables in a loop
- The for loop iterates over all files with ".js" extension in the current directory
- Inside the loop, set filename=%%A sets the filename variable to the current file name
- set extension=!filename:~-7! extracts the last 7 characters (i.e., the file extension) from the filename variable and sets the extension variable to it
- if not !extension! == .min.js checks if the file extension is not ".min.js"
- If the condition is true, set filename=!filename:.js=.min.js! replaces the ".js" extension with ".min.js" in the filename variable
- Finally, @echo %%A "->" !filename! outputs the original file name followed by the new file name in the desired format.