dimanche 18 juillet 2010

fonctions utiles

voici quelques fonctions utiles


1/ extraire d'une chaîne de caractères uniquement les lettres /ou les nombres:
@Echo oFF
Setlocal EnableExtensions

Call :Xtract 65446gjkngd5343dflgd
set substr 
Endlocal & goto :eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Xtract  %1=chaîne de caractères  %2=paramètre optionnel [$str]
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Setlocal enableExtensions
If [%2]==[$str] Set "v1=-v" & set "v2=^"
Set is=%~1
For /F "Tokens=2 Delims=:" %%# In ('
  "%Comspec% /u /cEcho:%is%|More|FindStr -o .|FindStr %v1% ".*:[%v2%0-9]"
 ') do Call Set substr=%%substr%%%%#
Endlocal & Set "substr=%substr%" & goto :eof


voici quelques exemples:

Call :xtract "45ds8f9558fr5r585f854447r"  
echo %substr%

c:\> tst.bat

45895585585854447


Call :xtract "45ds8f9558fr5r585f854447r"  $str
echo %substr%

c:\> tst.bat

dsffrrfr 


2/ afficher les propriétés avancées d'un fichier:

:AdvancedProp
Setlocal EnableExtensions
If Not Exist "%~f1" Exit /B 2
For %%x In (
    "0:$Nom","1:$Taille","2:$Type","3:$Date.Modif"
    "4:$Date.Create","5:$Date.Access","6:$Attribut"
    "7:$Etat","8:$Proprietaire","9:$Auteur"
    "10:$Titre","11:$objet","12:$Categorie"
    "14:$Commentaire","15:$Copyright","16:$Artist"
    "17:$Titre.Album","18:$Annee","19:$Numero.Piste"
    "20:$Genre","21:$Duree","23:$Protege"
    "26:$Dimension","34:$Chaines","35:$Entreprise"
    "36:$Description","37:$Version.Fichier"
    "38:$Nom.Produit","39:$Version.Produit"
    "40:$Mots.Clefs" 
) Do For /F "Tokens=1* Delims=:" %%s In (%%x) Do (
   >$ Call :$Prop "%~1" %%s %%t 
     set "%%t="
     for /f "tokens=* delims=" %%a in ('
           "cscript -nologo -e:vbs $ & del $"
    ') Do %%a
     if defined %%t Set %%t
)
Endlocal & goto :EOF
:$Prop
echo Set o=createObject("shell.application").^
     namespace("%~dp1")
echo wsh.echo "set %3="^&o.^
     getdetailsof(o.parsename("%~nx1"),%2)
Exit /B 0


Call :AdvancedProp "%windir%\explorer.exe"


3/ Lire un fichier à l'envers:

:fileRev
for /f "tokens=1* delims=][" %%s in ('

find /n /v "" ^< %1') do (
    Set "tab[%%s]=%%t"
    if not defined tab[%%s] set tab[%%s]=µµµ
    Set nline=%%s
)
for /l %%i in (%nline% -1 1) Do (
  for /f "tokens=1* delims=µ=" %%_ in ('

set tab[%%i]

') do echo=%%_
)
goto:eof


Call :fileRev "mon fichier de test.txt" 


4/ Convertir une chaîne de caractères en majuscule:

:ucase
for /f "tokens=1* delims=µ" %%a in ('
  "tree \µµµ%1|find ":\""
  ') do set "ucase=%%b"
goto:eof

 

Call :ucase "tunisia"


5/ Inverse une chaîne de caractère:

:reverse
for /f "tokens=1* delims=:" %%a in ('
    "%comspec% /u /c echo:%~1|more|findstr /o ."
 ') do set "reverse=%%b!reverse!"
goto:eof

 

 Call :reverse "vive palestine" 


6/ a* Convertir un nombre décimal en hexa

:tohexa
Setlocal enableextensions
set hx=0x0
Set exp=%comspec% /cexit/b %1^&set;^|find "^=ExitCode^="
for /f "tokens=1* delims=0=" %%a in ('
      "%exp%" % expression evalue %
') do Set hx=0x%%b
Endlocal & Set "hex=%hx%" & goto :eof


 set hex=98
 set hex
 Call :tohexa %hex% && Set hex   


6/ b* Convertir un nombre décimal en hexa:

:tohexa
for /f "tokens=1,3" %%i in ('^(
 reg add hkcu\console /v hexa /t reg_dword /d %1 /f ^&
 reg query hkcu\console /v hexa  ^&
 reg delete hkcu\console /v hexa /f
 ^)^|find "REG_DWORD"') do Set %%i=%%j
goto:eof

 

set hexa=685

set hexa & Call :tohexa %hexa% & set hexa



7/ Compter le nombre de répétition d'une lettre:

:CountChar
for /f %%: in ('
  "%comspec% /u /c echo:%~1|more|sort"
 ') do Set /A "$[%%:]+=1"
goto:eof


Call :CountChar "tunisia and africa"
Set $[


8/ Supprimer les espaces de plus en debut/fin d'un string:

@echo off 
(set Ch=    ceci    est un simple test   )
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
set path=%path%;D:\Program Files\GnuWin32\bin
set e="s_^\s*__;s_\s*$__;s_^_del %%0\&set %%1=&_w ~.Cmd"
echo [%Ch%]
echo\%Ch%|1>&0 sed %e%
2>Nul=(call ~.Cmd ch)
echo [%Ch%]&goto:EOF :::::GnuSED Version 4.2::::::::::::::::

Aucun commentaire: