x404.co.uk
http://www.x404.co.uk/forum/

VB Script Experts
http://www.x404.co.uk/forum/viewtopic.php?f=4&t=7868
Page 1 of 1

Author:  veato [ Wed Apr 21, 2010 8:46 am ]
Post subject:  VB Script Experts

Anyone know a VB script that will create a folder on %homedrive% (this will be the logged on users network share mapped as U:) called 'favourites' and 'desktop shortcuts' (if they dont already exist) and then copy the logged on users favourites and shortcuts to these folders?

I'm not in the least a programmer and to be honest hate it with a passion (yes, even little VB scripts - I'd rather lick a fat mans armpit) but will spend the day trawling Google. Any help appreciated. :D

Author:  EddArmitage [ Wed Apr 21, 2010 9:08 am ]
Post subject:  Re: VB Script Experts

Possibly worth having a looksy on Superuser or ServerFault. Sorry I can't be more useful.

Author:  finlay666 [ Wed Apr 21, 2010 1:04 pm ]
Post subject:  Re: VB Script Experts

Does it have to be a VB script?

May be work looking at running a basic batch file on startup

Author:  veato [ Wed Apr 21, 2010 1:41 pm ]
Post subject:  Re: VB Script Experts

finlay666 wrote:
Does it have to be a VB script?

May be work looking at running a basic batch file on startup


The script needs to look for the two folders on the network share. If they are present it moves onto the next part (copying the data over) or if not it creates the folders before moving on. Can a batch file do this?

At the minute I've got VBSs to check for and/or create the folders which are run by a batch file also handling the copying of data:

batch file running on log off wrote:
start create_fav.vbs
start create_short.vbs

@echo off
:: variables
set backupcmd=xcopy /c /r /y /s
echo off
%backupcmd% "%USERPROFILE%\Favorites\*.*" "%HOMESHARE%\FAVORITES\"
%backupcmd% "%USERPROFILE%\Desktop\*.lnk" "%HOMESHARE%\DESKTOP SHORTCUTS\"
@echo off
cls


VBS to check/create favorites folder on homeshare wrote:
Option Explicit
Dim objFSO, objFolder, objShell, strDirectory
strDirectory = "U:\Favorites"

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists(strDirectory) Then
WScript.Quit

Else
Set objFolder = objFSO.CreateFolder(strDirectory)

End If

WScript.Quit


VBS to check/create desktop shortcuts folder on homeshare wrote:
Option Explicit
Dim objFSO, objFolder, objShell, strDirectory
strDirectory = "U:\Desktop Shortcuts"

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists(strDirectory) Then
WScript.Quit

Else
Set objFolder = objFSO.CreateFolder(strDirectory)

End If

WScript.Quit


Dumb questions but....

1) Can I combine the 2 VBS into 1?
2) In the scripts I've used the drive letter and path but would have prefered to use %homeshare%. I cannot make this work even with the help of Google ;) Does anyone know how?

Author:  opensvr [ Wed Apr 21, 2010 2:16 pm ]
Post subject:  Re: VB Script Experts

Hi, you need to be very careful here - you have said you want to use the environment vairable "homedrive"

This is a system environment variable i.e. if you drop to a command prompt and run set you will see it already exists.
for example on my PC %homedrive% is c: If you want anew env variable let me know.

So you can test this script, I have left a load of msgbox diagnostics in - remove or comment out as needed.
Also uncomment the on "error resume next" lines when you are happy with it.

Good luck.

##########Copy below

'This script create a folder on %homedrive% (this will be the logged on users network share mapped as U:)
'called 'favourites' and 'desktop shortcuts' (if they dont already exist) and then copy the logged on users
'favourites and shortcuts to these folders?


'-------------------------------------------------
Sub CreateDirs()
'on error resume next

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
Set WshSysEnv = objShell.Environment("System")

strhomedrive = WshSysEnv("homedrive")

msgbox strhomedrive

'Make Favorites folder
objFso.CreateFolder strhomedrive & "\favorites"
if err.number = 58 then
msgbox "'%homedrive%\favourites' already exists ..."
end if

'Make desktop shortcuts folder
objFso.CreateFolder strhomedrive & "\desktop shortcuts"
if err.number = 58 then
msgbox "'%homedrive%\desktop shortcuts' already exists ..."
end if

