How to make VBScript copy permissions along with files - windows

This is all in Windows XP utilizing VBScript.
I have a directory with several files inside. The files have varying permissions set. I need to be able to copy the files to a new directory while retaining the permissions. Using the script below the copy works fine but the permissions are overwritten by the new parent folder.
I am aware of xcopy but I am unsure how to make it work within the script. Using robocopy is a slight possibility but should be avoided if at all possible. Other utilities are out of the question due to network contraints.
Any help is greatly appreciated.
Dim CopyFromPath, CopyToPath
Const WINDOW_HANDLE = 0
Const NO_OPTIONS = 0
Const OverwriteExisting = TRUE
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(WINDOW_HANDLE,"Select folder to copy:",NO_OPTIONS,ssfDRIVES)
if (not objFolder is nothing) then
Set objFolderItem = objFolder.Self
CopyFromPath = objFolderItem.Path
else
Set objShell = nothing
WScript.Quit(0)
end if
Set objFolder = objShell.BrowseForFolder(WINDOW_HANDLE, "Where should the folder be copied to?:", NO_OPTIONS, ssfDRIVES)
if (not objFolder is nothing) then
Set objFolderItem = objFolder.Self
CopyToPath = objFolderItem.Path
else
Set objShell = nothing
WScript.Quit(0)
end if
Set objFolder = nothing
Set objFolderItem = nothing
Set objShell = nothing
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile CopyFromPath & "\*.*", CopyToPath & "\", OverwriteExisting
msgbox "The folder has now been copied to " & CopyToPath

xcopy is a good idea for it.
An example to how make it work within vbscript.
Set oWSHShell = CreateObject("WScript.Shell")
oWSHShell.Exec "xcopy C:\source C:\destination /O /X /H /K /Y"
Set oWSHShell = Nothing

Related

VBScript command to wait for files to be extracted before launching EXE to install program

I'm looking at having a script that decompresses a file (PDMsetup.zip) and then launch the executable that it extracts.
ZipFile="PDMsetup.zip"
ExtractTo=".\"
Set fso = CreateObject("Scripting.FileSystemObject")
sourceFile = fso.GetAbsolutePathName(ZipFile)
destFolder = fso.GetAbsolutePathName(ExtractTo)
Set objShell = CreateObject("Shell.Application")
Set FilesInZip=objShell.NameSpace(sourceFile).Items()
objShell.NameSpace(destFolder).copyHere FilesInZip, 16
Set fso = Nothing
Set objShell = Nothing
Set FilesInZip = Nothing
wscript.sleep 480000
Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
strPath = strFolder & "\Startwinstall.exe"
objShell.Run strPath
I want to get rid of;
wscript.sleep 480000
and replace it with a command that tells the script wait until the extraction is done before launching startwinstall.exe
I've kept adjusting the wait time to make up for differences in PC performance with the extraction, but a command to just 'wait' until it's done would be preferential.
Delete any previous copy of the installer exe in the target folder and then wait for that file to be created. Create your objects once at the top of the script. And there's no need to set the objects to Nothing. That will happen automatically when the script ends. The edited script is below:
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWSH = CreateObject("Wscript.Shell")
Set oApp = CreateObject("Shell.Application")
MyFolder = oFSO.GetParentFolderName(WScript.ScriptFullName)
ExtractTo = ".\"
ZipFile = "PDMsetup.zip"
StartApp = ExtractTo & "Startwinstall.exe"
On Error Resume Next
oFSO.DeleteFile StartApp
On Error Goto 0
sourceFile = oFSO.GetAbsolutePathName(ZipFile)
destFolder = oFSO.GetAbsolutePathName(ExtractTo)
Set FilesInZip = oApp.NameSpace(sourceFile).Items()
oApp.NameSpace(destFolder).copyHere FilesInZip, 16
Do Until oFSO.FileExists(StartApp)
WScript.Sleep 1000
Loop
oWSH.Run StartApp
Note: I assigned a MyFolder variable, but it's not currently being used. ExtractTo = ".\" could be changed to ExtractTo = MyFolder. You could also eliminate the GetAbsolutePathName lines if you are using MyFolder with the ZipFile name. There are always many ways to do the same thing.
Note: I think the above can be done with a much briefer (probably two line) PowerShell script. Let me know if you're interested in that solution.

Trying to code for the first time, but failed with file location

