Please wait...

 
Search this site:

MS DOS Batch Code

Tips and tricks


I have collected a varied set of batch codes to do things so I am sharing them here. Currently they are in no particular order. I figured some of them out on my own and some I got from other web pages.

Most if not all of these batch files will work with current versions of Windows.

Use at your own risk.


Run a batch file in a completely hidden way

 

Save this one line of text as file invisible.vbs:

 

CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False

 

To run any program or batch file invisibly, use it like this:

 

wscript.exe "C:\Wherever\invisible.vbs" "C:\Some Other Place\MyBatchFile.bat"

 

You must put full path to Invisible.vbs, and you must surround that and the batch file location in double quotes.

To pass-on/relay a list of arguments use only two double quotes …

This was found at:

https://superuser.com/questions/62525/run-a-batch-file-in-a-completely-hidden-way#62646

How to minimize the command prompt from the batch file.

 

There is a quite interesting way to execute script minimized by making him restart itself minimised. Here is the code to put in the beginning of your script:

if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit
... script logic here ...
exit

How it works

When the script is being executed IS_MINIMIZED is not defined (if not DEFINED IS_MINIMIZED) so:

IS_MINIMIZED is set to 1: set IS_MINIMIZED=1.

Script starts a copy of itself using start command && start "" /min "%~dpnx0" %* where:
"" - empty title for the window.
/min - switch to run minimized.
"%~dpnx0" - full path to your script.
%* - passing through all your script's parameters.

Then initial script finishes its work: && exit.

For the started copy of the script variable IS_MINIMISED is set by the original script so it just skips the execution of the first line and goes directly to the script logic.
Remarks

You have to reserve some variable name to use it as a flag.
The script should be ended with exit, otherwise the cmd window wouldn't be closed after the script execution.

If your script doesn't accept arguments you could use argument as a flag instead of variable:

if "%1" == "" start "" /min "%~dpnx0" MY_FLAG && exit


Creating a batch file delay

 

Below is an example of how to delay a batch file any where from 5 to 99 seconds. In the below example we illustrate a 5 second delay.

TYPE NUL | CHOICE.COM /N /CY /TY,5 >NUL

Another way to create a pause in a batch file in Windows 7, 8 and 10.


TIMEOUT [/T] timeout [/NOBREAK]

Description:
This utility accepts a timeout parameter to wait for the specified
time period (in seconds) or until any key is pressed. It also
accepts a parameter to ignore the key press.

Parameter List:
/T timeout Specifies the number of seconds to wait.
Valid range is -1 to 99999 seconds.

/NOBREAK Ignore key presses and wait specified time.

/? Displays this help message.

NOTE: A timeout value of -1 means to wait indefinitely for a key press.

Examples:
TIMEOUT /?
TIMEOUT /T 10
TIMEOUT /T 300 /NOBREAK
TIMEOUT /T -1


Location of Windows 7 start menu.


Current User

"C:\Users\[User logon name here]\AppData\Roaming\Microsoft\Windows\Start Menu"

"%homepath%\AppData\Roaming\Microsoft\Windows\Start Menu"

---

Example:

C:\Users\7\AppData\Roaming\Microsoft\Windows\Start Menu\Programs

is the path to my start menu, programs folder (My user account is called "7")

Of course that is for THAT user.

---

All Users is in a slightly different location:

C:\Users\All Users\Microsoft\Windows\Start Menu\Programs


Prompts for user input and places it in variable.


set /p input=enter your name:

Would prompt

enter your name:

with the curser after name:


Picking a set of characters from a fixed location in a string.


set var2=%var:~x,y%

Where VAR can be any variable name.
Where X=offset (how many spaces to skip)
Where y=characters to view.

If y is possitive it counts from the beginning.
If y is negative it counts from the end of the variable.


To find the day of the week so one can run a routine only on that day.


:: The variables for the day of week are:

:: Mon Tue Wed Thu Fri Sat Sun

set day=Thu
set dayok=
date /T | find "%day%" > nul
if NOT ERRORLEVEL 1 set dayok=yes

This command is for the lower case conversion operation subroutine.


