Author: Thomas Block
Dies ist eine Sammlung von Scriptanweisungen in VBS und PERL.
| Aktion | VBS | PERL | DOS |
|---|---|---|---|
| Hilfsvariablen: |
itemPath = "c:\temp"
itemName = "test"
set oFs = WScript.CreateObject("Scripting.FileSystemObject")
|
$itemPath = "c:\\temp\\";
$itemName = "test";
|
set itemPath = "c:\\temp\\"
set itemName = "test"
|
| Datei Existenz: |
if oFs.FileExists( itemPath & "\" & itemName ) then
' Aktion
endif
|
if( -e "$itemPath$itemName" ) {
#---- Datei existiert
}
|
|
| Datei lesend öffnen: |
' nur lesend (STANDARD)
set oFileIn = oFs.OpenTextFile( itemPath & "\" & itemName, 1 )
|
open( FILE, "$itemPath$itemName <" ); | |
| Datei schreibend öffnen: |
' 2 => Daten überschreiben; 8 => Daten anhängen
set oFileOut = oFs.OpenTextFile( itemPath & itemName, 2 )
|
open( FILE, "$itemPath$itemName >" ); | |
| Zeile lesen: | data = oFileIn.Readline | $data = < FILE >; | |
| Alle Zeilen lesen: |
data = oFileIn.Readall oder
Do Until objTextFile.AtEndOfStream
data = data & objTextFile.Readline
Loop
|
@var = < FILE >; | |
| In Datei schreiben: |
oFileOut.Writeline "data"
|
#---- goto offset from beginning of file
seek FILE_OUT, $offset, 0;
#---- save data in tmp
print FILE_OUT "data";
|
|
| Datei schließen: |
oFileOut.Close
|
close( FILE ); | |
| Datei löschen: |
set oItem = oFs.GetFile( itemPath & "\" & itemName )
oItem.Delete
|
`del $itemPath$itemName`;
|
del %itemPath%%itemName%
|
| Datei verschieben: | src = itemPath & "\" & itemName dst = "c:\temp\neu.txt" set oFs = WScript.CreateObject( "Scripting.FileSystemObject" ) set oItem = oFs.GetFile( src ) oItem.Move( dst ) |
$src = $itemPath$itemName;
$dst = "c:\\temp\\neu.txt";
`move $src $dst`;
|
set src=%itemPath%%itemName% set dst = "c:\temp\neu.txt" move %src% %dst% |
| Verzeichnis Existenz: |
if oFs.FolderExists( itemPath ) then
' Aktion
endif
|
if( -d "$itemPath" ) {
#---- Verzeichnis existiert
}
|
|
| Verzeichnis anlegen: |
set folder = oFs.createfolder( itemPath )
|
`mkdir $itemPath`;
|
mkdir %itemPath%
|
| Verzeichnis wechseln: |
set oShell = createobject("wscript.shell")
oShell.CurrentDirectory = itemPath
|
`cd $itemPath`;
|
cd %itemPath%
|
| Verzeichnis löschen: |
oFs.deleteFolder itemPath
|
`rmdir $itemPath`;
|
rmdir %itemPath%
|
| Verzeichnis verschieben: |
src = itemPath & "\" & itemName
dst = "c:\temp\neu.txt"
set oItem = oFs.GetFolder( src )
oItem.Move( dst )
|
$src = "$itemPath$itemName";
$dst = "c:\temp\neu.txt";
rename( $src, $dst );
|
set src = %itemPath%%itemName%
set dst = "c:\temp\neu.txt"
move %src% %dst% |
| Aktion | VBS | PERL |
|---|---|---|
| Befehl ausführen ohne zu warten. |
set oWss = WScript.CreateObject("WScript.Shell")
' notepad starten mit diesem script
oWss.Run ("%windir%\notepad " & WScript.ScriptFullName)
|
|
| Befehl ausführen warten bis beendet. |
set oWss = WScript.CreateObject( "WScript.Shell" )
' notepad starten mit diesem script
oWss.Run( "%windir%\notepad " & WScript.ScriptFullName, 1, true )
|
`copy alt neu`; |
| Befehl ausführen Rückgabe übernehmen. |
Dim oWss, oExec As Object
Set oWss = CreateObject("WScript.Shell")
Set oExec = oWss.Exec("cmd /C dir ")
if 1 then
' read all in one
Select Case oExec.Status
Case WshFinished
getEnvVal = oExec.StdOut.ReadAll
Case WshFailed
getEnvVal = oExec.StdErr.ReadAll
End Select
else
' read line by line
While (oExec.StdOut.AtEndOfStream = False)
getEnvVal = getEnvVal & oExec.StdOut.ReadLine()
Wend
endif
|
$retVal = `copy alt neu`; |
| Befehl ausführen Parameterübergabe aus Script. |
| Aktion | VBS | PERL |
|---|---|---|
| Excel starten |
Set oEx = CreateObject("Excel.Application") |
use Win32::OLE;
# Starte Excel und mache es sichtbar
$xlApp = Win32::OLE->new('Excel.Application');
$xlApp->{Visible} = 1;
|
| Excel Makro test ausführen |
itemPath = "c:\Temp\test\"
itemName = "ExcelFileName.xlsm"
Set oExWB = oEx.Workbooks.Open( itemPath & itemName )
oExWB.Run oEx itemName & ".xlsm!test"
|
TBD |
| Word starten | TBD |
use Win32::OLE;
# Starte Word
$xlApp = Win32::OLE->new('Word.Application');
|
| Ordner zippen |
' call example: zipFiles "zipFile.zip", "c:\Temp\zipFolder"
sub zipFiles (ZipFile, InputFolder)
' create ZipFile and copy source from InputFolder to ZipFile
' Create empty ZIP file.
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set oShell = CreateObject("Shell.Application")
Set source = oShell.NameSpace(InputFolder).Items
oShell.NameSpace(ZipFile).CopyHere(source)
' notwendig!
wScript.Sleep 2000
end sub
|