The problem is that I tried to make a file on somebody else's desktop but I don't know what to write in the users.
I tried "C:\Users\" & strUser & "\desktop\Test"
Set fso = CreateObject("Scripting.filesystemobject")
fso.Createfolder "C:\Users\" & strUser & "\desktop\Test"
error: Can't find the file location.
Use the special folder Shell function to return the path to the desktop.
set objShell = Wscript.CreateObject("Wscript.Shell")
strDesktopPath = objShell.SpecialFolders("Desktop")
Set fso = CreateObject("Scripting.filesystemobject")
fso.Createfolder strDesktopPath & "\Test"
I think your code will work if you simply add a \ at the end of your path:
Set fso = CreateObject("Scripting.filesystemobject")
fso.Createfolder "C:\Users\" & strUser & "\desktop\Test\"

Create backup folder with several directories and files

I am new at this and I am trying to get a VBScript (Because the aplicattion only works with this) tha makes a backup of several directories and files...
EXample:
Check if Folder1 exist... (It may be on c:\ or another drive)
If donĀ“t exist the finish
If exist than should create a Bck Folder
THan Backup the folders that are inside folder1 (Folder2, Folder3)
Also backup all the files that are *.mds, *.vbs inside Folder4
The script have to maintain the struture...
And after that delete all folder1
This is what I have so far:
IF NOT EXIST "%INSTALLDIR%\Folder1\" GOTO ENDPROG
mkdir "%INSTALLDIR%\BCK\"
mkdir "%INSTALLDIR%\BCK\DADOS\"
mkdir "%INSTALLDIR%\BCK\IMAGEM\"
mkdir "%INSTALLDIR%\BCK\CONFIG\"
mkdir "%INSTALLDIR%\BCK\OFFBck"
copy "%INSTALLDIR%\Folder1\Dados*.MDB" "%INSTALLDIR%\BCK\dados\"
copy "%INSTALLDIR%\Folder1\Dados*.MDD" "%INSTALLDIR%\BCK\dados\"
copy "%INSTALLDIR%\Folder1\Dados*.VEI" "%INSTALLDIR%\BCK\dados\"
copy "%INSTALLDIR%\Folder1\Imagem*.*" "%INSTALLDIR%\BCK\Imagem\"
copy "%INSTALLDIR%\Folder1*.cfg" "%INSTALLDIR%\BCK\Config\"
copy "%INSTALLDIR%\Folder1\OFFbck*.ZIP" "%INSTALLDIR%\BCK\OFFbck\"
copy "%INSTALLDIR%\Folder1\Folder1\OFFbck*.ZIP" "%INSTALLDIR%\BCK\OFFbck\"
rmdir "%INSTALLDIR%\Folder1" /s /q
:ENDPROG
Can anyone give me a hand on this?
I already did this.....But i give an error if the folder already exist for the backup....but I cannot start to copy....
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists("C:\Folder1") Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const OverwriteExisting = TRUE
Set objFolder = objFSO.CreateFolder("C:\BCK")
Set objFolder = objFSO.CreateFolder("C:\BCK\Imagem")
Set objFolder = objFSO.CreateFolder("C:\BCK\dados")
Set objFolder = objFSO.CreateFolder("C:\BCK\config")
Set objFolder = objFSO.CreateFolder("C:\BCK\off")
Else
End If
Thanks in advance
I just created this....
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists("C:\audatex") Then
Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder("C:\BCK")
Set objFolder = objFSO.CreateFolder("C:\BCK\Imagem")
Set objFolder = objFSO.CreateFolder("C:\BCK\dados")
Set objFolder = objFSO.CreateFolder("C:\BCK\WTB")
Set objFolder = objFSO.CreateFolder("C:\BCK\CFG")
Set objFolder = objFSO.CreateFolder("C:\BCK\config")
Set objFolder = objFSO.CreateFolder("C:\BCK\offdaten")
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Audatex\offdaten\*.zip" , "c:\BCK\Offdaten\" , OverwriteExisting
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Audatex\Dados\*.vei" , "c:\BCK\dados\" , OverwriteExisting
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Audatex\Dados\*.mdd" , "c:\BCK\dados\" , OverwriteExisting
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Audatex\*.cfg" , "c:\BCK\CFG\" , OverwriteExisting
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Audatex\Dados\*.mdb" , "c:\BCK\dados\" , OverwriteExisting
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFolder "C:\Audatex\Imagem" , "c:\BCK\Imagem" , OverwriteExisting
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Audatex\WTB\*.wtb" , "c:\BCK\WTB\" , OverwriteExisting
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Audatex\WTB\*.dtb" , "c:\BCK\WTB\" , OverwriteExisting
End IF
But know I have the following problems: If there is nothing on the folders i got an error and i cannot overwrite the folders
Thanks
You can copy files with particular extensions that start with a specific string like this:
For Each f In objFSO.GetFolder("C:\Folder1").Files
ext = LCase(objFSO.GetExtensionName(f))
If (ext = "mdb" Or ext = "mdd" Or ext = "vei") And LCase(Left(f.Name, 5)) = "dados" Then
f.Copy "C:\BCK\dados\"
End If
Next
Another (perhaps simpler) option would be to check the file name with a regular expression:
Set re = New RegExp
re.Pattern = "^dados.*\.(mdb|mdd|vei)$"
re.IgnoreCase = True
For Each f In objFSO.GetFolder("C:\Folder1").Files
If re.Test(f.Name) Then f.Copy "C:\BCK\dados\"
Next