:: Variable (%~1) is the variable to change to Lower Case
:LoCase
:: Subroutine to convert a variable VALUE to all lower case.
:: The argument for this subroutine is the variable NAME.
SET %~1=!%1:A=a!
SET %~1=!%1:B=b!
SET %~1=!%1:C=c!
SET %~1=!%1:D=d!
SET %~1=!%1:E=e!
SET %~1=!%1:F=f!
SET %~1=!%1:G=g!
SET %~1=!%1:H=h!
SET %~1=!%1:I=i!
SET %~1=!%1:J=j!
SET %~1=!%1:K=k!
SET %~1=!%1:L=l!
SET %~1=!%1:M=m!
SET %~1=!%1:N=n!
SET %~1=!%1:O=o!
SET %~1=!%1:P=p!
SET %~1=!%1:Q=q!
SET %~1=!%1:R=r!
SET %~1=!%1:S=s!
SET %~1=!%1:T=t!
SET %~1=!%1:U=u!
SET %~1=!%1:V=v!
SET %~1=!%1:W=w!
SET %~1=!%1:X=x!
SET %~1=!%1:Y=y!
SET %~1=!%1:Z=z!


What day is it?


@echo off
@echo.
@echo.
date /T | find "Sun" > nul
if NOT ERRORLEVEL 1 set day=Sunday
if NOT ERRORLEVEL 1 goto foundit
date /T | find "Mon" > nul
if NOT ERRORLEVEL 1 set day=Monday
if NOT ERRORLEVEL 1 goto foundit
date /T | find "Tue" > nul
if NOT ERRORLEVEL 1 set day=Tuesday
if NOT ERRORLEVEL 1 goto foundit
date /T | find "Wed" > nul
if NOT ERRORLEVEL 1 set day=Wendsday
if NOT ERRORLEVEL 1 goto foundit
date /T | find "Thr" > nul
if NOT ERRORLEVEL 1 set day=Thursday
if NOT ERRORLEVEL 1 goto foundit
date /T | find "Fri" > nul
if NOT ERRORLEVEL 1 set day=Friday
if NOT ERRORLEVEL 1 goto foundit
date /T | find "Sat" > nul
if NOT ERRORLEVEL 1 set day=Saturday
if NOT ERRORLEVEL 1 goto foundit
:foundit
@echo.
@echo It is %day%!
@echo.
@echo.
:quit

Get current directory into a variable.


:: curdir.bat
@SET cd=
@SET promp$=%prompt%
@PROMPT SET cd$Q$P
@CALL>%temp%.\setdir.bat
@
% do not delete this line %
@ECHO off
PROMPT %promp$%
FOR %%c IN (CALL DEL) DO %%c %temp%.\setdir.bat
ECHO. current directory=%cd%

Gosub with Batch Files.

 

goto endsubs
:sub1
:: Enter subroutine here
goto %return%
:endsubs
:: Call Subroutine with this.
set return=ret1
goto sub1
:ret1

Creating folder with time stamp for it's name.


You can execute this batch file so that every time it creates a new folder that is unique to the date and time when it was created, thus not deleting the previous folder.
This Help, the For Loops are on one line.

@echo off
rem Make Directory With Name Date and Time
for /f "tokens=1-3 delims=/- " %%a in ('date /t') do set XDate=%%a-%%b-%%c
for /f "tokens=1-2 delims=: " %%a in ('time /t') do set XTime=%%a.%%b
echo %XDate% %XTime%
md "%Xdate% %XTime%"

Archive.bat

