Bonjour, je voudrai faire un script pour supprimer tout les raccourci nul sur la bureau, pour ca il faut que je récupére la cible des racourci pour tester si le fichier pointer existe.
Pouriez vous me dire comment récupérer la cible d'un raccourci ?
Version imprimable
Bonjour, je voudrai faire un script pour supprimer tout les raccourci nul sur la bureau, pour ca il faut que je récupére la cible des racourci pour tester si le fichier pointer existe.
Pouriez vous me dire comment récupérer la cible d'un raccourci ?
Est ce que je t'ai déjà parler de l'outil WMICreator ?
Il est très pratique, regarde dans les contributions.
Avec le Namespace : root\CIMV2
Et la classe : Win32_ShortcutFile
J'ai trouvé la propriété et le code adéquate pour lister tous les raccourcis du bureau et obtenir le chemin de la cible.
La propriété est Target qui veut dire cible en anglais ;)
Code:
 1
2
3
4
5
6
7
8
9
10 strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery( _ "SELECT * FROM Win32_ShortcutFile",,48) For Each objItem in colItems Wscript.Echo "-----------------------------------" Wscript.Echo "Win32_ShortcutFile instance" Wscript.Echo "-----------------------------------" Wscript.Echo "Target: " & objItem.Target Next
merci beaucoup ;)
mais pour l'instant le voudrai juste le code pour obtenir la cible d'un seul raccourci, je me débroulerrai aprés
j'avou que j'ai du mal à suivre :oops:
Je n'ai pas testé mais cela devrait donner un truc dans le genre :
Code:
 1
2
3
4
5
6
7
8
9
10
11
12 Dim NomRaccourci NomRaccourci = "youpi" ' nom du raccourci strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery( _ "SELECT * FROM Win32_ShortcutFile WHERE name = '" & NomRaccourci & "'",,48) For Each objItem in colItems Wscript.Echo "-----------------------------------" Wscript.Echo "Win32_ShortcutFile instance" Wscript.Echo "-----------------------------------" Wscript.Echo "Target: " & objItem.Target Next
j'ai mi ceci:
et j'ai cette erreur:Code:
 1
2
3
4
5
6
7
8
9
10
11
12 Dim NomRaccourci NomRaccourci = "C:\Documents and Settings\Kookatix\Bureau\Exemples.lnk" ' nom du raccourci strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery( _ "SELECT * FROM Win32_ShortcutFile WHERE name = '" & NomRaccourci & "'",,48) For Each objItem in colItems Wscript.Echo "-----------------------------------" Wscript.Echo "Win32_ShortcutFile instance" Wscript.Echo "-----------------------------------" Wscript.Echo "Target: " & objItem.Target Next
http://hebergement-images.voiloo.net...a9f9120545.jpg
cela marchera mieux ainsi :
Code:
 1
2
3
4
5
6
7
8
9
10
11
12 Dim NomRaccourci NomRaccourci = "Exemples" ' Nom du raccourci et juste le nom :D strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery( _ "SELECT * FROM Win32_ShortcutFile WHERE name = '" & NomRaccourci & "'",,48) For Each objItem in colItems Wscript.Echo "-----------------------------------" Wscript.Echo "Win32_ShortcutFile instance" Wscript.Echo "-----------------------------------" Wscript.Echo "Target: " & objItem.Target Next
Bon je me suis trompé :oops:
WMICreator me dit que c'est ainsi :
Code:
 1
2
3
4
5
6
7
8
9
10 strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery( _ "SELECT * FROM Win32_ShortcutFile WHERE Name = 'c:\\documents and settings\\all users\\start menu\\security\\virusscan.lnk'",,48) For Each objItem in colItems Wscript.Echo "-----------------------------------" Wscript.Echo "Win32_ShortcutFile instance" Wscript.Echo "-----------------------------------" Wscript.Echo "Target: " & objItem.Target Next
ok c'est parfait ;)
merci encore :D
j'ai garder ceci:
Code:
 1
2
3
4
5
6
7 strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery( _ "SELECT * FROM Win32_ShortcutFile WHERE Name = 'C:\\Documents and Settings\\All Users\\Menu Démarrer\\Programmes\\Accessoires\\Calculatrice.lnk'",,48) For Each objItem in colItems Wscript.Echo "Cible: " & objItem.Target Next
Ok pour que cela soit plus simble, tu peux redépalcer le chemin dans NomRaccourci, mon erreur été de ne vouloir utiliser que le nom, et la tienne était de n'avoir pas doublé les \
Mais tu peux t'amuser un peu si tu veux, et faire des choses simples comme :
Enfin cela sert à rien vu que tu es déjà dépendant du lecteur avec le chemin.Code:
 1