how to delete multiple folders,desktop and start menu shortcut using vbscript

I never did any vbscript before, so i don't know if my question is very easy one. Following is the flow of steps that has to be done :
Check if exist and delete a folder at c:\test1 if found and continue. If not found continue.
Check if exist and delete a folder at c:\programfiles\test2 if found and continue. If not found continue.
Check if a desktop shortcut and start menu shortcut exist and delete if found. If not exit.
I could delete 2 folders with the following code:
strPath1 = "C:\test1"
strPath1 = "C:\test1"
DeleteFolder strPath1
DeleteFolder strPath1
Function DeleteFolder(strFolderPath1)
Dim objFSO, objFolder
Set objFSO = CreateObject ("Scripting.FileSystemObject")
If objFSO.FolderExists(strFolderPath) Then
objFSO.DeleteFolder strFolderPath, True
End If
Set objFSO = Nothing
But i need to run one script to delete 2 folders in different paths, 2 shortcuts one in start menu and one on desktop.
I was experimenting with this code to delete the shortcut on my desktop:
Dim WSHShell, DesktopPath
Set WSHShell = WScript.CreateObject("WScript.Shell")
DesktopPath = WSHShell.SpecialFolders("Desktop")
on error resume next
Icon = DesktopPath & "\sample.txt"
Set fs = CreateObject("Scripting.FileSystemObject")
Set A = fs.GetFile(Icon)
A.Delete
WScript.Quit
It works fine for txt file on desktop, but how do i delete a shortcut for an application from desktop as well as start menu.
strPath1 = "C:\test1"
strPath2 = "C:\test2"
DeleteFolder strPath1
DeleteFolder strPath2
DeleteShortcut
'-------------------------------------------------------
Sub DeleteFolder(strFolderPath)
Set fso = CreateObject ("Scripting.FileSystemObject")
If fso.FolderExists(strFolderPath) Then
fso.DeleteFolder strFolderPath, True
End If
End Sub
'-------------------------------------------------------
Sub DeleteShortcut()
Set WSHShell = WScript.CreateObject("WScript.Shell")
DesktopPath = WSHShell.SpecialFolders("Desktop")
shortcutPath = DesktopPath & "\MyShortcut.lnk"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(shortcutPath) Then
Set myFile = fso.GetFile(shortcutPath)
myFile.Delete
End If
End Sub

geting special folders with shell.application

I need in a script to return the path to the current user desktop. Now I know you can do it with WScript.
var WshShell = WScript.CreateObject("WScript.Shell");
strDesktop = WshShell.SpecialFolders("Desktop");
But for my script this will not work as I cant use WScript. but I can use the shell.application object as below.
dim objShell
dim ssfWINDOWS
dim objFolder
ssfWINDOWS = 0
set objShell = CreateObject("shell.application")
set objFolder = objshell.BrowseForFolder(0, "Example", 0, ssfWINDOWS)
if (not objFolder is nothing) then
Set objFolderItem = objFolder.Self
g_objIE.Document.All("logdir").Value = objFolderItem.path
end if
set objFolder = nothing
set objShell = nothing
what is the syntax so the rather than "BrowseForFolder" i can simple return the path of the current users desktop?
IE replace the line
set objFolder = objshell.BrowseForFolder(0, "Example", 0, ssfWINDOWS)
with the equilivent of.
strDesktop = WshShell.SpecialFolders("Desktop");
Cheers
Aaron
You need to use Shell.Namespace(...).Self.Path:
Const ssfDESKTOPDIRECTORY = &h10
Set oShell = CreateObject("Shell.Application")
strDesktop = oShell.NameSpace(ssfDESKTOPDIRECTORY).Self.Path
WScript.Echo strDesktop
But for my script this will not work as I cant use WScript.
Do you mean you can't use WScript.CreateObject(...) because WScript is undefined? If so, you can simply use CreateObject("WScript.Shell").SpecialFolders("Desktop") instead. See What is the difference between CreateObject and Wscript.CreateObject?.
Try the namespace method:
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H10&)
Where &H10& is a special folder constant for the desktop. See technet for a list of all special folder constants.

Resources