1

I'm working on a little script that should be simple but is really problematic. (I started learning command prompt and batch scripts a few days ago x.x ).

I need it to, when executed, change system year to 2010, and when executed again return to correct year. After a research I came up with this:

@echo off

:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>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"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"

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

:gotAdmin
    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------

SETX DAY "%Date:~0,2%" /M
SETX MONTH "%Date:~3,2%" /M
SETX YEAR "%DATE:~-4%" /M


IF NOT "%year%"=="2010" (
    SETX CPS_YEAR "%year%" /M   
    date %day%-%month%-2010
) ELSE (
    date %day%-%month%-%cps_year%
)

The first part is a script I found (https://sites.google.com/site/eneerge/scripts/batchgotadmin) that asks administrator privileges to handle system variables, the problem is in my logic.

It kind of works, but after changing the year once, it need to be executed twice to change back. And twice again to change again.

Any leads on what could be causing this?

Many Thanks!

rCirelli
  • 11
  • 3
  • 1
    Why are you using `SETX`? The variables you create with `SETX` are not available during runtime of the script. See NOTE 2 of the help file. Just use the SET command to create your variables. – Squashman Mar 17 '17 at 15:57
  • is there another way to store a variable that continues to exist after runtime of the script? because I need it to store the current year on a variable, change system date to 2010, then when executed again change it back. (sorry for delayed reply) edit: for the time being, I pasted the code 2 times inside the script...It works but I feel bad about myself hahaha) – rCirelli Mar 20 '17 at 12:24

1 Answers1

0

After thinking through and "optimizing" the code, it now works properly. I have to use SETX variables because I need to store the current year on it, so when I execute the script again I can access that value and change the year back to the original value.

IF NOT "%DATE:~-4%"=="2010" (
    SETX CPS_YEAR "%DATE:~-4%" /M
    date %Date:~0,2%-%Date:~3,2%-2010
) ELSE (
    date %Date:~0,2%-%Date:~3,2%-%CPS_YEAR%
)
rCirelli
  • 11
  • 3