L'auteur
Mike Gagnon Canada Membre Simple # 0000000025 enregistré le 14/10/2004 Gagnon Mike Pointe Cla H9R 3K8 de la société Carver Technologies Inc. Fiche personnelle
Note des membres
19/201 vote
Contributions > 09 - Automation > Windows Scripting
Fonctions utiles avec Windows Scripting
# 0000000099
ajouté le 26/11/2004 15:59:26 et modifié le 14/07/2008
consulté 17685 fois
Niveau
initié Version(s) Foxpro : VFP 7.0
Description
Ces exemples requiert que Windows scripting soit installé. Vous pouvez trouver Windows Scripting 5.x ici http://msdn.microsoft.com/library/default.asp?url=/downloads/list/webdev.asp
Code source :
&& Obtenir le nom du domaine, le nom de l'usager et le nom de l'ordinateur
WshNetwork = CreateObject ('WScript.Network' )
lcMessage='Domaine = ' + WshNetwork.UserDomain + CHR (13)
lcMessage=lcMessage+ 'Nom de l' ordinateur =' + WshNetwork.ComputerName+CHR(13)
lcMessage=lcMessage+ ' Nom de l'usager = ' + WshNetwork.UserName
MESSAGEBOX (lcMessage)
&& Créer un connection de réseau.
oNet = CreateObject ('WScript.Network' )
oNet.MapNetworkDrive('I' ,'\\myserver\myFiles' ,.T. ,'mike' ,'password' )
&& Enlever un connexion de réseau.
WshNetwork = CreateObject ('WScript.Network' )
WshNetwork.RemoveNetworkDrive('E' )
&& Ajouter un imprimante du réseau (ou locale)
oNet = createobject ('WScript.Network' )
oNet.AddWindowsPrinterConnection('\\ServerName\PrinterName' )
&& Changer l'imprimante par défaut dans Windows
oNet = CreateObject ('WScript.Network' )
oNet.SetDefaultPrinter('\\ServerName\PrinterName' )
&& Vérifier l'espace vide disponible sur un disque dur.
objFSO = CreateObject ('Scripting.FileSystemObject' )
objDrive = objFSO.GetDrive('C:' )
MESSAGEBOX ('Espace disponible: ' + chr (13)+TRANSFORM (objDrive.AvailableSpace,'999,999,999,999,999' +' kb' ))
&& Copier un fichier.
FSO = CreateObject ('Scripting.FileSystemObject' )
FSO.CopyFile('c:\COMPlusLog.txt' ,'c:\x\' )
&& Créer un dossier
fso = createobject ('Scripting.FileSystemObject' )
fldr = fso.CreateFolder('C:\MyTest' )
&& Supprimmer un dossier.
fso =createobject ('Scripting.FileSystemObject' )
fldr = fso.DeleteFolder('C:\MyTest' )
&& Vérifier l'existance d'un dossier
fso =createobject ('Scripting.FileSystemObject' )
? fso.FolderExists('C:\MyTest' )
&& Créer un fichier texte.
fso = CreateObject ('Scripting.FileSystemObject' )
f1 = fso.CreateTextFile('c:\testfile.txt' , .T. )
&& Créer un fichier texte et écrire dedans.
fso = CreateObject ('Scripting.FileSystemObject' )
tf = fso.CreateTextFile('c:\testfile.txt' , .t. )
tf.WriteLine('Testing 1, 2, 3.' )
tf.WriteBlankLines(3) && Skip three lines
tf.Write ('This is a test.' )
tf.Close
MODIFY FILE 'c:\testfile.txt'
&& a) Créer un icone sur le bureau.
oShell = CreateObject ('WScript.Shell' )
DesktopPath = oShell.SpecialFolders('Desktop' )
oURL = oShell.CreateShortcut(DesktopPath + '\MSDN Scripting.URL' )
oURL = oShell.CreateShortcut(DesktopPath + '\MSDN Scripting.URL' )
oURL.TargetPath = 'HTTP://MSDN.Microsoft.com/scripting/'
oURL.Save
&& b) Créer un icone sur le bureau.
Shell = CreateObject ('WScript.Shell' )
DesktopPath = Shell .SpecialFolders('Desktop' )
link = Shell .CreateShortcut(DesktopPath+'\test.lnk' )
link.Arguments = '1 2 3'
link.Description = 'test shortcut'
link.HotKey = 'CTRL+ALT+SHIFT+X'
link.IconLocation = 'app.exe,1'
link.TargetPath = 'c:\blah\app.exe'
link.WindowStyle = 3
link.WorkingDirectory = 'c:\blah'
link.Save ()
&& Créer une entrée dans le régistre.
oSh = CreateObject ('WScript.Shell' )
key = 'HKEY_CURRENT_USER\'
oSh.RegWrite( key + 'WSHTest\' ,'testkeydefault' )
oSh.RegWrite(key + 'WSHTest\string1' , 'testkeystring1' )
oSh.RegWrite( key + 'WSHTest\string2' , 'testkeystring2' , 'REG_SZ' )
oSh.RegWrite( key + 'WSHTest\string3' , 'testkeystring3' , 'REG_EXPAND_SZ' )
oSh.RegWrite( key + 'WSHTest\int' , 123, 'REG_DWORD' )
&& Supprimmer une entrée dans le régistre.
oSh = CreateObject ('WScript.Shell' )
oSh.RegDelete('HKCU\\Software\\ACME\\FortuneTeller\\MindReader' )
oSh.RegDelete('HKCU\\Software\\ACME\\FortuneTeller\\' )
oSh.RegDelete ('HKCU\\Software\\ACME\\' )
&& Un remplacement du MESSAGEBOX() de VFP.
WshShell = Createobject ('WScript.Shell' )
BtnCode = WshShell.Popup('Do you feel alright?' , 7, 'Answer This Question:' , 4 + 32)
Do Case
Case BtnCode=6
WSHSHELL.Popup('Glad to hear you feel alright.' )
Case BtnCode=7
WSHSHELL.Popup('Hope you' re feeling better soon.')
Endcase
&& Une boite de dialogue qui permet le sélection multiple de dossier. (Requiert Windows XP)
oDlg= Createobject(' userAccounts.commonDialog')
oDlg.flags= ' &h1a04'
oDlg.Filter= ' All Files |*.*|'+ ' Text and Batch Files |*.txt;*.bat'
oDlg.filterIndex= 2
oDlg.initialDir=' C:\'
qSln= oDlg.showOpen
If qSln
Messagebox(oDlg.fileName)
Else
Messagebox(' Action cancellée.')
Endif
&& Rouler une tache planifiée immediatement, au lieu d'attrendre qu'elle parte au temps spécifié.
ssfCONTROLS = 3 && Control Panel's Schedule Tasks folder
sJobName = ' fta' && Name of the task to run
sRunVerb = ' R&un' && Executing command
sEndVerb = ' &End Task' && Cancelling command
shellApp = Createobject(' shell .application')
oControlPanel = shellApp.Namespace(ssfCONTROLS) && Schedule Tasks folder
oST = ' '
For Each folderitem In oControlPanel.items && Loop though the items in the Control Panel items
If folderitem.Name = ' Scheduled Tasks'
oST = folderitem.getfolder() && Found it
Exit
Endif
Next
If Vartype(oST) != ' O'
Messagebox(' Couldn't find ' TS' folder' )
RETURN
Endif
oJob = ''
For Each folderitem In oST.items && Loop through the different scheduled tasks until we fiind it.
If Lower (folderitem.Name ) = Lower (sJobName)
oJob = folderitem && Found it
Exit
Endif
Next
If Vartype (oJob) !='O'
Messagebox ( 'Couldn' t find ' + sJobName + ' item')
Else
bEnabled = .T.
oRunVerb = ' '
oEndVerb = ' '
s = ' Verbs: ' + Chr(13)
For Each Verb In oJob.verbs && Loop through the different commands in the scheduled task until we find right one.
s = s + Chr(13) + Verb.Name
If Verb.Name = sRunVerb
oRunVerb = Verb
bEnabled = .F.
Endif
If Verb.Name = sEndVerb
oEndVerb = Verb
Endif
Next
If bEnabled
oJob.InvokeVerb(oEndVerb) && Cancel the task
Else
Wait Window Nowait ' executing job'
oJob.InvokeVerb(sRunVerb) && Run the task!
Endif
Endif
&& Ouvrir la boite de dialogue pour ajouter une imprimante. (Applet)
oShell = CreateObject("WScript.Shell")
oShell.Run("rundll32.exe shell32.dll,SHHelpShortcuts_RunDLL AddPrinter")
&& Ouvrir le panneau de control.
oShell = CreateObject(' WScript.Shell')
oShell.Run(' Control .exe Sysdm.cpl,,0') && Parametre pour spécifier quelle page a le focus
&& Ouvrir le panneau pour suprimmer les logiciels installés.
oShell = CreateObject(' WScript.Shell')
oShell.Run(' Control .exe AppWiz.cpl,,1')
&& Faire roule un programme ou ouvrir un fichier et attendre que l'application se terminer (ou le &&fichier se ferme.
WshShell = CreateObject("WScript.Shell")
ReturnCode = WshShell.Run("%windir%\notepad.exe", 1, .t.)
MESSAGEBOX("fini")
Commentaires
Aucun commentaire enregistré ...