Useful MS DOS Commands  

Posted by ReelTym


Create a 15 second delay (Win98, WinNT, WinXP):
ping -n 15 localhost > NUL
the "localhost" could also be 127.0.0.1, and change the "15" to the number of seconds delay required.

Get the time down to a 100th of a second (WinNT, WinXP):
for /F "tokens=5 delims= " %i in ('echo ^| time ^| find "current" ') do echo %i
if used in a batch file, change the %i to %%i and "echo %i" to something like "set ttn=%%i
or, but only in WinXP:
for /F "tokens=1 delims= " %i in ('echo.%TIME%') do echo %i
or, if you only want to print it directly to the screen and don't need to save it to a variable, use:
echo %TIME%
Could also use:
for /F "tokens=1-3 delims=: " %i in ('time /t') do echo %i%j%k
But have found this last method to be very dependant on the Regional settings of Windows, of which you may or may not have control over and does not have the 100th's.
Got sent the following one from Christoph Sternberg, which works a treat with WinXP, and is a lot easier that the 'for' method:
echo %time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%
which can also be used in a 'set' command, just as well
set ttrn=%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%

Get the date in "yyyymmdd" format (WinNT, WinXP):
for /F "tokens=2-4 delims=/ " %i in ('date /t') do echo %k%i%j
This one does assume the date is displayed as mm/dd/yyyy, with separators of "/" when "date /t" is excuted.
A little playing after checking the output of "date /t" can soon correct any change.
Have found it to be useful when wanting to produce unique daily filenames for log files.
Got sent the following one from Christoph Sternberg, which works a treat with WinXP, and is a lot easier that the 'for' method:
echo %date:~6,4%%date:~3,2%%date:~0,2%
which can also be used in a 'set' command, just as well
set tddt=%date:~6,4%%date:~3,2%%date:~0,2%


Create a new file, or completely blank an existing (Win98, WinNT, WinXP):
type NUL>newfile.txt
This will create a new file named "newfile.txt" with a 0 byte count, it is completely empty.

Change a File's Date and Time Stamp to current (Win98):
copy thefilename.txt /B+ ,,/Y
and if you need to do a bulk change to a whole directory of gif files:
for %i in ('dir *.gif') do copy %i /B+ ,,/Y
which all change the Date/Time stamp of all the files with the extension GIF in the current directory.
Have not tested in WinNT or WinXP yet.

Create a unique filename use date and time (WinNT, WinXP):
The following is a short batch file that uses date and time to create a unique filename.
@echo off
:: file = makenew.bat
::
:: description = this batch file uses current date and time to create a file with a unique filename

::
:: Date         Author    Change/Update
:: 04-Jun-2005  AGButler  Original
::
 
:: set variables
set tdtd=none
set ttrn=none

 
:: get the date and time and then into single variable
for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set tdtd=%%i%%j%%k
for /F "tokens=5-8 delims=:. " %%i in ('echo.^| time ^| find "current" ') do set ttrn=%%i%%j%%k%%l
set tufn=%tdtd%%ttrn%.txt
 
:: now create the file
type NUL>%tufn%
 
:EOF

Will have to play a little with "date" line to get the delims and order of the %%i %%j %%k correct. To check just type date on the command line and use ouput to change. Have not tested in Win98.

Remove or Substitue characters with a variable(winXP):
It is possible to do some neat functions with DOS scripts. One that is often very useful is Removing or Substituting characters with a variable.
So you have a variable that may have some thing like the time, that contain ":" collin characters "20:12:34.54", but you want to remove the collins. This can be done relitively easily with the SET command. The following script will load the time into a variable "ttrn", then remove the collins and then remove the period.
for /F "tokens=5 delims= " %%i in ('echo.^|time^|find "current" ') do set ttrn=%%i 

echo %ttrn%

set ttrn=%ttrn::=%

echo %ttrn%

set ttrn=%ttrn:.=%

echo %ttrn%
You basically have the variable the a : then the character to search for the = then the character to subsitute, if none the a deletion occurs.
variable=%variable:search=replace%
It is also useful, if loading data from a file that may enclosed in quotes, and then just use SET to remove the quotes.