@echo off
:: archive.cmd
:: Created by Daniel Jacobson
:: Last Updated 2/23/2016
:: This batch program was created to be used for
:: incremental backups of important data bases and
:: data files.
:: Variables
:: file = File to be backed up
:: directory = directory for backup folder/file
:: XDate = current date
:: XTime = current time
:: file = File to be archived
:: Directory = Directory the newly created folder is to be created in.
:: newdirectory = compleate path & name for new directory.
:: -----------------------------------------
:: *************Start**************
:: Set Variables
set file=
set directory=
:: Test Variables
:: Testing first variable
if %1'==' goto syntax
if not exist %1 goto syntax1
set file=%~1
:: Testing second variable
if %2'==' goto nodirectory
set directory=%~2\
goto dirok
:nodirectory
:: Make target directory for sub directory same as directory file is in.
set directory=%~dp1
:dirok
:: Program
:: Make Directory With Name Date and Time
for /f "tokens=1-4 delims=/- " %%a in ('date /t') do set XDate=%%d-%%b-%%c %%a
for /f "tokens=1-2 delims=: " %%a in ('time /t') do set XTime=%%a.%%b
set newdirectory=%directory%%Xdate% %XTime%
@echo.
@echo Creating Directory: %newdirectory%
@echo.
md "%newdirectory%"
:: Copying file to created directory
@echo.
@echo Copying "%file%" to "%newdirectory%"
@echo.
xcopy /v /z "%file%" "%newdirectory%"
@echo.
goto EOF
:: -------------------------------------
:: End Messages
:syntax1
@echo.
@echo The filename entered does not exist.
@echo. :syntax
@echo.
@echo Archive
@echo.
@echo Copies file(s) to folders that are named the current time and date.
@echo.
@echo Syntax: ARCHIVE filename directory
@echo.
@echo filename = The file to be archived.
@echo.
@echo note: If no file is specified, all files in folder are copied.
@echo.
@echo Directory = Location where you want the new folder to be created.
@echo.
@echo note: If no directory is listed, then folder is created in the
@echo same directory as file is found.
:EOF

Batch file to install program from a spicific folder.


This is for installing a program from a network or thumb drive. It will delete the destination folder and then install the program. Of course you can modify it to change what it does.

Create a folder with the name of the program to be installed Program and put this batch file in it. Then a subfolder called Install in that folder, and another folder called Shortcut in the 'install' folder.

Folders to create:

Program/Install/Shortcut

---

@echo off
CLS
::
::
:: This program installs a folder and a file and an icon.
:: It is a generic install program designed to install programs using
:: a batch file.
::
:: This program is set only to be run as a subroutine.
::
::
@echo.
@echo.
@echo This program will install {put the name of your program here}.
@echo.
@echo Program can be accesed via a Start Menu shortcut under {Location of sortcut in start menu here}.
@echo.
@echo.
@echo.
@echo To abort, exit this window with the X in the upper right hand corner, else...
@echo.
@echo.
pause
@echo.
@echo.
@echo Are you sure?
@echo.
@echo.
@echo To abort, exit this window with the X in the upper right hand corner, else...
@echo.
@echo.
pause
cls
SETLOCAL
Set THISDIR=%~dp0
:: Sourcefolder = folder you wish to copy from.
set sourcefolder=%THISDIR%Install\{Name of install program here}
:: destfolder is the destination you wish to put the sourcefolder.
set destfolder=%PROGRAMFILES%\DAJWare\{Name of install program here}
:: Shortcut is the location of the shortcut
set shortcut=%THISDIR%Install\Shortcut\*.lnk
:: shortcutdest = is where you want the shortcut to be installed.
set shortcutdest=%ALLUSERSPROFILE%\Start Menu\Programs\Accessories\System Tools
:: Shortcut is the location of the Startup shortcut
set Startup=%THISDIR%Install\Startup\*.lnk
:: shortcutdest = is where you want the shortcut to be installed.
set Startupdest=%ALLUSERSPROFILE%\Start Menu\Programs\Startup
:: Checking if program folder already exists, if it then delete it.
if not exist "%destfolder%" goto :skip1
del /F /S /Q "%destfolder%\*.*"
rd /Q /s "%destfolder%"
:Skip1
ROBOCOPY /E /zB "%sourcefolder%" "%destfolder%"
ROBOCOPY /E /zB "%shortcut%" "%shortcutdest%"
ROBOCOPY /E /zB "%Startup%" "%Startupdest%"
:ENDPROGRAM
rem start "Edit" NOTEPAD.EXE "C:\Program Files\DAJWare\Defrag\DefragFixedDrives.bat"
@echo.
@echo Program end
@echo.
@echo To close this dialog...
@echo.
REM PAUSE
exit


Lock Workstation (works in batch file or a desktop shortcut)


rundll32.exe user32.dll, LockWorkStation


Shutdown PC.


shutdown [/i | /l | /s | /r | /g | /a | /p | /h | /e | /o] [/hybrid] [/soft] [/fw] [/f] [/m \\computer][/t xxx][/d [p|u:]xx:yy [/c "comment"]]

