Reply to topic  [ 7 posts ] 
VB Script Experts 
Author Message
I haven't seen my friends in so long
User avatar

Joined: Fri Apr 24, 2009 7:17 am
Posts: 5550
Location: Nottingham
Reply with quote
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

_________________
Twitter
Blog
flickr


Wed Apr 21, 2010 8:46 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 5288
Location: ln -s /London ~
Reply with quote
Possibly worth having a looksy on Superuser or ServerFault. Sorry I can't be more useful.

_________________
timark_uk wrote:
Gay sex is better than no sex

timark_uk wrote:
Edward Armitage is Awesome. Yes, that's right. Awesome with a A.


Wed Apr 21, 2010 9:08 am
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
Does it have to be a VB script?

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

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Wed Apr 21, 2010 1:04 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Fri Apr 24, 2009 7:17 am
Posts: 5550
Location: Nottingham
Reply with quote
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?

_________________
Twitter
Blog
flickr


Wed Apr 21, 2010 1:41 pm
Profile WWW
Has a life

Joined: Fri Apr 09, 2010 9:18 pm
Posts: 17
Reply with quote
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"


Wed Apr 21, 2010 2:16 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Fri Apr 24, 2009 7:17 am
Posts: 5550
Location: Nottingham
Reply with quote
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:\

_________________
Twitter
Blog
flickr


Last edited by veato on Thu Apr 22, 2010 10:28 am, edited 1 time in total.



Wed Apr 21, 2010 4:45 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Fri Apr 24, 2009 7:17 am
Posts: 5550
Location: Nottingham
Reply with quote
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>

_________________
Twitter
Blog
flickr


Thu Apr 22, 2010 10:26 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.