Reset Data Usage with a Simple Batch Script

Discussion in 'Windows 10' started by Polo6RGTI, May 9, 2017.

  1. Polo6RGTI

    Polo6RGTI MDL Junior Member

    Jun 5, 2015
    66
    81
    0
    #1 Polo6RGTI, May 9, 2017
    Last edited: May 9, 2017
    I was reading a tutorial on TenForums yesterday on how to reset the Data Usage, but the methods described there only involves the disabling of network adapters and then manually deleting the files contained in the "C:\Windows\System32\sru" folder. As I'm no expert at coding I thought I'd search on the internet on how make a simple batch script to disable the network adapters and then delete the contents of a folder.

    So I found the required commands on the internet, but my first couple of attempts to reset the Data Usage did not work. Then I searched for a command to take ownership of a folder and I also added a couple of commands to stop and disable the Data Usage service(DusmSvc). The script seemed to work, but only after a restart. I also found that if the Data Usage app page was opened/closed and then running the script some of the files failed to be deleted due to being in use by another process. After some more searching I only discovered the Reset Data Usage V1.0 app of wtarkan here on the MDL forum. I downloaded it and tried it. I immediately noticed that the app did not disable the network adapters(Hint) to reset the Data Usage.

    While I was checking the sru folder security permissions and only by accident I removed the DPS object. The Data usage page then failed to load the Data Usage statistics. So I searched the internet on how to restore a folders permissions. I found a command to do a backup of a folders permissions, I ran the command on my desktop computer to backup the folder missions of the untouched sru folder then copied it over to my laptop and restored the folder missions of the sru folder. I then searched DPS and typed it in on Blackviper's webpage and found it was the Diagnostic Policy Service(Hint).

    Here below is the Batch Script I ended up with. As I have mentioned above I'm not an expert coder so any constructive comments about this script would be appreciated.

    Code:
    @ECHO OFF
    :: BatchGotAdmin (Run as Admin code starts)
    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"
    :: BatchGotAdmin (Run as Admin code ends)
    :: Your codes should start from the following line
    COLOR 1F
    ECHO Commencing with Data Usage Reset:
    ECHO.
    ECHO Stopping Diagnostic Policy Service
    ECHO.
    net stop DPS > NUL 2>&1
    ECHO.
    ECHO Resetting Data Usage
    echo Y | del /f /q "C:\Windows\System32\sru\*.*" > nul
    ECHO.
    ECHO Restarting Diagnostic Policy Service
    ECHO.
    net start DPS > NUL 2>&1
    ECHO.
    ECHO Data Usage was Reset to 0
    ECHO.
    ECHO Closing in 5 Seconds....
    ping 127.0.0.1 -n 6 >nul 2>&1
    exit
     
  2. wilenty

    wilenty MDL Senior Member

    Jan 15, 2014
    270
    494
    10
    and what if no network?

    Code:
    ECHO Closing in 5 Seconds....
    timeout /T 5 >nul 2>&1
    
    :)
     
  3. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,093
    24,398
    340
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. Polo6RGTI

    Polo6RGTI MDL Junior Member

    Jun 5, 2015
    66
    81
    0
    I have tweaked the script a little bit.
    Code:
    @ECHO OFF
    :: BatchGotAdmin (Run as Admin code starts)
    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"
    :: BatchGotAdmin (Run as Admin code ends)
    :: Your codes should start from the following line
    COLOR 1F
    CHOICE /C:YN /M "Do you want to reset the Data Usage now?"
    IF ERRORLEVEL 2 goto no
    IF ERRORLEVEL 1 goto yes
    :yes
    COLOR 2F
    ECHO.
    ECHO Commencing with Data Usage Reset:
    ECHO.
    ECHO Stopping Diagnostic Policy Service
    ECHO.
    net stop DPS > NUL 2>&1
    ECHO Resetting Data Usage
    ECHO Y | del /f /q "C:\Windows\System32\sru\*.*" > nul
    ECHO.
    ECHO Restarting Diagnostic Policy Service
    ECHO.
    net start DPS > NUL 2>&1
    ECHO.
    CLS
    ECHO Data Usage was Reset to 0
    ECHO.
    ECHO Closing in 5 Seconds....
    ping 127.0.0.1 -n 6 >nul 2>&1
    exit
    :no
    exit
     
  5. Polo6RGTI

    Polo6RGTI MDL Junior Member

    Jun 5, 2015
    66
    81
    0
    The "ping 127.0.0.1 -n 6 >nul 2>&1" creates the necessary delay to simulate the "Closing in 5 Seconds....".
     
  6. wilenty

    wilenty MDL Senior Member

    Jan 15, 2014
    270
    494
    10
    do You check the command TIMEOUT parameters, NO...
    try this:
    Code:
    ECHO Closing in 5 Seconds....
    timeout /t 5 /NOBREAK >nul 2>&1
    
    I expect from people thinking, not just copy-paste, greetings from the snowy north
     
  7. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,093
    24,398
    340
    Still timeout is the new ping :cool2:. Ping is kinda deprecated.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. Polo6RGTI

    Polo6RGTI MDL Junior Member

    Jun 5, 2015
    66
    81
    0
    Thanks, I'll update the script accordingly.
     
  9. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,093
    24,398
    340
    I should note that the self-elevation you use iirc fails when a user (forcedly) disabled UAC :g:.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. Polo6RGTI

    Polo6RGTI MDL Junior Member

    Jun 5, 2015
    66
    81
    0
    By "Forcedly" do you mean via registry or control panel?
     
  11. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,093
    24,398
    340
    Not sure, what exactly triggered it, stopped using it quite a while ago, since it can also act picky if scripting host is disabled/tampered. Aditionally it uses a temp file that i wanted to avoid.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  12. Polo6RGTI

    Polo6RGTI MDL Junior Member

    Jun 5, 2015
    66
    81
    0
    I disabled UAC via the Control panel on my laptop now to test and the script completed without issues.
     
  13. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,093
    24,398
    340
    Then it seems to be the rogue route. The script itself is not affected, it' only the VBS file trying to trigger the UAC promt.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  14. Polo6RGTI

    Polo6RGTI MDL Junior Member

    Jun 5, 2015
    66
    81
    0
    @s1ave77 Please try this script:
    Code:
    @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 "cmd.exe", "/C """"%~f0""""", , "runas", 1 >> "%temp%\getadmin.vbs"
    cscript "%temp%\getadmin.vbs"
    exit /B
    :gotAdmin
    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
    pushd "%CD%"
    CD /D "%~dp0"
    :: BatchGotAdmin (Run as Admin code ends)
    :: Your codes should start from the following line
    COLOR 1F
    ECHO  ===============================================
    ECHO     Windows 10 Data Usage Reset Script
    ECHO  ===============================================
    ECHO.
    REM -->Offer the user a choice to run the script or not
    CHOICE /C:YN /M "Do you want to reset the Data Usage now?"
    IF ERRORLEVEL 2 goto no
    IF ERRORLEVEL 1 goto yes
    :yes
    COLOR 2F
    ECHO.
    ECHO Commencing with Data Usage Reset:
    ECHO.
    ECHO Stopping Diagnostic Policy Service
    ECHO.
    net stop DPS > NUL 2>&1
    ECHO Resetting Data Usage
    ECHO Y | del /f /q "C:\Windows\System32\sru\*.*" > nul
    ECHO.
    ECHO Restarting Diagnostic Policy Service
    ECHO.
    net start DPS > NUL 2>&1
    ECHO Data Usage was Reset to 0
    ECHO.
    ECHO Closing in 5 Seconds....
    timeout /t 5 /nobreak >nul 2>&1
    exit
    :no
    exit
     
  15. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,093
    24,398
    340
    Works in my 15063 VM.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  16. Polo6RGTI

    Polo6RGTI MDL Junior Member

    Jun 5, 2015
    66
    81
    0
    So is the failure you previously experienced resolved with this script?

    I'm updating the whole script at this moment to add several features.
     
  17. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,093
    24,398
    340
    You misunderstood me, i had no problem, it's a general problem with the method used for elevation.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...