CMD - auto-mate/CheatSheetWiki GitHub Wiki
- Clip
- Dir of Sub Dirs
- Escape Charachters
- FOR List
- FINDSTR
- Groups
- HEAD
- IF
- Joining Commands
- Keyboard Shortcuts
- Pipes
- Prompt - Changing
- Redirect Output
- Substring
- Sleep
- Taskkill
- WMIC
- Useful Commands
- User ACL Functions
- Variables Set
NB Single % at prompt %% in bat file
Use list which results from a command (command in single quotes '')
FOR /F %a IN ('dir /b') DO dir %a
runs dir /b (bare directory listing as a command because it's in single quotes ‘’)
e.g. fileA
fileB
then for each item then runs dir on that item.
e.g. Dir fileA
results......
Dir fileB
results......
etc..
NB use for /F "tokens=1*" %a in ('dir') do echo %a%b to show whole row as %a will just show first item delimited by space
Use list which is a string
for %a in ("A Broken Clamp" is no good) do @echo %a
"A Broken Clamp"
is
no
good
**OR**
FOR %%t IN (a b c d) DO (Echo %%t & Echo OK)
NB use %~a to remove surrounding quotes "" see FOR /? for more expansions
Use Steps
::Enter as (From,Step Size,Maximum)
for /L %a in (1,3,12) do @echo %a
1
4
7
10
Additional Examples
Update SET Variable From Counter Loop and In List Loop (Sends Value TO Subroutine) [In a bat file]
REM Counts FROM 2 to 56 interval of 1 & Calls :SubroutineOne with variable %%x OR %1 when it is in the sub
REM ######################################################################################################
FOR /L %%x IN (2,1,56) DO CALL :SubroutineOne %%x
SET SQL=USE Database; CREATE TABLE Database.dbo.%TEMPTABLE% ([1] Varchar(MAX) %SQL% )
REM Chooses Items in List & Calls :SubroutineTwo with variable %%i OR %1 when it is in the sub
REM ##########################################################################################
FOR %%i in (6,7,8,35,36,39,40,48,49,50,51,52,53,54,55) DO CALL :SubroutineNegVals %%i
goto:eof
:SubroutineOne
SET SQL=%SQL% ,[%1] varchar(max)
:: goto:eof is like end of file or end of subroutine!
goto:eof
:SubroutineTwo
sqlcmd -S SERVER\SQLEXPRESS -E -Q "update Database.dbo.TABLEname SET [%1]='A Value' WHERE [%1]='Some Criteria'"
sqlcmd -S SERVER\SQLEXPRESS -E -Q "update Database.dbo.TABLEname SET [%1]= replace([%1],' ','')"
goto:eof
IF [NOT] string1==string2 (
CMD1....
) ELSE (
CMD2....
)
IF DEFINED variable cmd
IF string1 comp string2 cmd
comp values EQU NEQ LSS LEQ GTR GEQ
/I case insensitive
& (Runs multiple commands e.g. "DIR & PAUSE")
^ (Allows multiple lines e.g. "DIR & ^" )
(2nd line.................. "more? DIR" ) [Runs 2 Dir Listings]
Use to break line
Dir^
Dir
NB no space after ^ and space on new line!
Prompt $P$G creates std prompt _"drive:\path...>"_ **WHERE** $P=Path $G =>
_COMMAND_ 1> newfile.txt = STDOUT to newfile.txt
_COMMAND_ 2> errfile.txt = STDERR to errfile.txt
SET X=SOMETHING
REM Substitue as follows
ECHO %X%
REM Echos THING
SET X=SOMETHING
ECHO %X:~4,5%
Below will return all lines in directory listing containing "txt"
DIR | FINDSTR /C:"txt"
Below will return all lines of tasks containing "iexplore"
TASKLIST | find "iexplore"
View a list of exe file in notepad
dir /b | find ".exe" > findlist.txt | notepad findlist.txt
Kill a PID with /T terminate child processes and /F Force
Taskkill /PID 1234 /T /F
Kill all where FI (filter) equals 'iexplore.exe*'
taskkill /F /FI "Imagename eq iexplore.exe*"
Kill every instance of notepad NB backquotes allow command to be looped, "delims == " i.e. space chops on whitespace.
FOR /F "usebackq delims== " %i IN (`tasklist`) DO IF %i == notepad.exe (taskkill /F /IM notepad.exe)
Additional Info Data Source and Actions
Sleep for 15 Seconds [NB May need to use START For Prior Command to get command to run as Sync]
C:\Windows\...\sleep.exe 15
Copies to clipboard
clip < file.txt
Or
dir | clip
@echo off runs command without showing it.
charmap (window of fonts showing each letter)
start . (opens file explorer in the current directory)
timeout.exe /T Secs (Waits Seconds)
F7 Select a previous command
ctrl+end clear from cursor to end of line
ctrl+home clear from cursor to start of line
ctrl+a select whole command
shft+end select to end of line
shft+home select to start of line
shft+up/down select up and down
ctrl+shft+end select to end of console
ctrl+shft+home select to start of console
ctrl+right skip to next word in command
ctrl+left skip to previous word in command
ctrl+up/down scroll
ctrl+left skip to previous word in command
FINDSTR "my search" a_file.txt ::finds rows with "my" OR "search" in them FINDSTR /C:"my search" a_file.txt ::finds rows with "my search" in them
Main Switches
/B match pattern at beginning of row
/E match pattern at end of row
/R regex e.g. ".=any" "*=repeat prev" "[a-z]=range" "^=beginning of row" "$=end of row"
To show membership of groups
1. press windows key + r
2. type cmd and press return
3. in the console type gpresult /R > %USERPROFILE%\Desktop\groups.txt
4. View file groups.txt that should be on your desktop
DIR C:\path /B /AD /S
Use to break line
Dir^
Dir
NB no space after ^ and space on new line!
Per Rob Vanderwoude @ https://www.robvanderwoude.com/escapechars.php
Col1 To Escape
Col2 Sequence to use
% %%
^ ^^ May not always be required in doublequoted strings, but it won't hurt
& ^&
< ^<
> ^>
| ^|
' ^' Required only in the FOR /F "subject" (i.e. between the parenthesis), unless backq is used
^
Required only in the FOR /F "subject" (i.e. between the parenthesis), if backq is used
, ^, Required only in the FOR /F "subject" (i.e. between the parenthesis), even in doublequoted strings
; ^;
= ^=
( ^(
) ^)
! ^^! Required only when delayed variable expansion is active
" "" Required only inside the search pattern of FIND
\ \ Required only inside the regex pattern of FINDSTR
[ [
] ]
" "
. .
* *
? ?
icacls <folder>\*. /findsid "domain\username" /T /C /L
*. = directory
/T = recursive
/C = Continue on Fail
/L = Includes Symbolic Links
icacls <folder> /remove "domain\username" /T /C
/T = recursive
/C = Continue on Fail
NB No \ after folder name
powershell -command "& {get-content "c:\Temp\somefile.txt" -first 10}"