end sub
'----------------------------------------------------------------------------------




'----------------------------------------------------------------------------------
Sub copyfiles()
'on error resume next

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objShell = Wscript.CreateObject("Wscript.Shell")

Set WshSysEnv = objshell.Environment("System")
strhomedrive = WshSysEnv("homedrive")

msgbox strhomedrive

strDesktop = objshell.SpecialFolders("Desktop")
strFavorites = objshell.SpecialFolders("Favorites")

msgbox strdesktop & " " & strfavorites

Const OverwriteExisting = True

objFSO.CopyFile strDesktop & "\*.lnk" , strhomedrive & "\desktop shortcuts" , OverwriteExisting
objFSO.CopyFile strFavorites & "\*.*" , strhomedrive & "\favorites" , OverwriteExisting

end sub
'----------------------------------------------------------------------------------



CreateDirs
msgbox "Finished setting Directory Structures"
copyfiles
msgbox "Finished copying files"

Author:  veato [ Wed Apr 21, 2010 4:45 pm ]
Post subject:  Re: VB Script Experts

opensvr wrote:
Hi, you need to be very careful here - you have said you want to use the environment vairable "homedrive"

This is a system environment variable i.e. if you drop to a command prompt and run set you will see it already exists.
for example on my PC %homedrive% is c: If you want anew env variable let me know.


I'm at home now and cant remember but it may have been homeshare it would be better pointed to. This by default is a users \\server\share

I ran your script to test and everything was placed in C:\

Author:  veato [ Thu Apr 22, 2010 10:26 am ]
Post subject:  Re: VB Script Experts

Thanks for the help. I've been using what I've read here and what I've found on the t'internet with my own splash of blindly trying stuff unitl it works and I have something. In its current state it does 95% of what I need it to - although it might not be pretty ;)

I wondered if anyone can help with the final 5%? Basically where the files from the favorites folder are copied (highlighted in bold text below) only the files are copied and not any (sub)folders in that location :?:

Quote:
' =========================================================
'
' Filename: Copy Shortcuts & Favs.vbs v1
' VBScript for use in Windows Script Host
'
' Date: Apr 2010
'
' Script to create folders on U: and populate with user
' favorites and desktop shortcuts
' =========================================================

Option Explicit
Dim ScriptName
On Error Resume Next

ScriptName = "Copy User Files Script"

' Standard VBScript configuration lines

Dim objFso, objShell, WshSysEnv, WshShell, strHomeshare, strDesktop, strFavorites
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
Set WshSysEnv = objShell.Environment("System")
Set WshShell = WScript.CreateObject("WScript.Shell")

' ---------------------------------------------------------

' Expands environment strings to use %homeshare%

strHomeshare = WshShell.ExpandEnvironmentStrings("%HOMESHARE%")

' ---------------------------------------------------------

' Checks if the folder Favorites exists
' on the users U: drive and if not creates it

Set objFSO = CreateObject("Scripting.FileSystemObject")

If not objFSO.FolderExists(strHomeshare & "\Favorites") Then
objFSO.CreateFolder(strHomeshare & "\Favorites")

End If

' ----------------------------------------------------------

' Will check if the folder Desktop Shortcuts
' exists on the users U: drive and if not creates it

Set objFSO = CreateObject("Scripting.FileSystemObject")

If not objFSO.FolderExists(strHomeshare & "\Desktop Shortcuts") Then
objFSO.CreateFolder(strHomeshare & "\Desktop Shortcuts")

End If

' -----------------------------------------------------------

' Will copy the users Favourites and Desktop
' Shortcuts from the local machine to the newly created
' folders on U: drive

strDesktop = objshell.SpecialFolders("Desktop")
strFavorites = objshell.SpecialFolders("Favorites")

objFSO.CopyFile strDesktop & "\*.lnk" , strHomeshare & "\Desktop Shortcuts"
objFSO.CopyFile strFavorites & "\*.*" , strHomeshare & "\Favorites"

' ------------------------------------------------------------

' Closes object variables

Set objFso = Nothing
Set objShell = Nothing

' ------------------------------------------------------------

' Terminates script

WScript.Quit

' <end>

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/