Usage: shutdown [/i | /l | /s | /r | /g | /a | /p | /h | /e | /o] [/hybrid] [/soft] [/fw] [/f]
    [/m \\computer][/t xxx][/d [p|u:]xx:yy [/c "comment"]]

    No args    Display help. This is the same as typing /?.
    /?         Display help. This is the same as not typing any options.
    /i         Display the graphical user interface (GUI).
               This must be the first option.
    /l         Log off. This cannot be used with /m or /d options.
    /s         Shutdown the computer.
    /r         Full shutdown and restart the computer.
    /g         Full shutdown and restart the computer. After the system is
               rebooted, restart any registered applications.
    /a         Abort a system shutdown.
               This can only be used during the time-out period.
               Combine with /fw to clear any pending boots to firmware.
    /p         Turn off the local computer with no time-out or warning.
               Can be used with /d and /f options.
    /h         Hibernate the local computer.
               Can be used with the /f option.
    /hybrid    Performs a shutdown of the computer and prepares it for fast startup.
               Must be used with /s option.
    /fw        Combine with a shutdown option to cause the next boot to go to the
               firmware user interface.
    /e         Document the reason for an unexpected shutdown of a computer.
    /o         Go to the advanced boot options menu and restart the computer.
               Must be used with /r option.
    /m \\computer Specify the target computer.
    /t xxx     Set the time-out period before shutdown to xxx seconds.
               The valid range is 0-315360000 (10 years), with a default of 30.
               If the timeout period is greater than 0, the /f parameter is
               implied.
    /c "comment" Comment on the reason for the restart or shutdown.
               Maximum of 512 characters allowed.
    /f         Force running applications to close without forewarning users.
               The /f parameter is implied when a value greater than 0 is
               specified for the /t parameter.
    /d [p|u:]xx:yy  Provide the reason for the restart or shutdown.
               p indicates that the restart or shutdown is planned.
               u indicates that the reason is user defined.
               If neither p nor u is specified the restart or shutdown is
               unplanned.
               xx is the major reason number (positive integer less than 256).
               yy is the minor reason number (positive integer less than 65536).

Reasons on this computer:
(E = Expected U = Unexpected P = planned, C = customer defined)
Type    Major   Minor   Title

 U      0       0       Other (Unplanned)
E       0       0       Other (Unplanned)
E P     0       0       Other (Planned)
 U      0       5       Other Failure: System Unresponsive
E       1       1       Hardware: Maintenance (Unplanned)
E P     1       1       Hardware: Maintenance (Planned)
E       1       2       Hardware: Installation (Unplanned)
E P     1       2       Hardware: Installation (Planned)
E       2       2       Operating System: Recovery (Unplanned)
E P     2       2       Operating System: Recovery (Planned)
  P     2       3       Operating System: Upgrade (Planned)
E       2       4       Operating System: Reconfiguration (Unplanned)
E P     2       4       Operating System: Reconfiguration (Planned)
  P     2       16      Operating System: Service pack (Planned)
        2       17      Operating System: Hot fix (Unplanned)
  P     2       17      Operating System: Hot fix (Planned)
        2       18      Operating System: Security fix (Unplanned)
  P     2       18      Operating System: Security fix (Planned)
E       4       1       Application: Maintenance (Unplanned)
E P     4       1       Application: Maintenance (Planned)
E P     4       2       Application: Installation (Planned)
E       4       5       Application: Unresponsive
E       4       6       Application: Unstable
 U      5       15      System Failure: Stop error
 U      5       19      Security issue (Unplanned)
E       5       19      Security issue (Unplanned)
E P     5       19      Security issue (Planned)
E       5       20      Loss of network connectivity (Unplanned)
 U      6       11      Power Failure: Cord Unplugged
 U      6       12      Power Failure: Environment
  P     7       0       Legacy API shutdown


Find Current Drive.


