Show Menu
Cheatography

CMD Cheat Sheet (DRAFT) by

Windows command line

This is a draft cheat sheet. It is a work in progress and is not finished yet.

DIR [path] [/A:at­trib] [/O:sort] [opt]

path
display specific folder (c:\...)
/A
:H(iden) :D(folder) :A(ll)...
/O
:N :S :E(xt) :D(ate)
options
/S(all sub fold)/­B(que le nom)
DIR abcd* /s /b
search for filename starting with abcd in current and all subs & display path only
DIR *abcd* /s /b
search for filenames containing abcd in current and all subs & display path only
DIR *.bat
search for *.bat files

ATTRIB [+attr | -attr] [path]­\file [opt]

DIR-like qui display les fichiers + leurs attrib.
ATTRIB
display [Attribs] [path of file]
[+X | -X]
set attrib: R(eadO­nly­),H­(idden)
/S set matching file in current and all sub fol
/D
process folder as well

TYPE [PATH]­\ra­ndo­m.txt (MORE)

Display text file
.txt .py .c ...
MORE is TYPE in better

MD nomfichier (MKDIR)

Crée un nouveau fichier
MD do re mi
crée ces 3 fichier dans le CD
MD [path]\re
crée le fichier re dans [path]
MD \do\re\mi
crée l'arbo ..\do­\re\mi

RD nomfichier (RMDIR)

Delete folder
RD \do\re\mi
del mi in ..\do­\re\mi
RD /S \do
after asking, del folders tree

COPY (files not folders)

COPY = Copy & past
COPY src.doc newfil­e.doc
Copy a file in the current folde
COPY src.txt c:\som­whe­re­\els­e\n­ew.txt
Copy from location to smhr else
COPY c:\tem­p\s­rc.txt c:\doc­\ne­w.txt
Copy from some folder to another folder
COPY c:\myw­ork­*.doc
Copy all the *.doc into current directory
COPY d:\wor­k\o­ldf­ile.wp
Copy to current, keep smpe fname
COPY "­c:\bull shit\calu ca va.txt­"
If spaces in names use quotes " "

DEL [path]­\file

Delete one or more files
DEL [path]­\do.txt
del do.txt in [path]
DEL [path]­\do.txt [path]­\re.txt [path]\mi.txt
del do.txt, re.txt and mi.txt (can be != loc)
DEL /S c:\test
del all files in ..\test

MOVE (folders or file)

MOVE = cut & past
MOVE oldfile.wp newfil­e.doc
In the current folde
MOVE c:\tem­p\o­ld.wp c:\wor­k\n­ew.doc
from specific place to antoher
MOVE c:\tem­p\o­ldf­ile.wp
move the file to current (keep same fname)

create­/manip text files (txt,p­y,c...)

type NUL>ne­w.txt
make empty new txt file
echo bla>ne­w.txt
make new.txt with bla
echo bla>>o­ld.txt
add "­bla­" to old.txt
 

SET

Display, set or remove CMD env var. Changes made with SET will remain only for the duration of the current CMD session.
c:\>SET var=poke
c:\>echo %var%
poke
c:\>SET var=c:\test
c:\>echo %var%
c:\test
c:\>cd %var%
c:\tes­t>_
 
/A=opé­rations
SET /A nb=3
SET /A nb=%nb%+1
echo %nb% 4
 
/P=prompt user
SET /P var=[p­rompt string]
 
divers:
SET var=one two
OK

Redire­ction

cmd > filename
dir>new.txt
redirect cmd output>file
cmd >> filename
append into a file
cmd < filename
sort<r­and.txt

sort file & print result
cmdA & cmdB
run cmdA then cmdB
cmdA && cmdB
run cmdA, if it suceeds then run cndB
cmdA || cmdB
run cmdA, if it fails then run cmdB
cmdA && cmdB || cmdC
If cmdA succeeds run cmdB, if it fails cmdC

Argume­nts­/Pa­ram­etres (batch)

Utile pour les scriptes avec paramètres
c:\>pr­int­Var.bat viande rouge
viande rouge
c:\>_
le fichier batch contient:
set arg1=%1
set arg2=%2
echo %arg1% %arg2%
%0-9

Compar­aisons

NEQ (!=)
EQU (==)
LSS (<)
LEQ (<=)
GTR (>)
GEQ (>=)

Divers

REM comment...
comment
date /t
display date
time /t
display time
cls
clear screen
@echo off (batch)
turn off print of each line
set /A n=%random% %%100 +1
n=random n [1;100]

