Jump to content

Convert VBScript to autoit


Recommended Posts

Hi guys!

I'm totaly new in autoit scripting, and need help with transformation of vbs script to autoit script

Here is my script:

Option Explicit 
Dim program

'****************************************
'Name of main folder to copy
'****************************************
    program = "era"
'****************************************


Dim objFSO, strSourceFolder, strDestFolder, strExclusion, strPrompt 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
'Copy from?
strSourceFolder = "c:\a\" & program
'Copy to...
strDestFolder = "D:\bak\"        
strExclusion = "zip,tmp,lst,bak,arh,rar,old,doc,docx,xls,xlsx,pdf,db,cdx" ' Example:    zip,vbs,exe,js,rar 
If IsNoData(strExclusion) Then strExclusion = "" 'mora biti prazno. 
'Call function for copying folders and all data in them. 
CopyFolderStructure strSourceFolder, strDestFolder, strExclusion 
Function CopyFolderStructure(strSource, strDestination, strExcludedExt) 
Const OVER_WRITE_FILES = True 
Dim objDir, objFolder, objFiles, strCurExt, intX, arrExt, blnExclude 

'Connect to current folder in strSource. 
Set objDir = objFSO.GetFolder(strSource) 

'If destination folder doesn't exist, create it. 
If Not objFSO.FolderExists(strDestination) Then 
 objFSO.CreateFolder(strDestination) 
End If 