:: CURDRIVE.BAT
:: Written by Rob van der Woude
:: Places the current drive letter in environment variable CURRDRIVE
::
SET CURDRIVE=
CD | CHOICE /C:ABCDEFGHIJKLMNOPQRSTUVWXYZ > NUL
IF ERRORLEVEL 1 SET CURDRIVE=A
IF ERRORLEVEL 2 SET CURDRIVE=B
IF ERRORLEVEL 3 SET CURDRIVE=C
IF ERRORLEVEL 4 SET CURDRIVE=D
IF ERRORLEVEL 5 SET CURDRIVE=E
IF ERRORLEVEL 6 SET CURDRIVE=F
IF ERRORLEVEL 7 SET CURDRIVE=G
IF ERRORLEVEL 8 SET CURDRIVE=H
IF ERRORLEVEL 9 SET CURDRIVE=I
IF ERRORLEVEL 10 SET CURDRIVE=J
IF ERRORLEVEL 11 SET CURDRIVE=K
IF ERRORLEVEL 12 SET CURDRIVE=L
IF ERRORLEVEL 13 SET CURDRIVE=M
IF ERRORLEVEL 14 SET CURDRIVE=N
IF ERRORLEVEL 15 SET CURDRIVE=O
IF ERRORLEVEL 16 SET CURDRIVE=P
IF ERRORLEVEL 17 SET CURDRIVE=Q
IF ERRORLEVEL 18 SET CURDRIVE=R
IF ERRORLEVEL 19 SET CURDRIVE=S
IF ERRORLEVEL 20 SET CURDRIVE=T
IF ERRORLEVEL 21 SET CURDRIVE=U
IF ERRORLEVEL 22 SET CURDRIVE=V
IF ERRORLEVEL 23 SET CURDRIVE=W
IF ERRORLEVEL 24 SET CURDRIVE=X
IF ERRORLEVEL 25 SET CURDRIVE=Y
IF ERRORLEVEL 26 SET CURDRIVE=Z
IF "%CURDRIVE%"=="" ECHO Error checking current drive
IF NOT "%CURDRIVE%"=="" ECHO Current drive is %CURDRIVE%:

Logoff


%SystemRoot%\System32\logoff.exe


Taskkill


TASKKILL [/S system [/U username [/P [password]]]]
{ [/FI filter] [/PID processid | /IM imagename] } [/T] [/F]

TASKKILL [/S system [/U username [/P [password]]]]
         { [/FI filter] [/PID processid | /IM imagename] } [/T] [/F]

Description:
    This tool is used to terminate tasks by process id (PID) or image name.

Parameter List:
    /S    system           Specifies the remote system to connect to.

    /U    [domain\]user    Specifies the user context under which the
                           command should execute.

    /P    [password]       Specifies the password for the given user
                           context. Prompts for input if omitted.

    /FI   filter           Applies a filter to select a set of tasks.
                           Allows "*" to be used. ex. imagename eq acme*

    /PID  processid        Specifies the PID of the process to be terminated.
                           Use TaskList to get the PID.

    /IM   imagename        Specifies the image name of the process
                           to be terminated. Wildcard '*' can be used
                           to specify all tasks or image names.

    /T                     Terminates the specified process and any
                           child processes which were started by it.

    /F                     Specifies to forcefully terminate the process(es).

    /?                     Displays this help message.

Filters:
    Filter Name   Valid Operators           Valid Value(s)
    -----------   ---------------           -------------------------
    STATUS        eq, ne                    RUNNING |
                                            NOT RESPONDING | UNKNOWN
    IMAGENAME     eq, ne                    Image name
    PID           eq, ne, gt, lt, ge, le    PID value
    SESSION       eq, ne, gt, lt, ge, le    Session number.
    CPUTIME       eq, ne, gt, lt, ge, le    CPU time in the format
                                            of hh:mm:ss.
                                            hh - hours,
                                            mm - minutes, ss - seconds
    MEMUSAGE      eq, ne, gt, lt, ge, le    Memory usage in KB
    USERNAME      eq, ne                    User name in [domain\]user
                                            format
    MODULES       eq, ne                    DLL name
    SERVICES      eq, ne                    Service name
    WINDOWTITLE   eq, ne                    Window title

    NOTE
    ----
    1) Wildcard '*' for /IM switch is accepted only when a filter is applied.
    2) Termination of remote processes will always be done forcefully (/F).
    3) "WINDOWTITLE" and "STATUS" filters are not considered when a remote
       machine is specified.

