1

I am struggling with how to make the Energy-Reporter program save dates and rename the files. I am new to Batch programing. I started to learn this language when my OEMCreate DELL Latitude E5500 battery died after about a year. I am attempting to make a program that allows me to monitor and save battery statistic reports to my computer. I am hoping to add the ability to graph battery degradation over time. I attached the current code below. I need to figure out how to make a CMD based File manager.

@echo OFF
TITLE Energy-Reporter
set CUR_YYYY=%date:~10,4%
set CUR_MM=%date:~4,2%
set CUR_DD=%date:~7,2%
set CUR_HH=%time:~0,2%
if %CUR_HH% lss 10 (
    set CUR_HH=0%time:~1,1%
    set CUR_AP="AM"
)
if %CUR_HH% GEQ 12 (
    set CUR_HH=CUR_HH-12
    set CUR_AP="PM"
)

set CUR_NN=%time:~3,2%
set CUR_SS=%time:~6,2%
set CUR_MS=%time:~9,2%

set SUBFILENAME="%CUR_MM%-%CUR_DD%-%CUR_YYYY%_At_%CUR_HH%-%CUR_NN%-%CUR_SS% %CUR_AP%"

:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
    IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) ELSE (
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
)

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params= %*
    echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params:"=""%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------
IF EXIST "C:\Program Files\Energy-Report\." (
    ECHO DIRECTIORY FOUND
) else (
    ECHO CREATING "C:\Program Files\Energy-Report\"
    mkdir "C:\Program Files\Energy-Report\"
)
COLOR F4

:USER_SELECTER
    CLS
    ECHO ===============================================================
    ECHO Battery logger 
    ECHO ===============================================================
    ECHO Select an option:
    Choice /C codq /N /M "[C]reate power log | [O]pen report | [D]elete log | [Q]uit"
    If ErrorLevel 4 GoTo :EOF
    If ErrorLevel 3 GoTo DELETE_LOG
    If ErrorLevel 2 GoTo SAVED_LOGS
    If ErrorLevel 1 GOTO REPORT_DATA
    PAUSE
    GOTO USER_SELECTER

:REPORT_DATA
    cd "C:\Program Files\Energy-Report\"
    powercfg -energy
    REN "Energy-Report.html" "energy-report_%SUBFILENAME%.html"
    PAUSE
    GOTO USER_SELECTER

:SAVED_LOGS
    cd "C:\Program Files\Energy-Report\"
    DIR
    PAUSE
    GOTO USER_SELECTER

:DELETE_LOG
    SET /P Entry_name=log to be erased
    DELETE "C:\%Program Files%\Energy-Report\%Entry_name%"
    PAUSE
    GOTO USER_SELECTER

I have uploaded all of the previous versions of the code to my website for those who are interested in the code development.

If I do not get a chance to update the stack overflow version, The latest version is available by running the code snippet.

<embed style="width:100%;height:100%;"src="https://theelectronichandbook.tech/code/downloads/battery-logger-for-windows/latestcode.php?FRAME=iframe"></embed>
JTS
  • 41
  • 10
  • 1
    Fix these, `EXSIST`, `/%userprofile%\ ` and `SET \p`. Also, I'm not sure why you're using the 32 bit version of `cacls`. – Compo Nov 05 '19 at 10:08
  • 1
    I would also suggest that you use `choice.exe` instead of `SET /P`, i.e. `Choice /C codq /N /M "[C]reate power log | [O]pen report | [D]elete log | [Q]uit"`. with `If ErrorLevel 4 GoTo :EOF`, `If Errorlevel 3 GoTo DELETE_LOG`, `If ErrorLevel 2 GoTo SAVED_LOGS`, then your fixed `cd /%userprofile%\desktop` line. – Compo Nov 05 '19 at 10:22
  • I switched 'SET \p' to 'Choice /C codq /N /M'. Just wondering how do I check for files that already exist? – JTS Nov 11 '19 at 14:44
  • 1
    `if exist "file.ext" ...` or `if not exist "file.ext" ...` – Stephan Nov 12 '19 at 11:11
  • 1
    `%PROCESSOR_ARCHITECTURE%` is "AMD64" (Uppercase) (at least on my systems), so your `IF` will never be true. Correct the string or use `if /i` to ignore capitalization. – Stephan Nov 13 '19 at 09:39
  • 1
    Your way to create `%SUBFILENAME%` is dependent on locale settings. To be helpful, we need to see *your* output of `echo %date%` (or you use [a method independent of locale settings](https://stackoverflow.com/a/18024049/2152082) – Stephan Nov 13 '19 at 09:44
  • Here the rename that is stored in `%SUBFILENAME%` is stored in the variable is, `11-12-2019_At_21-08-43`. The result im looking for is `11-12-2019_At_09-08-43PM`. – JTS Nov 13 '19 at 18:45
  • 1
    If your locale settings does 24hours and you need 12Hours and AM/PM, there's no built-in way. You have to do a little math. – Stephan Nov 13 '19 at 20:09
  • ok then so should i use `elif`, `if/else`, or some other statement? – JTS Nov 14 '19 at 14:22
  • @Stephan I have not revived a response from regarding how to use arithmetic to judge if the value is above or bellow 12 hours. – JTS Nov 23 '19 at 23:37
  • 1
    You have created a variable `HH` to hold the hours. Is it that difficult to build `if %HH% GEQ 12`? – Stephan Nov 24 '19 at 09:00
  • @Stephan I did not know that `GEQ` was a command. Like I said I am quite new to batch files. I have 4years of javascript, css, and html but almost non in batch. – JTS Nov 24 '19 at 14:55
  • 1
    `GEQ` isn't a command, it's a comparator within the `if` syntax. See `if /?` for more. – Stephan Nov 25 '19 at 10:40
  • @Stephan Thanks I will look into it. – JTS Nov 25 '19 at 19:01
  • @Stephan The date time has been resolved now I need to figure out how to make a not so labor intensive UI for deleting logs that are no longer necessary and can also be deleted by windows disk cleanup. – JTS Nov 28 '19 at 16:19
  • 1
    Batch is a pure console application (CUI). There is no way to create a GUI with it (at least not without the help of another language). Only pure batch solution: `start "" "C:\Program Files\Energy-Report\"`. Well, in fact, this fits your request quite well: "opens a simple GUI to delete files". – Stephan Nov 29 '19 at 08:29
  • @Stephan The GUI is just Explorer. Can I CMD reskin the explorer to somewhat Match the mono-space look of CMD? – JTS Nov 29 '19 at 14:00
  • 1
    Nope. `Cmd` isn't able to do anything with GUI applications (besides just starting them) – Stephan Nov 29 '19 at 14:26
  • @Stephan Then I guess I need to mind something like IBM BASIC V1.0 Text editor floppy. It is command line based and allows the user to delete files by selecting the item with the arrow keys. – JTS Nov 29 '19 at 14:57

0 Answers0