FINDSTR

FINDSTR "­Joe­" Contac­ts.txt
recherche la str Joe dans Contac­ts.txt
FINDSTR /C:"Joe Poulet­" Contac­ts.txt
"­/C:­" n'est pas une adresse c'est une option (inclue l'espace dans la recherche de str)
FINDSTR /S /I "­wor­k" *.*
search in every file and filename in current and subs for the word work
OPTIONS: /S(search in subs) /I( ignore case)
/A:2f (filenames in colour (2f is the colour))
 

Precur­seurs

setlocal Enable­Del­aye­dEx­pansion
set i=2
echo %elem{%i%]%
Doesn't give the desired result because it means: show the value of the elem [variable, followed by i, followed by the value of the ] variable. To solve this problem you must use Delayed Expansion: Enclose index variables in percent symbols, and enclose the array elements in exclam­ation marks:
set elem[1­]=First element
set elem[2­]=S­econd one
set elem[3­]=The third one
set i=2
echo !elem[­%i%]!
setlocal Enable­Ext­ensions

ex1 creeva­r.bat [nomvar] [valvar]

@echo off
setlocal EnableDelayedExpansion
if "2"=="" goto erreur
set %1==%2
goto reussi
:erreur
    echo parametre manquant
    goto fin
:reussi
    echo variable definie
    set%1
    echo la variable %1 est definie et vaut !%1!
:fin

ex2 loop print liste

@echo off
for /L %%i IN (1,1,10) do echo %%i

ex3 affiche nombres entre 1 et N

@echo off
:debut
    set /P saisie=nombre plus grd que 0 svp:
    set /A valeur=%saisie%
    if %valeur% LEQ 0 goto erreur
    for /L %%i IN (1,1,%valeur%) DO echo %%i
    goto fin
:erreur
    echo plus grd que 0
    goto debut
:fin

ex 4 guess number

@echo off
set /A n=%random% %%100 + 1
set essais=0
:boucle
    echo. //empty line
    set /A essais=essais+1
    set /P m="[%essais%] nb entre 1 et 100:"
    if %m%==%n% goto trouve
    if %m%==[?] goto abandon
    if %m%LSS%n% echo %m% trop petit
    if %m%GTR%n% echo %m% trop grd
goto boucle
:trouve
    echo gg %m% est la bonne reponse
    goto fin
:abandon
    echo Abandon. Le nm etait: %n%
:fin

Ex 5 disp nb entre 1 et n&­car­re&cube

@echo off
setlocal EnableDelayedExpansion
setlocal EnableExtensions
:debut
set /P saisie=n plus grd que 0:
set /A valeur=%saisie%
if %valeur% LEQ 0 goto erreur
for /L %%i in (1,1,%valeur%) do (
    set /a carre=%%i * %%i
    set /a cube=!carre! * %%i
    echo %%i !carre! !cube!
)
goto fin
:erreur
    echo un nb + grd que 0
    goto debut
:fin

ex6 tri d'une série de mots

@echo off
setlocal EnableDelayedExpansion
setlocal EnableExtensions
echo "Explications"
set nombre_mots=1
if exist mots.txt del mots.txt
:saisie
    set mots=
    set /P mots="[%nombre_mots%] "
    if "%mots%" == "" goto fin_saisie
    echo %mots% >> mots.txt
    set /A nombre_mots=nombre_mots+1
    goto saisie
:fin_saisie
    echo.
    echo ===Liste entree au clavier===
    type mots.txt
    echo ===Liste triee============
    sort mots.txt
    echo ===Listre triee inv ========
    sort /r mots.txt

ex7 recherche avancée en fonction param à l'exec

setlocal EnableDelayedExpansion
setlocal EnableExtensions
echo Ligne de commande: %0 %*
if "%2"=="" goto erreurParam    //verification para
if "%1"=="" goto erreurParam    //''
set dossier=%1
if exist %dossier%NUL goto dossier_existe
set dossier=%1\
if exist %dossier%NUL goto dossier_existe
goto erreur_dossier
:dossier_existe
    echo Scanning ...
    dir /S /B %dossier%^%2 >liste.txt
    echo Repertoire: %dossier%
    echo Fichier: %2
    Liste des fichiers trouves:
    type liste.txt
    echo Nombre de fichiers:
    type liste.txt | find /C /V ""
    goto fin
:erreur_dossier
    echo %1 n'existe pas
    goto fin
:erreur_param
    echo Parametres manquants
    goto fin
:fin