0

Im writing a script to automatially import all .reg files in %bkps% but i would like a custom success message instead of the orriginal "Operation completed successfully." This is my code; (NOTE '{ESC}' I'm using to denote the invisible ESC character)

(for /f "tokens=*" %%f in ('dir %bkps% /b /o:gd /a-d') do (
    reg import "%bkps%\%%f" 2>NUL && echo {ESC}[37m│ ├─{ESC}[30m{ESC}[42m%%f was successfully restored.{ESC}[40m
))

and when its run in my program i get the output;

←[37m│ ├─←[30m←[42mFontBKP_partA133200.reg was successfully restored.←[40m
←[37m│ ├─←[30m←[42mFontBKP_partB133200.reg was successfully restored.←[40m

it should look like this with the colors added;

│ ├─FontBKP_partA133200.reg was successfully restored.
│ ├─FontBKP_partB133200.reg was successfully restored.

The echo command works elsewhere in the code and even works on its own inside the FOR loop but as soon as it goes inside that FOR loop with the reg command it is printed literally without the colors. (i tried it with the && and on a whole new line running non-conditionally, no luck)

some other threads about this are here but i am not sure if i can apply any of their to my code as i need it to run conditionally off the import command and it needs to run an indefinite amount of times (although it should never be more than like 25)

How to echo with different colors in the Windows command line INSIDE A FOR LOOP WITH A CHOICE cmd

Batch script : issue with colored echo when chaining commands

Cmd.exe Batch Script Color Output With FOR /F 'MSYS' Commands

Æthan
  • 9
  • 4
  • If instead of `&& echo` on the same line, you do `if "!errorlevel!"=="0"` on the next line (you will have to enable delayed expansion if you haven't already), does it work correctly? – SomethingDark May 02 '23 at 23:26
  • You need to offer us much more information. We have no idea what the content of your script is up to that point, and we also have no idea what the OS and version/build information is. – Compo May 02 '23 at 23:56
  • @SomethingDark yes I did try that and it still does not work properly. – Æthan May 03 '23 at 18:06
  • @Compo Sorry about that, im new to StackOverflow so haven't gotten used to the site's etiquette. OS is Windows 10 version 21h2 build 19044.2846. The script I'm writing needs to be able to run on most if not all windows 10 and 11 versions. as for the rest of the script its too long to add here so what information would I need to include? – Æthan May 03 '23 at 18:18
  • Well to start with, **all versions of Windows 10 do not have the facility**, and the earlier versions of those which do, could only utilise it after having first modified a registry key value. – Compo May 03 '23 at 20:16

2 Answers2

0

This answer applies, assuming you are using a later version of Windows, i.e 10 and up.

You seemed to have included only a snippet of your code, however you left out an important part of the code, and not sure if you included it, considering you viewed my answer linked in your question, which shows the for /F %%a in ('echo prompt $E ^| cmd') do set "col=%%a" and then using the %col% variable:

@echo off
for /F %%a in ('echo prompt $E ^| cmd') do set "col=%%a"

(for /f "tokens=*" %%f in ('dir /b /o:gd /a-d') do (
   reg import "%bkps%\%%f" 2>NUL && echo %col%[37m│ ├─%col%[30m%col%[42m%%f was sucessfully restored.%col%[40m
))
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • Sorry forgot to mention this in my post so ill go back and edit it, I do have the invisible ESC characters in front of the ]00m color encoding bit. – Æthan May 03 '23 at 18:40
  • ok, but that does not add any value, did you try and use the version I posted as it is? Can you also confirm your OS version? – Gerhard May 04 '23 at 05:43
  • Im using Windows 10 version 21h2 build 19044.2846 and if I copy your code exactly it starts trying to import every file in my C:\ directory. Also, the color still isn't working but that could be because none of the files it's trying to import are succeeding so the second command doesn't run. – Æthan May 05 '23 at 22:15
  • After changing 'dir /b /o:gd /a-d' to 'dir %bkps% /b /o:gd /a-d' so it has the correct location to import from it still is doing the exact same thing. its echo-ing the text literally as "←[37m│ ├─←[30m←[42mFontBKP_partA_121321.reg was successfully restored.←[40m←[33m The operation completed successfully." – Æthan May 05 '23 at 22:19
  • I suggest you edit your question and post the updated code, as I provided, including the changes you made. – Gerhard May 06 '23 at 13:53
0

Ok i found a solution using this fix; https://stackoverflow.com/a/60865920/21768379

I changed my code from

(for /f "tokens=*" %%f in ('dir %bkps% /b /o:gd /a-d') do (
    reg import "%bkps%\%%f" 2>NUL && echo {ESC}[37m│ ├─{ESC}[30m{ESC}[42m%%f was successfully restored.{ESC}[40m
))

to

(for /f "tokens=*" %%f in ('dir %bkps% /b /o:gd /a-d') do (
    reg import "%bkps%\%%f" 2>NUL && echo success | (findstr /R success) & echo {ESC}[37m│ ├─{ESC}[30m{ESC}[42m%%f was successfully restored.{ESC}[40m
))

and now it outputs with the correct colors (albeit with the extra line that says "success" before it but I can figure that out later)

Edit: It was as simple as adding >NUL to the first echo command like this;

(for /f "tokens=*" %%f in ('dir %bkps% /b /o:gd /a-d') do (
        reg import "%bkps%\%%f" 2>NUL && echo success >NUL | (findstr /R success) & echo {ESC}[37m│ ├─{ESC}[30m{ESC}[42m%%f was successfully restored.{ESC}[40m
))
Æthan
  • 9
  • 4