Examples:
    TASKKILL /IM notepad.exe
    TASKKILL /PID 1230 /PID 1241 /PID 1253 /T
    TASKKILL /F /IM cmd.exe /T
    TASKKILL /F /FI "PID ge 1000" /FI "WINDOWTITLE ne untitle*"
    TASKKILL /F /FI "USERNAME eq NT AUTHORITY\SYSTEM" /IM notepad.exe
    TASKKILL /S system /U domain\username /FI "USERNAME ne NT*" /IM *
    TASKKILL /S system /U username /P password /FI "IMAGENAME eq note*"

Who am I


WhoAmI has three ways of working:

Syntax 1:
    WHOAMI [/UPN | /FQDN | /LOGONID]

Syntax 2:
    WHOAMI { [/USER] [/GROUPS] [/CLAIMS] [/PRIV] } [/FO format] [/NH]

Syntax 3:
    WHOAMI /ALL [/FO format] [/NH]

Description:
    This utility can be used to get user name and group information
    along with the respective security identifiers (SID), claims,
    privileges, logon identifier (logon ID) for the current user
    on the local system. I.e. who is the current logged on user?
    If no switch is specified, tool displays the user name in NTLM
    format (domain\username).

Parameter List:
    /UPN                    Displays the user name in User Principal
                            Name (UPN) format.

    /FQDN                   Displays the user name in Fully Qualified
                            Distinguished Name (FQDN) format.

    /USER                   Displays information on the current user
                            along with the security identifier (SID).

    /GROUPS                 Displays group membership for current user,
                            type of account, security identifiers (SID)
                            and attributes.

    /CLAIMS                 Displays claims for current user,
                            including claim name, flags, type and values.

    /PRIV                   Displays security privileges of the current
                            user.

    /LOGONID                Displays the logon ID of the current user.

    /ALL                    Displays the current user name, groups
                            belonged to along with the security
                            identifiers (SID), claims and privileges for
                            the current user access token.

    /FO       format        Specifies the output format to be displayed.
                            Valid values are TABLE, LIST, CSV.
                            Column headings are not displayed with CSV
                            format. Default format is TABLE.

    /NH                     Specifies that the column header should not
                            be displayed in the output. This is
                            valid only for TABLE and CSV formats.

    /?                      Displays this help message.

Examples:
    WHOAMI
    WHOAMI /UPN
    WHOAMI /FQDN
    WHOAMI /LOGONID
    WHOAMI /USER
    WHOAMI /USER /FO LIST
    WHOAMI /USER /FO CSV
    WHOAMI /GROUPS
    WHOAMI /GROUPS /FO CSV /NH
    WHOAMI /CLAIMS
    WHOAMI /CLAIMS /FO LIST
    WHOAMI /PRIV
    WHOAMI /PRIV /FO TABLE
    WHOAMI /USER /GROUPS
    WHOAMI /USER /GROUPS /CLAIMS /PRIV
    WHOAMI /ALL
    WHOAMI /ALL /FO LIST
    WHOAMI /ALL /FO CSV /NH
    WHOAMI /?

Adjust System Volume from the Command Line

This batch file is to adjust the system volume from the command line or from a batch file. You can use a program called nircmd.exe found at http://www.nirsoft.net/utils/nircmd.html. I created the below batch file to make using the nircmd.exe easier to use. With this you only have to type systole and then the number of the percentage you want to set the system volume to. I called the below program sysvol.cmd. I downloaded the nircmd.exe from the web site and but all the files in the windows system32 folder. However it is a batch file so you must use the call command to access it from a batch file unless it is the last command in the batch file. Otherwise the remaining commands in the batch file will not execute.

---

@echo off
::
:: sysvol.cmd
::
:: Created by Daniel Jacobson
::
:: Created 8/19/2016
::
:: This program requires the program nircmd.exe.
::
:: nircmd.exe be downloaded at: http://www.nirsoft.net/utils/nircmd.html
::
:: Clearing Variables
Set $X=
SET $LEV=
:: This filters the input to only 0 through 100
:: %1 is the value that is entered after the sysvol command: sysvol X
if %1'==' goto Syntax
if %1 LSS 0 goto Syntax
if %1 GTR 100 goto Syntax
:: 100% = 65535
:: 75%= 49151.25
:: 50%= 32767.5
:: 25%= 16383.75
:: Calculating persentage to a value nircmd.exe can understand.
set /a "$X=%1*65535"
set /A "$LEV=$X/100"
@echo.
@echo Set Windows System Volume to %1%%%
@echo.
nircmd.exe setsysvolume %$LEV%
goto end
:syntax
@echo.
@echo sysvol [n]
@echo.
@echo Sets the system volume to the percent you specify from 0%% to 100%%.
@echo.
@echo n = Any positive number from 0 to 100.
@echo (Do not use the percent %% sign.)
@echo.
:end
:: Clearing Variables
Set $X=
SET $LEV=