'If current folder doesn't exist under destination folder, create it. 
If Not objFSO.FolderExists(strDestination & "\" & objDir.Name) Then 
 objFSO.CreateFolder(strDestination & "\" & objDir.Name) 
End If 
'Validate if there is something in strExcludedExt. 
If Not IsNoData(strExcludedExt) Then 
 'If yes, transfer content from strExcludedExt in an array. 
 arrExt = Split(strExcludedExt, ",") 
 blnExclude = False  'Intialize to False blnExclude 
End If 
'Loop through all files in current folder if any and copy all files in destination folder 
'except for excluded extension if any in strExcludedExt. 
For Each objFiles In objFSO.GetFolder(strSource).Files 
 If objFiles.attributes and 32 Then ' Check if the Archive Atrtibut Is set 
  'If there is exclusion, will compare if current extension file is excluded or not. 
  If Not IsNoData(strExcludedExt) Then 
   strCurExt = objFSO.GetExtensionName(objFiles.Name) 'Take current extension. 
   'Loop through the array to find if current extension has to be copied or not. 
   For intX = 0 To UBound(arrExt) 
    If LCase(strCurExt) = arrExt(intX) Then 
     blnExclude = True 'If found, set to True blnExclude and exit for. 
     Exit For 
    Else 
     blnExclude = False 
    End If 
   Next 
   If Not blnExclude Then 'If blnExclude is True, current file will not be copied. 
    objFSO.CopyFile strSource & "\" & objFiles.Name, strDestination & "\" & objDir.Name & "\" & objFiles.Name, OVER_WRITE_FILES 
    objFiles.attributes = objFiles.attributes - 32 
   End If 
  Else 'If no exclusion, copy everything in all folders/subfolders 
   objFSO.CopyFile strSource & "\" & objFiles.Name, strDestination & "\" & objDir.Name & "\" & objFiles.Name, OVER_WRITE_FILES 
   objFiles.attributes = objFiles.attributes - 32 
  End If 
 End If 
Next 
' If there is subfolder(s) under current folder in strPath, Call 
' recursively this sub until there is no other subfolder(s) 
For Each objFolder In objFSO.GetFolder(strSource).SubFolders 
 CopyFolderStructure objFolder.Path, strDestination & "\" & objDir.Name, strExcludedExt 
Next 
End Function 
Function IsNoData(varVal2Check) 
'Verify if varVal2Check contain something. 
On Error Resume Next 
   If IsNull(varVal2Check) Or IsEmpty(varVal2Check) Then 
 IsNoData = True 
   Else 
       If IsDate(varVal2Check) Then 
  IsNoData = False 
       Elseif varVal2Check = "" Then 
  IsNoData = True 
 ElseIf Not IsObject(varVal2Check) Then 
  IsNoData = False 
 Else 
           IsNoData = False 
       End If 
   End If 
End Function
wscript.sleep 100

This script copy's all data from given folder with atrib set to "archive" to "bak" folder.

I tried to convert it automaticaly but it shows error on line 87. This is converted code:

; ----------------------------------------------------------------------------
;
; VBScript to AutoIt Converter v0.4
;
; ----------------------------------------------------------------------------

#include <date.au3>

AutoItSetOption("MustDeclareVars", 1) 
Dim $program

;****************************************
;
;****************************************
    $program = "era"
;****************************************


Dim $objFSO, $strSourceFolder, $strDestFolder, $strExclusion, $strPrompt 
 $objFSO = ObjCreate("Scripting.FileSystemObject") 
;
$strSourceFolder = "c:\a\" & $program
;...
$strDestFolder = "D:\bak\"        
$strExclusion = "zip,tmp,lst,bak,arh,rar,old,doc,docx,xls,xlsx,pdf,db,cdx" ; Example:    zip,vbs,exe,js,rar 
If IsNoData($strExclusion) Then $strExclusion = "" ;mora biti prazno. 
; 
CopyFolderStructure ($strSourceFolder, $strDestFolder, $strExclusion )
Func CopyFolderStructure($strSource, $strDestination, $strExcludedExt) 
Const $OVER_WRITE_FILES = 1 
Dim $objDir, $objFolder, $objFiles, $strCurExt, $intX, $arrExt, $blnExclude 

; 
 $objDir = $objFSO.GetFolder($strSource) 

;If destination folder doesn;t exist, create it. 
If Not $objFSO.FolderExists($strDestination) Then 
 $objFSO.CreateFolder($strDestination) ()
EndIf 

;If current folder doesn;t exist under destination folder, create it. 
If Not $objFSO.FolderExists($strDestination & "\" & $objDir.Name) Then 
 $objFSO.CreateFolder($strDestination (& "\" & $objDir.Name) )
EndIf 
;Validate if there is something in $strExcludedExt. 
If Not IsNoData($strExcludedExt) Then 
 ;If yes, transfer content from $strExcludedExt in an array. 
 $arrExt = StringSplit($strExcludedExt, ",") 
 $blnExclude = 0  ;Intialize to 0 $blnExclude 
EndIf 
;Loop through all $FILES in current folder if any and copy all $FILES in destination folder 
;except for excluded extension if any in $strExcludedExt. 
For $objFiles In $objFSO.GetFolder($strSource).$FILES ()
 If $objFiles.attributes and 32 Then ; Check if the Archive Atrtibut Is set 
  ;If there is exclusion, will compare if current extension file is excluded or not. 
  If Not IsNoData($strExcludedExt) Then 
   $strCurExt = $objFSO.GetExtensionName($objFiles.Name) ;Take current extension. 
   ;Loop through the array to find if current extension has to be copied or not. 
   For $intX = 0 To UBound($arrExt) 
    If StringLower($strCurExt) = $arrExt($intX) Then 
     $blnExclude = 1 ;If found, set to 1 $blnExclude and exit for. 
     ExitLoop 
    Else 
     $blnExclude = 0 
    EndIf 
   Next 
   If Not $blnExclude Then ;If $blnExclude is True, current file will not be copied. 
    $objFSO.CopyFile ($strSource & "\" & $objFiles.Name, $strDestination & "\" & $objDir.Name & "\" & $objFiles.Name, $OVER_WRITE_FILES )
    $objFiles.attributes = $objFiles.attributes - 32 
   EndIf 
  Else ;If no exclusion, copy everything in all folders/subfolders 
   $objFSO.CopyFile ($strSource & "\" & $objFiles.Name, $strDestination & "\" & $objDir.Name & "\" & $objFiles.Name, $OVER_WRITE_FILES )
   $objFiles.attributes = $objFiles.attributes - 32 
  EndIf 
 EndIf 
Next 
; If there is subfolder(s) under current folder in strPath, Call 
; recursively this sub until there is no other subfolder(s) 
For $objFolder In $objFSO.GetFolder($strSource).SubFolders ()
 CopyFolderStructure ($objFolder.Path, $strDestination & "\" & $objDir.Name, $strExcludedExt )
Next 
EndFunc 
Func IsNoData($varVal2Check) 
    Local $Return
;Verify if $varVal2Check contain something. 
; On Error Resume Next 
   If IsNull($varVal2Check) Or IsEmpty($varVal2Check) Then 
 $Return = 1 
   Else 
       If _DateIsValid($varVal2Check) Then 
  $Return = 0 
       $Elseif $varVal2Check = "" Then 
  $Return = 1 
 $Elseif Not IsObj($varVal2Check) Then 
  $Return = 0 
 Else 
           $Return = 0 
       EndIf 
   EndIf 
    Return $Return
EndFunc

Please help

Link to comment
Share on other sites

I think autoit is lacking a filecopy() function, so you'll have to make one.

heres some code to get you started.

will write to console of scite (if ran from scite) files with archive attribute, just replace your function with a copy one.

#include <File.au3>

$s_Dir = @HomeDrive & "\"
$a_Files = _FileListToArray($s_Dir, "*.*", 1)

For $i = 1 To $a_Files[0]
    $s_Attribs = FileGetAttrib($s_Dir & $a_Files[$i])
    If @error Then
        MsgBox(0, "Error", "Cannot Get Attribs for " & $s_Dir & $a_Files[$i])
    Else
        If StringInStr($s_Attribs, "A") Then
            _SomeCopyFunc($a_Files[$i])
        EndIf
    EndIf
Next


Func _SomeCopyFunc($sfile)
    ConsoleWrite("Archive = " & $sfile & @CRLF)
EndFunc   ;==>_SomeCopyFunc
Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • 5 months later...

Guys I have this VBS and I can't convert to AutoIT. Can you guys help me on this?

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.OpenTextFile(".\Check_EMS_AD.txt", 2, TRUE)
Set objSystemInfo = CreateObject("ADSystemInfo")
strdname = objSystemInfo.DomainShortName
set conn1 = createobject("ADODB.Connection")
strConnString = "Data Provider=NONE; Provider=MSDataShape"
conn1.Open strConnString
set conn = createobject("ADODB.Connection")
set com = createobject("ADODB.Command")
Set iAdRootDSE = GetObject("LDAP://RootDSE")
strNameingContext = iAdRootDSE.Get("defaultNamingContext")
Conn.Provider = "ADsDSOObject"
objLog.WriteLine "EMS Check - Checking Users with Send AS and Receive AS Permissions."
objLog.WriteLine ""
Conn.Open "ADs Provider"
Query = "<LDAP://" & strNameingContext & ">;(&(&(& (mailnickname=*) (| (&(objectCategory=person)(objectClass=user)(|(homeMDB=*)(msExchHomeServerName=*))) ))));samaccountname,displayname,distinguishedName;subtree"
Com.ActiveConnection = Conn
Com.CommandText = Query
Com.Properties("Page Size") = 1000
set objParentRS = createobject("adodb.recordset")
set objChildRS = createobject("adodb.recordset")
strSQL = "SHAPE APPEND" & _
           "  NEW adVarChar(255) AS UOADDisplayName, " & _
           "  NEW adVarChar(255) AS UOADTrusteeName, " & _
           " ((SHAPE APPEND  " & _
           "      NEW adVarChar(255) AS MRmbox, " & _
           "      NEW adVarChar(255) AS MRTrusteeName, " & _
           "      NEW adVarChar(255) AS MRRights, " & _
           "      NEW adVarChar(255) AS MRAceflags) " & _
           "      RELATE UOADTrusteeName TO MRTrusteeName) AS rsUOMR"
objParentRS.LockType = 3
objParentRS.Open strSQL, conn1
Set Rs = Com.Execute
While Not Rs.EOF
dn = "LDAP://" & replace(rs.Fields("distinguishedName").Value,"/","\/")
set objuser = getobject(dn)
Set oSecurityDescriptor = objuser.Get("ntSecurityDescriptor")
Set dacl = oSecurityDescriptor.DiscretionaryAcl
Set ace = CreateObject("AccessControlEntry")
objParentRS.addnew
objParentRS("UOADDisplayName") = rs.fields("displayname")
objParentRS("UOADTrusteeName") = strdname & "\" & rs.fields("samaccountname")
objParentRS.update
Set objChildRS = objParentRS("rsUOMR").Value
For Each ace In dacl
     if lcase(ace.ObjectType) = "{ab721a54-1e2f-11d0-9819-00aa0040529b}" and ace.AceType = 5 then
   if ace.Trustee <> "NT AUTHORITY\SELF" and ace.AceFlags <> 6 then
    objChildRS.addnew
    objChildRS("MRmbox") = rs.fields("displayname")
    objChildRS("MRTrusteeName") = ace.Trustee
    objChildRS("MRRights") = "Send As"
    objChildRS("MRAceflags") = ace.AceFlags
    objChildRS.update
   end if
     end if
     if lcase(ace.ObjectType) = "{ab721a56-1e2f-11d0-9819-00aa0040529b}" and ace.AceType = 5 then
   if ace.Trustee <> "NT AUTHORITY\SELF" and ace.AceFlags <> 6 then
    objChildRS.addnew
    objChildRS("MRmbox") = rs.fields("displayname")
    objChildRS("MRTrusteeName") = ace.Trustee
    objChildRS("MRRights") = "Receive As"
    objChildRS("MRAceflags") = ace.AceFlags
    objChildRS.update
   end if
     end if
Next
rs.movenext
Wend
objLog.WriteLine ("Number of Mailboxes Checked " & objParentRS.recordcount)
objLog.WriteLine ("")
objParentRS.MoveFirst

Do While Not objParentRS.EOF
Set objChildRS = objParentRS("rsUOMR").Value
crec = 0
countRA = 0
countSA = 0
if objChildRS.recordcount <> 0 then
  objLog.WriteLine ("User with Send AS and Receive AS Permissions: " & objParentRS("UOADDisplayName"))
  Do While Not objChildRS.EOF
   If InStr(objChildRS.fields("MRRights"), "Receive") <> 0 Then
           countRA = countRA + 1
                 ElseIf InStr(objChildRS.fields("MRRights"), "Send") <> 0 Then
                  countSA = countSA + 1
          End If
   objChildRS.movenext
  loop
  objLog.WriteLine ("")
  objLog.WriteLine ("Number of Receive As permissions: " & countRA)
  objLog.WriteLine ("Number of Send As permissions:    " & countSA)
  objLog.WriteLine ("")
end if
objParentRS.MoveNext
loop

Tks

Edited by Mvighi
Link to comment
Share on other sites

@Mvighi: I would suggest to open a new topic for your problem! Btw, look here for AD support:

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

@imbrija: try this

#include <Array.au3>
#include <File.au3>
$sSource = "c:\Test"
$sDest = "c:\Bak"
$sExcl = "zip,tmp,lst,bak,arh,rar,old,doc,docx,xls,xlsx,pdf,db,cdx"

$aFiles = GetFiles($sSource, $sExcl)
If @error Then Exit MsgBox(0, "Information", "No files found")

Global $i
If StringRight($sDest, 1) <> "\" Then $sExcl &= "\"
For $files In $aFiles
    $m = FileMove($files, $sDest, 8)
Next

Func GetFiles($sSource, $sExcl = "", $rec = False)
    Local Static $sFiles
    Local $file, $sAtt, $aF
    If StringRight($sExcl, 1) <> "," Then $sExcl &= ","
    If StringLeft($sExcl, 1) <> "," Then $sExcl = "," & $sExcl
    Local $hSearch = FileFindFirstFile($sSource & "\*")
    While 1
        $file = FileFindNextFile($hSearch)
        If @error Then ExitLoop
        $sAtt = FileGetAttrib($sSource & "\" & $file)
        If Not StringInStr($sExcl, "," & StringRight($file, 3) & ",") And $sAtt = "A" Then $sFiles &= $sSource & "\" & $file & "|"
        If $sAtt = "D" And $rec Then GetFiles($sSource & "\" & $file, $sExcl)
    WEnd
    FileClose($hSearch)
    If $sFiles <> "" Then
        $aF = StringSplit(StringLeft($sFiles, StringLen($sFiles) - 1), "|")
        Return $aF
    EndIf
    Return SetError(1, 0 , 0) ;no files found
EndFunc

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...