Dealing with large numbers like disk free bytes for Gigabyte capacity drives, 10,000,000,000(winXP):
My problem was that when using the "fsutil" command in WinXP, Win2K and Win2K3 to retrieve the capacity and free bytes of disk drives, that were to large to be able to be used in arithmetic commands. The task was to read the capacity and compare to the free remaining and make a call if the free remaining had dropped below a set level.
The solution was to use a series of SET commands, that could simulate the conversion of Bytes into Gigabytes. OK it is not highly accurate, but it is enough to be able to assess if a drive is 90% free remaining or 2% free remaining or anywhere in between.
What the follow will do is: - put the capacity and free bytes of my C: drive into variables.  - cut off the last 6 digits, simulating conversion from Bytes to Megabytes. - Check to make sure the result has not gone below 1MB, if it has then make it 1MB. - Calculate the capacity used. This will be a number between 1 and the Total Capacity. The smaller the number the more used, or less free. - Then make an action based on the limit set.
:: put the capacity and free bytes of my C: drive into variables from the fsutil command
for /F "tokens=7 delims= " %%i in ('fsutil volume diskfree c:^|find "Total # of free bytes" ') do set ctmp1=%%i
for /F "tokens=6 delims= " %%i in ('fsutil volume diskfree c:^|find "Totel # of bytes" ') do set ctmp2=%%i


:: shorten the strings removing last 6 characters simulating conversion from Bytes to MegaBytes
set ctmp1=%ctmp1:~0,-6%
set ctmp2=%ctmp2:~0,-6%


:: check to ensure that "free bytes" now contains something, if not then make equal to 1
:: the variable will be left as is if it contains anything initially, only changes when nul
if .%ctmp1%==. set ctmp1=1


:: now evaluate using the two variables
:: this will produce a RESULT between 1(plenty of free disk) and the capacity of the drive in MegaBytes(near full)
set /A ctmp3=%ctmp2%/%ctmp1%


:: the following assumes for example a 1GB(1000MB) drive has only 100MB free remaining then = LOW
:: set a dummy result, then make a check
set ctmp4=OK
if %ctmp3% GTR 10 set ctmp4=LOW


:: output the result
echo Your C: Drive remaining capacity = %ctmp4%

It does only produce a simple rough guide, but is effective enough to judge whole percentage changes, which should be able to catch a drive at 10% remaining before it gets to 0%, leaving enough time for some action.
You can also change the 'set dummy and make a check' lines to be a bit more fail safe by:
set ctmp4=LOW
if %ctmp3% LSS 10 set ctmp4=OK
It can take a little bit of playing with, but is an excellent method of keeping an eye on your drive capacities without the need for any fancy software. It is also highly scalable for what ever drive capacity and limits that need to be set or monitored.

Creating and using a variable array in DOS(winXP):
I searched everywhere without success for a method to produce an array in DOS.
This solution has proven to be very successful.
The basics is to use a variable name that has some type of separator, like a period ".", the second half of the variable name can then be substituted with the contents of another variable, following is very a simple example.
:: set the segment variables 
set agbtp.1=A 
set agbtp.2=B 
Notice the period between the 'agbtp' and '1' ?
Next setup or supply a third variable that will be substituted into the array.
:: set the segid
set segid=1
Now put the whole lot in to a FOR-DO to obtain the new variable from the array based on the supplied segid. Note the '^=' in the delims, this allows the search to use the '=' as a separator. The 'find' with the ".variable=" is also necessary to do the correct filtering.
 :: calculate the Segment printed variable
for /F "tokens=2 delims=^=" %%i in ('set agbtp.%segid% ^| find ".%segid%=" ') do set psegid=%%i
This resulted in the variable 'psegid' now containing 'A'.
If the variable 'segid' had been loaded with the value '2', then the 'psegid' would then contain 'B'.
This may all appear very simplistic, but if there was a problem where you wanted to count from 0 to 255, and on each count, produce the output in HEX (00-FF). Setup an array of:
set myhex.0=00
set myhex.1=01
set myhex.2=02
 "    "      " - (repeat from 3 to 252 / 03 to FC)
set myhex.253=FD
set myhex.254=FE
set myhex.255=FF
Set up a counting loop.
set /A cntr=0
set /A scntr=256
:loop
Then substitute in the variable and output the results.
for /F "tokens=2 delims=^=" %%i in ('set myhex.%cntr% ^| find ".%cntr%=" ') do echo %%i
Which can be simplified by removal of the 'find' command to be.
for /F "tokens=2 delims=^=" %%i in ('set myhex.%cntr%') do echo %%i
And loop until finished.
set /A cntr=%cntr%+1
if NOT [%scntr%]==[%cntr%] goto loop
This technique can be used to read data or configuration from a text file, into an array.

This entry was posted on Wednesday, May 23, 2012 and is filed under , . You can leave a response and follow any responses to this entry through the Subscribe to: Post Comments (Atom) .

0 comments