hyperpolyglot
hyperpolyglot copied to clipboard
and more about cmd.exe and batch files
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)
3.Libraries there's a hack that allows you to call a function from another batch: http://stackoverflow.com/a/30170342/388389
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:
whoami
-or-
echo %username%
6.command substitution doskey ls=dir /b . though doskey macros cannot be used in batch file
7.speech
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 -or- tskill cmd
12.list processes tasklist -or- qprocess
- dirname: for %%# in ("./somedir/somefile") do echo %%~dp#
- basename: for %%# in ("./somedir/somefile") do echo %%~nx#
- directory test (the trick is in the backslash at the end): if exist somedir\ echo directory
- Create symlink mklink command available from vista and above
16.is readable, is writeable, is executable
attrib command should be used here - http://ss64.com/nt/attrib.html
- file size - better way
for %%# in ("somefile") do echo %%~z#
- 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
- Break a loop
despite there's a bug in FOR /L and breaking does not work so good with large numbers - http://www.dostips.com/forum/viewtopic.php?f=3&t=5778 but for other FOR loops will work
for /l %%# in (1;1;10) do ( if %%# equ 5 ( goto :break_for ) echo %%# ) :break_for
21.default scope
scope can be limited with ENDLOCAL and SETLOCAL commands - http://ss64.com/nt/setlocal.html
22.Anonymous functions are possible with a hacks... one of them are the macros - http://ss64.com/nt/syntax-macros.html
the other way is to abuse one bug in CALL function - this one is more close to the anonymous function definition https://github.com/npocmaka/batch.scripts/blob/master/discovers_and_bugs/discovers/anonymous.bat
- Formatting date and time . there are few ways but require more scripting - http://stackoverflow.com/a/19799236/388389
- string comparison
set string1=zig set string2=zag
if %string1% equ %string2% echo strings are identical
- String replication
@echo off setlocal enableDelyedExpansion set "string=_" set "result=" for /l %%# in (1 , 1 , 80 ) do ( set "result=!result!!string!" ) echo %result%
- split is possible with pure batch but requires more complicated function - https://github.com/npocmaka/batch.scripts/blob/master/stringutils/split.bat
- 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%