2
3
4
5
6
7
8
9
10
11
12 strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery( _ "SELECT * FROM Win32_ShortcutFile WHERE Name = 'c:\\documents and settings\\all users\\start menu\\communication\\ftp.lnk'",,48) For Each objItem in colItems If objItem.Drive = "c:" Then Wscript.Echo "-----------------------------------" Wscript.Echo "Win32_ShortcutFile instance" Wscript.Echo "-----------------------------------" Wscript.Echo "Target: " & objItem.Target End If Next
Dès fois je vous jure je dis n'importe koi :aie:
j'en sui la mais ce ne marche pas:
Code:
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 set WshShell = WScript.CreateObject("WScript.Shell") DesktopPath = WSHShell.SpecialFolders("Desktop") Set oFSO = CreateObject("Scripting.FileSystemObject") stRep = DesktopPath If oFSO.FolderExists(stRep) Then For each oFl in oFSO.GetFolder(stRep).Files if LCase(stExt) = "lnk" then vrChmRccrc = replace(oFl.Path,"\","\\") strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery( _ "SELECT * FROM Win32_ShortcutFile WHERE Name = vrChmRccrc",,48) For Each objItem in colItems Wscript.Echo "Cible: " & objItem.Target Next Else End If 'vrFchs = vrFchs & vbCrLf & oFl.Name 'vrChmNomFchs = vrChmNomFchs & vbCrLf & oFl.Path Next End If
oui mais la plu par du temps tu donne des codes qui fonctionnent ;)
j'ai ajouter quelque chose mais j'ai une erreu à la ligne 22
Code:
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 set WshShell = WScript.CreateObject("WScript.Shell") DesktopPath = WSHShell.SpecialFolders("Desktop") Set oFSO = CreateObject("Scripting.FileSystemObject") stRep = DesktopPath If oFSO.FolderExists(stRep) Then For each oFl in oFSO.GetFolder(stRep).Files 'msgbox oFl.Path stExt = oFso.GetExtensionName(oFl) if LCase(stExt) = "lnk" then vrChmRccrc = replace(oFl.Path,"\","\\") strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery( _ "SELECT * FROM Win32_ShortcutFile WHERE Name = vrChmRccrc",,48) For Each objItem in colItems Wscript.Echo "Cible: " & objItem.Target Next Else End If 'vrFchs = vrFchs & vbCrLf & oFl.Name 'vrChmNomFchs = vrChmNomFchs & vbCrLf & oFl.Path Next End If
tu cherche un fichier qui se nomme :vrChmRccrc ... ?Code:"SELECT * FROM Win32_ShortcutFile WHERE Name = vrChmRccrc",,48)
il manquer des apsotrophe:
la variable elle vien de la:Code:"SELECT * FROM Win32_ShortcutFile WHERE Name = 'vrChmRccrc'",,48)
Code:
 1
2vrChmRccrc = replace(oFl.Path,"\","\\")
Il manque aussi .. l'opérateur de concaténation de chaine ... : & ..! et il faut sortir la variable des " ...
mais bon .... Cedric .. ta montré le bon code un peu plus haut...! suffit de changer le nom de ta variable...
ce code devrai fonctionner il me semble:
Code:
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 set WshShell = WScript.CreateObject("WScript.Shell") DesktopPath = WSHShell.SpecialFolders("Desktop") Set oFSO = CreateObject("Scripting.FileSystemObject") stRep = DesktopPath If oFSO.FolderExists(stRep) Then For each oFl in oFSO.GetFolder(stRep).Files stExt = oFso.GetExtensionName(oFl) if LCase(stExt) = "lnk" then vrChmRccrc = replace(oFl.Path,"\","\\") 'msgbox vrChmRccrc strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ShortcutFile WHERE Name = '" & vrChmRccrc & "'",,48) For Each objItem in colItems If oFSO.FolderExists(objItem.Target) Then Else Set fsoFile = ofso.getFile(oFl.Path) fsoFile.delete End If Wscript.Echo "Cible: " & objItem.Target Next Else End If Next End If
il me semble aussi mais est ce qu'il fontionne ? ;)
Un truc pour ne pas faire de If vide et mettre que du code dans le else:
Tu peux faire :Code:
 1
2
3
4
5 If oFSO.FolderExists(objItem.Target) Then Else Set fsoFile = ofso.getFile(oFl.Path) fsoFile.delete End If
Not va négationner ton test (oui oui c certainement faux et si c vrais c un cou de bol) et donc quand ton résultat de test sera vrais, cela va devenir faux avec le test, et inversement.Code:
 1
2
3
4 If Not oFSO.FolderExists(objItem.Target) Then Set fsoFile = ofso.getFile(oFl.Path) fsoFile.delete End If
Donc avec une méthode FolderExists :
Si le répertoire existe, cela renvois normalement vrai et avec Not cela donnera faux comme résultat de test.
Et je rappelle que l'on ne rentre dans la condition que si l'on obtiens vrai par le test.
Donc dans notre cas, Not permettra à la condition d'entrée dans le test de n'être vrai que si le fichier n'existe pas.
en me parlent de folder tu m'a fait remaqruer que j'aurai du mette file puisque c'est un fichier que je teste, mais le code ne fonctionne pas pour autant, leaurait du fonctionner luiCode:
 1
2Wscript.Echo "Cible: " & objItem.Target
Affiche la propriété count de colItems :
Cela te donnera la taille de la liste.Code:
 1
2
3 Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ShortcutFile WHERE Name = '" & vrChmRccrc & "'",,48) wscript.echo colItems.count For Each objItem in colItems
Si cela vaut 0, cela veut dire que la requête, "SELECT * FROM Win32_ShortcutFile WHERE Name = '" & vrChmRccrc & "'", ne te renvois aucun élément.
Donc soit elle est fausse soit vrChmRccrc contient un nom de fichier inexistant.
Il faut alors vérifié avec msgbox vrChmRccrc. Mais tu sembles l'avoir déjà fait.
Comme tu l'a dit c'est faux :DCode:If oFSO.FolderExists(objItem.Target) Then
Je ne l'avais pas vu mais il faut bien utiliser FileExist.