and more about cmd.exe and batch files #36

npocmaka opened this Issue Sep 17, 2015 · 0 comments


None yet

1 participant


For a lot of lines batch cell is set to none or is empty (or incomplete) though there are ways the limitations to be worked around with hacks/functions and etc.

And I'm not handy with markdown formatting so I suppose you should also read this as plain text also.

0.version - ver command - will print the version of windows. Also %CMDEXTVERSION% variable prints the version of the command prompt which hasn't been changed since windows 2000

1.getting CPU usage - in batch script use double % :
typeperf "\Processor(_Total)% Processor Time" -sc 1

2.List all variables
set " -> this will list more variables (though is rather a hack)

there's a hack that allows you to call a function from another batch:

4.environment variable
environment variables are imported to the cmd and they can be accessed with %variable% -or- with delayed expansion !variable!

5.Getting the username:
echo %username%

6.command substitution
doskey ls=dir /b . though doskey macros cannot be used in batch file


mshta "javascript:code(close((V=(v=new ActiveXObject('SAPI.SpVoice')).GetVoices()).count&&v.Speak('hyperpolyglot')))"

8.Command path:
for %%a in ("cmd.exe") do @echo %%~f$PATH:a

9.there are lot of ways to start a background process but requires a more complicated function script

10.suspend should be ctrl+c

11.terminate process:
taskkill /im cmd* /f
tskill cmd

12.list processes

  1. dirname:
    for %%# in ("./somedir/somefile") do echo %%~dp#
  2. basename:
    for %%# in ("./somedir/somefile") do echo %%~nx#
  3. directory test (the trick is in the backslash at the end):
    if exist somedir\ echo directory
  4. Create symlink
    mklink command available from vista and above readable, is writeable, is executable

attrib command should be used here -

  1. file size - better way

for %%# in ("somefile") do echo %%~z#

  1. Iterate file line by line:
    This will skip the empty lines (and the lines that contain only delimiters) but in most of the cases will do the job .There are more robust ways but require more scripting

setlocal enableDelayedExpansion
set counter=1
for /f "useback tokens=* delims=" %%# in ("c:\somefile.txt") do (
(echo(line !counter!:%%#)
set counter=counter+1

19.write file to stdout

type someFile.txt

  1. Break a loop

despite there's a bug in FOR /L and breaking does not work so good with large numbers -
but for other FOR loops will work

for /l %%# in (1;1;10) do (
if %%# equ 5 (
goto :break_for
echo %%#

21.default scope

scope can be limited with ENDLOCAL and SETLOCAL commands -

22.Anonymous functions are possible with a hacks...
one of them are the macros -

the other way is to abuse one bug in CALL function - this one is more close to the anonymous function definition

  1. Formatting date and time . there are few ways but require more scripting -
  2. string comparison

set string1=zig
set string2=zag

if %string1% equ %string2% echo strings are identical

  1. String replication

@echo off
setlocal enableDelyedExpansion
set "string=_"
set "result="
for /l %%# in (1 , 1 , 80 ) do (
set "result=!result!!string!"
echo %result%

  1. split is possible with pure batch but requires more complicated function -
  2. Cases
    to upper

@echo off
setlocal enableDelayedExpansion
set string="AaBbCC"
for %%# in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do set string=!string:%%#=%%#!
echo %string%

to lower

@echo off
setlocal enableDelayedExpansion
set string="AaBbCC"
for %%# in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do set string=!string:%%#=%%#!
echo %string%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment