Wednesday 11 March 2015

CREATE/COPY/DELETE the FILE/FOLDER through VBScript - Advance Application Packaging

Hi all,

Here is small piecs of code which will be very usefule in doing file & folder operations during writting any VBScript.


Option Explicit
on error resume next
Dim objShell,objFSO,strPath
Dim CreateFolder1,Copyfile1,DelFolder1
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strPath = objShell.ExpandEnvironmentStrings("%ProgramData%")

*************** CREATE FOLDER ***************

CreateFolder1=strPath & "\Microsoft\Windows\Start Menu\Programs\MyApps"
CreateFolder2=strPath & "\Microsoft\Windows\Start Menu\Programs\MyApps\Vinayak"

If NOT objFSO.FolderExists(CreateFolder1) Then

objFSO.CreateFolder(CreateFolder1)
objFSO.CreateFolder(CreateFolder2)
End If

OR 

objShell.Run "CMD.exe /C md "& chr(34) & strPath & "\MyApps\Vinayak"& chr(34),0,True

objShell.Run "cmd.exe /c " & chr(34) & "md " & chr(34) & CreateFolder2 & chr(34),0,True



*************** COPY FILE ***************

Copyfile1=strPath & "\Microsoft\Windows\Start Menu\Programs\MyApps\Vinayak\MyShortcut.lnk"

If objFSO.FileExists(Copyfile1) Then

objFSO.copyFile Copyfile1,CreateFolder2
End If

objShell.Run "cmd.exe /c " & chr(34) & "copy /Y " & chr(34) & Copyfile1 & chr(34) & " " & chr(34) & CreateFolder2 & chr(34) & chr(34),0,True


*************** COPY FOLDER ***************


If objFSO.FolderExists(CreateFolder1) Then
objFSO.CopyFolder CreateFolder2,CreateFolder1
objFSO.CopyFolder "C:\MyApps\Vinayak", "C:\Vinayak\" 
objFSO.CopyFolder strPath & "\MyApps\Vinayak",strPath & "\Vinayak\",True
End If

*************** DELETE FILE ***************

Delfile1=strPath & "\Microsoft\Windows\Start Menu\Programs\MyApps\Vinayak\MyShortcut.lnk"

If objFSO.FileExists(Delfile1) Then

objFSO.DeleteFile Delfile1,TRUE
End If

*************** DELETE FOLDER ***************


DelFolder1=strPath & "\Microsoft\Windows\Start Menu\Programs\MyApps\Vinayak\"

If objFSO.FolderExists(DelFolder1) Then

objFSO.DeleteFolder DelFolder1,TRUE
objFSO.DeleteFolder StrPath & "\MyApps\Vinayak",True
End If

objShell.Run "cmd.exe /c " & chr(34) & "RD /S /Q " & chr(34) & DelFolder1 & chr(34) & chr(34),0,True

  
Set objFSO = Nothing
Set objShell = Nothing


Thank you for reading.

No comments:

Post a Comment