color based on file extension

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Weshuggah
Posts: 7
Joined: 16 Dec 2020 11:56

color based on file extension

#1 Post by Weshuggah » 16 Dec 2020 12:18

Hello

So that's a part of my script that lists files in a folder and displays them so I can select one and change its extension. It works as expected but I would like to add colours based on file extensions. For exemple .dll files show in green and .bak files show in red.

In the current state it shows the list in red no matter the extensions.

Code: Select all

@echo off
:menu4
cls
echo.
color 0a
	
setlocal enabledelayedexpansion

	set Index=1
	for %%i in (Mods\*) do (
	set "SubFiles[!Index!]=%%i"
	set /a Index+=1
	)
	set /a UBound=Index-1
	
	for /l %%i in (1,1,%UBound%) do (
	echo   %%i. [91m!SubFiles[%%i]![92m          rem  Using Ansi escape color codes 
	)
	
	
		:choiceloop
		echo.  
		set /p Choice=  Enter a number to change file extension :
		if "%Choice%"=="" goto choiceloop
		if %Choice% LSS 1 goto choiceloop
		if "%Choice%"=="f" goto main

		set SubFiles=!SubFiles[%Choice%]!
		ren !SubFiles[%Choice%]! "*.dll"
		ren !SubFiles[%Choice%]! "*.bak"
	
		goto menu4

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: color based on file extension

#2 Post by Squashman » 16 Dec 2020 16:15

Use an IF command to compare the file extension and call out to Carlos' Color Function.

T3RRY
Posts: 250
Joined: 06 May 2020 10:14

Re: color based on file extension

#3 Post by T3RRY » 16 Dec 2020 20:37

Squashman wrote:
16 Dec 2020 16:15
Use an IF command to compare the file extension and call out to Carlos' Color Function.
An alternative to the Color Function is to use a color macro

Code: Select all

@Echo off
Setlocal DisableDelayedExpansion
 If "!![" == "[" (
  Echo/%%COL%% macro must be defined prior to delayed expansion being enabled
  Goto :end
 )
rem /* Macro Definitions */
 For /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (set "DEL=%%a")
rem /* %hCol% - Color macro; No error checking. Usage: (%hCol:?=HEXVALUE%Output String) */
 Set "hCol=For %%o in (1 2)Do if %%o==2 (( <nul set /p ".=%DEL%" > "!os:\n$=!" ) & ( findstr /v /a:? /R "^$" "!os:\n$=!" nul ) & ( del "!os:\n$=!" > nul 2>&1 ) & (Set "testos=!os:\n$=!" & If not "!testos!" == "!os!" (Echo/)))Else Set os="
Setlocal EnableDelayedExpansion
 PUSHD "%~dp0"
rem /* to force a new line; terminate an output string with \n$ */
(%hCol:?=40% This is an example of usage)&(%hCol:?=0d%. Done\n$.)
%hCol:?=30%bye
:end
 POPD
 Endlocal & Endlocal
Goto :Eof

Weshuggah
Posts: 7
Joined: 16 Dec 2020 11:56

Re: color based on file extension

#4 Post by Weshuggah » 17 Dec 2020 05:49

Ok I'm not sure where and how to use the IF command without breaking the index, could you detail it please? It's my first script only had experience with ahk before.

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: color based on file extension

#5 Post by Meerkat » 18 Dec 2020 02:28

Of course there are no single way on where to put the IF command. I think the "basic" (and not the shortest code) way is to have a separate pseudo-array for file extensions: (Note: I did not test this, and please remove the "<-- Addition #N" in your actual code)

Code: Select all

	...
	set Index=1
	for %%i in (Mods\*) do (
		set "SubFiles[!Index!]=%%i"
		set "ExtFiles[!index!]=%%~xi"       <-- Addition #1
		set /a Index+=1
	)
	set /a UBound=Index-1
	
	for /l %%i in (1,1,%UBound%) do (
		if /i "!ExtFiles[%%i]!"==".dll" (       <-- Addition #2
			rem show in green
			rem I don't know escape color codes stuff
		) else if /i "!ExtFiles[%%i]!" == ".bak" (
			rem show in red instead
		)
	)
Info on Addition #1 can be found in the command for /? (at the very end of the documentation), or here https://ss64.com/nt/syntax-args.html (see "parameter extensions").

Addition #2 is the IF command; documentation can be found in the command if /?. The /i switch means case-insensitive string comparison.

Meerkat

Post Reply