Tips: FreeBasic: CopyFolder


#Include once "dir.bi"
#Include once "file.bi"
#Include Once "string.bi"
#include once "ext/strings/strsplit.bi"
#Include Once "win/shlobj.bi"

sub copyfolder(dirName as string, dstdir as string)
    const attrib_mask = fbDirectory  or fbNormal Or fbHidden Or fbSystem or fbReadOnly or fbArchive
    dim as uinteger out_attr
    dim fileName as string
    dim absname as String
    
    fileName = Dir(dirName + "\*", attrib_mask, out_attr)
    print "fileName >>>> " + fileName
    Do Until Len(fileName) = 0 '' loop until Dir returns empty string
        If (fileName <> ".") And (fileName <> "..") Then '' ignore current and parent directory entries
     
            absName = dirName & "\" & fileName
            '
            If out_attr And fbDirectory Then
                dim d as string
                d = right(absName,Len(absName) - InStrRev(absName, "\"))
                Print "d = " + d        
                Print "absName =" + absName                
                dstDir = dstDir + "\" + d 
                Print "DST = " + dstdir
                
                SHCreateDirectoryEx(0,StrPtr(dstDir),0)
                copyfolder(absName, dstDir )
            Else
                dim d as String
                d = right(absName,Len(absName) - InStrRev(absName, "\"))
                SHCreateDirectoryEx(0,StrPtr(dstDir),0)
                FileCopy(absName, dstdir + "\" + d)
                Print "file: " + absName + " DST: " + dstDir
            End If
        End If
      fileName = Dir(out_attr)
    Loop
end sub

Code: Freebasic: runas


#Include "windows.bi"
#inclib "advapi32"

Declare FUNCTION CreateProcessWithLogonW LIB "ADVAPI32" ALIAS "CreateProcessWithLogonW" ( _
        byval lpUsername As LPCWSTR  , _
        BYVAL lpDomain AS LPCWSTR , _
        BYVAL lpPassword AS LPCWSTR, _
        BYVAL dwLogonFlags AS DWORD, _
        BYVAL lpApplicationName AS LPCWSTR , _
        BYVAL lpCommandLine AS LPCWSTR , _
        BYVAL dwCreationFlags AS DWORD, _
        BYVAL lpEnvironment AS DWORD, _
        BYVAL lpCurrentDirectory AS LPCWSTR , _
        lpStartupInfo AS STARTUPINFO, _
        lpProcessInfo AS PROCESS_INFORMATION) AS LONG

Const X_CJ_LOGON_WITH_PROFILE=1
Const X_CJ_CREATE_DEFAULT_ERROR_MODE = &H04000000

FUNCTION RunAsUser(BYVAL UserName AS STRING, _                   
        BYVAL Password AS STRING, _
        BYVAL DomainName AS STRING, _
        BYVAL CommandLine AS STRING, _
        BYVAL CurrentDirectory AS STRING) AS boolean

        Dim si AS STARTUPINFO
        Dim pi AS PROCESS_INFORMATION
        Dim AS String wCurrentDir,wCommandLine,wPassword,wUser,wDomain
        Dim R01 AS LONG

        si.cb = LEN(si)
        wUser = UserName
        wDomain = DomainName
        wPassword = Password
        wCommandLine = CommandLine
        wCurrentDir = CurrentDirectory
        Print wCommandLine & "TEST"
        R01 = CreateProcessWithLogonW(wUser,wDomain,wPassword, _
                        X_CJ_LOGON_WITH_PROFILE, 0&, wCommandLine, _
                        X_CJ_CREATE_DEFAULT_ERROR_MODE, 0&, wCurrentDir, si, pi)
        IF R01  0 THEN
                CloseHandle pi.hThread
                CloseHandle pi.hProcess
                Return TRUE
        ELSE
                Return FALSE
        END If
End FUNCTION

Dim As Long R01
Dim As String  user_name,password,domain,program,workingDir

user_name="username"
password="pa$$word"
domain="domain"
program="C:\\windows\\notepad.exe"
workingDir="C:\\windows"

R01=RunAsUser(user_name,password,domain,program,workingDir)