Delete Temporary Files in Windows


@echo off
:: Created by Daniel Jacobson
:: Last Updated 10-11-2015
::
:: This program deletes the standard Windows temporary files.
:: This was written for Windows Windows version 5, 6, and 10.
:: Not recomended on any other OS.
::
::
:: With Windows 10 it must be run from a shortcut that has administrator privilages.
::
:: Windows XP - 5.1
:: Windows XP Pro - 5.2
:: Windows Vista - 6.0
:: Windows 7 - 6.1
:: Windows 8 - 6.2
:: Windows 8.1 - 6.3
:: Windows 10 - 10
::
::
@echo.
@echo This program deletes the standard Windows temporary files.
@echo.
@echo.
@echo **************************************************************
@echo * *
@echo * This has been tested on Windows Version 5, 6, and 10 only. *
@echo * *
@Echo * Not recomended on any other OS. *
@echo * *
@echo * The OS on this PC is: *
ver
@echo * *
@echo **************************************************************
@echo.
@echo.
@echo To abort, exit this window with the X in the upper right hand corner, else...
@echo.
@echo.
pause
@echo.
@echo.
@echo Are you sure?
@echo.
@echo.
@echo To abort, exit this window with the X in the upper right hand corner, else...
@echo.
@echo.
pause
@echo.
@echo.
@echo DELETE WINDOWS TEMPORARY FILES PROGRAM
@echo.
@echo %date% %time%
@echo.
@echo This program will delete standard windows temporary
@echo files on this computer for the current user.
@echo.
@echo ------------------------------------------------------
GOTO OSOK
:: Testing for Windows Version.
ver | find "Version 5" > nul
if NOT ERRORLEVEL 1 goto :OSOK ver | find "Version 6" > nul
if NOT ERRORLEVEL 1 goto :OSOK ver | find "Version 10" > nul
if NOT ERRORLEVEL 1 goto :OSOK @ECHO.
@ECHO Wrong Windows Version
@ECHO.
@ECHO As of July 2011
@echo.
@ECHO This program only works on Windows Versions 5 and 6.
@echo.
@echo Your version is version:
ver
@echo.
GOTO :ENDPROGRAM
:OSOK :: Delete files in the %temp% directory.
set lastfive=%temp:~-5%
IF /i "%temp%"=="" (
@echo.
@Echo %%temp%% variable is does not exist.
@echo.
@echo %%temp%% Variable needs to be set on this PC.
@echo.
@echo This program is halted.
@echo.
pause
goto :ENDPROGRAM
)
if /i "%lastfive%"=="\temp" (
SET DESC=Temporary files in the %%temp%% Directory.
SET TARGET=%temp%
SET FILEORDIR=DIR
call :DELFILES
) ELSE (
@echo.
@echo %%temp%% variable = %temp%
@echo.
@echo %%temp%% variable does not end in \temp
@echo.
@echo Not taking any chances, not deleteing files.
@echo.
@echo Recomend changing %%temp%% variable.
@echo.
@echo This program is halted.
@echo.
@echo %date% %time%
@echo.
pause
goto :ENDPROGRAM
)
)
:: The following works in Windows versions 5 and 6
SET DESC=Temporary files in System Root directory.
SET TARGET=%SYSTEMROOT%\temp
SET FILEORDIR=DIR
call :DELFILES
SET DESC=Temp files root of the Home Drive.
SET TARGET=%homedrive%\temp
SET FILEORDIR=DIR
call :DELFILES
ver | find "Version 5" > nul
if ERRORLEVEL 1 goto :NOTOS5
:: This is for Windows Version 5
SET DESC=Temporary Internet Files of Current user.
SET TARGET=%HOMEDRIVE%%homepath%\Local Settings\Temporary Internet Files
SET FILEORDIR=DIR
call :DELFILES
:NOTOS5
ver | find "Version 6" > nul
if ERRORLEVEL 1 goto :NOTOS6
:: This is for Windows Version 6.
SET DESC=Temporary Internet Files of Current user.
SET TARGET=%HOMEDRIVE%%homepath%\AppData\Local\Microsoft\Windows\Temporary Internet Files
SET FILEORDIR=DIR
call :DELFILES
:NOTOS6
ver | find "Version 10" > nul
if ERRORLEVEL 1 goto :NOTOS10
:: This is for Windows Version 10.
SET DESC=Temporary Internet Files of Current user.
SET TARGET=%HOMEDRIVE%%homepath%\AppData\Local\Microsoft\Windows\INetCache
SET FILEORDIR=DIR
call :DELFILES
:NOTOS10
GOTO :ENDPROGRAM
:: Subroutines
:DELFILES
@ECHO.
IF %FILEORDIR%==DIR (
if not exist "%TARGET%" echo *
if not exist "%TARGET%" echo * %DESC%
if not exist "%TARGET%" echo *
if not exist "%TARGET%" echo * %TARGET%
if not exist "%TARGET%" echo *
if not exist "%TARGET%" echo * [DIRECTORY NOT FOUND]
if not exist "%TARGET%" echo *
if not exist "%TARGET%" goto :ENDDEL
@echo *
@echo * Deleting %DESC%
@echo *
@echo * Target=%TARGET%
@echo *
@echo.
echo y| cacls "%TARGET%/*.*" /T /C /G EVERYONE:F SYSTEM:F
attrib /S /D -r -a -s -h "%TARGET%\*.*"
del /F /S /Q "%TARGET%\*.*"
if exist "%TARGET%\*.*" (
echo y| cacls "%TARGET%" /T /C /G EVERYONE:F SYSTEM:F
rd /S /Q "%TARGET%"
md "%TARGET%"
)
goto :ENDDEL
)
IF %FILEORDIR%==FILE (
if not exist "%TARGET%" echo *
if not exist "%TARGET%" echo * %DESC%
if not exist "%TARGET%" echo *
if not exist "%TARGET%" echo * %TARGET%
if not exist "%TARGET%" echo *
if not exist "%TARGET%" echo * [FILE NOT FOUND]
if not exist "%TARGET%" echo *
if not exist "%TARGET%" goto :ENDDEL
@echo *
@echo * Deleting %DESC%
@echo *
@echo * Target=%TARGET%
@echo *
@echo.
echo y| cacls "%TARGET%" /T /C /G EVERYONE:F SYSTEM:F
attrib /S /D -r -a -s -h "%TARGET%"
del /F /S /Q "%TARGET%"
GOTO :ENDDEL
)
@ECHO *
@ECHO * Not specified if the target is a File or a Directory:
@ECHO *
@ECHO * %DESC%
@ECHO *
@ECHO * %TARGET%
@ECHO *
@ECHO * %FILEORDIR%
@ECHO *

