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
    60
    69
    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 Member

    Jan 15, 2014
    162
    282
    10
    and what if no network?

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

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,697
    22,596
    340
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. Polo6RGTI

    Polo6RGTI MDL Junior Member

    Jun 5, 2015
    60
    69
    0
    Hi, like I said in my OP, I am not an expert coder. The commands what are contained in my script is what I have found while searching on the internet. So far the script works and I haven't had any major negative feedback about it.
    Yes it does and as I stated in my OP, I only discovered the " Reset Data Usage V1.0" app after I already had started to compile the script. The app however did assist me by making me aware that disabling network adapters were not necessary to run the script.
    Yes.
     
  5. Polo6RGTI

    Polo6RGTI MDL Junior Member

    Jun 5, 2015
    60
    69
    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
     
  6. Polo6RGTI

    Polo6RGTI MDL Junior Member

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

    wilenty MDL Member

    Jan 15, 2014
    162
    282
    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
     
  8. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,697
    22,596
    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...
  9. Polo6RGTI

    Polo6RGTI MDL Junior Member

    Jun 5, 2015
    60
    69
    0
    Thanks, I'll update the script accordingly.
     
  10. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,697
    22,596
    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...
  11. Polo6RGTI

    Polo6RGTI MDL Junior Member

    Jun 5, 2015
    60
    69
    0
    By "Forcedly" do you mean via registry or control panel?
     
  12. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,697
    22,596
    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...
  13. Polo6RGTI

    Polo6RGTI MDL Junior Member

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

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,697
    22,596
    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...
  15. Polo6RGTI

    Polo6RGTI MDL Junior Member

    Jun 5, 2015
    60
    69
    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
     
  16. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,697
    22,596
    340
    Works in my 15063 VM.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  17. Polo6RGTI

    Polo6RGTI MDL Junior Member

    Jun 5, 2015
    60
    69
    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.
     
  18. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,697
    22,596
    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...