:ENDDEL
@ECHO.
GOTO:EOF

:ENDPROGRAM
@echo ---------------------------------------
@echo.
@echo End Program. %date% %time%
@echo.
EXIT


Run Microsoft Windows Defender AnitVirus program on all Local Drives and Boot Sector

@echo.
@echo * Scanning Boot Sector
@echo.
@echo "C:\Program Files\Windows Defender\MpCmdRun.exe" -Scan -BootSector
@echo.
"C:\Program Files\Windows Defender\MpCmdRun.exe" -Scan -BootSector
@echo.
@echo * Full System Scan - (This PC only).
@echo.
@echo "C:\Program Files\Windows Defender\MpCmdRun.exe" -Scan -ScanType 2
@echo.
"C:\Program Files\Windows Defender\MpCmdRun.exe" -Scan -ScanType 2
@echo.


Run Microsoft Windows Defender AnitVirus program on Network Drive

@echo Connecting to NAS.
@echo.
:: Put the location of your network drive to scan in place of //XXX/XXX:
net use X: \\XXX\XXX

@echo * Full System Scan - NAS Drive.
@echo.
@echo "C:\Program Files\Windows Defender\MpCmdRun.exe" -Scan -ScanType 3 -File X:
@echo.
"C:\Program Files\Windows Defender\MpCmdRun.exe" -Scan -ScanType 3 -File X:
@echo.


I will add more in the future...