Ask a questionAsk a question
 

Answervbs to copy nk2

  • Saturday, July 04, 2009 11:43 AMtnetplus Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    this is driving me crazy. 

    Fist a batch file would do this so simple in 4 lines but my company wants only vbs. 

    Here is what I need. 

    When the user logs on a vbs will run that copies there nk2 file to s sever share.

    If they don't have one it skips it.

    We use Vista and XP so %appdata%\Microsoft\outlook\*.nk2. 

    VBS does not understand %appdata% or at least I don't know how to make it understand. 

    does anyone have this or something like it? 

    Thanks

Answers

  • Sunday, July 05, 2009 7:24 AMperhof Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Yes, I'm sorry about that Typo. Even did it twice... sigh. You only need one shell object.
    You can skip the strUsername part completely if you want to since we're already expanding environment strings.
    Just use \\SUN-RON-XP\nk2\%USERNAME%\ for strTarget (why did you remove %username% from my example?) and then do this to create the folder when necessary:

    If not objFSO.FolderExists(strTarget) Then objFSO.CreateFolder(strTarget)

    You said that it doesn't work on both Vista and XP. Is that still so? Where does it NOT work?
    You should be able to put %APPDATA%\Microsoft\outlook in the address bar in both operating systems and end up in the right folders.
    %APPDATA%\Microsoft\outlook\*.nk2 shouldn't work though. That's not a valid path in Explorer.
    • Marked As Answer bytnetplus Sunday, July 05, 2009 1:19 PM
    •  

All Replies

  • Saturday, July 04, 2009 12:22 PMperhof Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    To use an environment variable in VBScript you will need to expand it first.
    This is done using the ExpandEnvirnomentStrings method of the Wscript.Shell object.
    Like this:

    Set objWSHShell = CreateObject("Wscript.Shell")
    strPath =objWSHShell.ExpandEnvironmentStrings("%appdata%\Microsoft\outlook\*.nk2")
  • Saturday, July 04, 2009 3:34 PMtnetplus Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks so much.  I have made some progress.

    As a update I found this script and I have altered it slightly for the vista variables as well as xp.  I have also added the echo commands as a temp thing so I can see if it is finding the file.  It is not finding it on either test pc (vista or xp).

    The issue i think it is looking for a nk2 file names username.nk2 and ours most likely will not be named that.  They may be outlook.nk2 or default user.nk2 or anything.  a user may have a old nk2 or nk2 from another account.  So can this be modified to look for any nk2 in the paths stated  and copy it to the share.
    I know nothing of VBS so i THINK this is correct.



    rem start


    Option Explicit

    Dim objFSO, objShell
    Dim strUser
    Set objShell = CreateObject("Wscript.Shell")
    strUser = objShell.ExpandEnvironmentStrings("%USERNAME%")
     
    Set objFSO = CreateObject("Scripting.FileSystemObject")
     
    If objFSO.FileExists("C:\Documents and Settings\" & strUser & "\Application Data\Microsoft\Outlook\" & strUser & ".NK2") Then
         objFSO.CopyFile "C:\Documents and Settings\" & strUser & "\Application Data\Microsoft\Outlook\" & strUser & ".NK2" , "\\server\" & strUser & "\" & strUser & ".NK2" , True
         WScript.Echo("XP File exists!")
    Else
    WScript.Echo("xp File does not exist!")
    End If
     
     If objFSO.FileExists("C:\Users\" & strUser & "\AppData\Roaming\Microsoft\Outlook\" & strUser & ".NK2") Then
         objFSO.CopyFile "C:\Users\" & strUser & "\AppData\Roaming\Microsoft\Outlook\" & strUser & ".NK2" , "\\server\" & strUser & "\" & strUser & ".NK2" , True
         WScript.Echo("Vista File exists!")
    Else
    WScript.Echo("Vista File does not exist!")
    End If

    set objFSO = nothing
    set strUser = nothing
    WScript.Quit
  • Saturday, July 04, 2009 5:37 PMperhof Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Your first approach was better. Use the %APPDATA% variable. It will work with both XP and Vista and simplifies things.
    You also had a *.nk2 in there which is the way to go.
    I think this should do it:
    Set objWshShell = CreateObject("Wscript.Shell")
    
    strSource = objWshShell.ExpandEnvironmentStrings("%APPDATA%\Microsoft\outlook\*.nk2")
    strTarget = objShell.ExpandEnvironmentStrings(<a>\\server\%USERNAME%\</a>)
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    On Error Resume Next
    objFSO.CopyFile strSource, strTarget, True
    
    If Err.Number = 0 Then
    	Wscript.Echo "File copied successfully"
    Else
    	Wscript.Echo "Error " & Err.Number & vbTab & Err.Description
    End If
    

    You can skip everything after objFSO.CopyFile if you don't need errorhandling but keep the On Error Resume Next statement or the script will fail if there are no nk2 files.
  • Saturday, July 04, 2009 8:22 PMtnetplus Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    First thank you so much I truly don't know how to thank you.

    However when I run it I receive the below error.

     

     

    syntax error line 4 char 47  the code is 800A03ea which is somewhere in this area

    strTarget = objShell.ExpandEnvironmentStrings(<a>\\server\%USERNAME%\</a>)

    • Marked As Answer bytnetplus Saturday, July 04, 2009 10:07 PM
    • Unmarked As Answer bytnetplus Saturday, July 04, 2009 10:07 PM
    •  
  • Saturday, July 04, 2009 9:36 PMtnetplus Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I have put my server path in the place where it says \\server but I can determine what the <a> at the start of the path and the </a> at the end is for.

    I am still getting the same error.  I have tried removing the a's and few other variations but still the same error and I am unable to determine the reason for the error.

     

  • Saturday, July 04, 2009 9:52 PMperhof Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have no idea what happened to the code when I posted it but it was wrong. It should be quotes - not an <a> tag.

    strTarget = objShell.ExpandEnvironmentStrings("\\server\%USERNAME%\")
  • Saturday, July 04, 2009 10:13 PMtnetplus Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    it is still giving a error on the same line but different place.

     

     

    Line 4 Char: 1   error object required: 'objshell'
    strTarget = objShell.ExpandEnvironmentStrings("
    \\SUN-RON-XP\nk2\%USERNAME%\")

    The \\sun-ron-xp is a test pc on my network

  • Saturday, July 04, 2009 11:05 PMtnetplus Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I changed one thing on the third line see below.

     

    Set objWshShell = CreateObject("Wscript.Shell")

     

    strSource = objWshShell.ExpandEnvironmentStrings("%APPDATA%\Microsoft\outlook\*.nk2")

    strTarget = objWshShell.ExpandEnvironmentStrings("\\SUN-RON-XP\nk2\%USERNAME%\")

    It did not give a VB error  but it gave               Error 76 path not found on both XP and vista

  • Saturday, July 04, 2009 11:10 PMtnetplus Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Also when i put %APPDATA%\Microsoft\outlook\*.nk2 in the run bar it does not find anything. If i put %APPDATA%\Microsoft\outlook\ it does get me to the directory.  I wonder if that wildcard is throwing the VBS for a loop.  Still you are light years ahead of me on this and i do thank you for any time you will give me.

  • Saturday, July 04, 2009 11:34 PMtnetplus Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    I am so excited this is as close as I have came thanks to you.

    Below does work if I change it to (see bottom script)

    The issue is it needs to create a folder for that user.  I had to change it to where the path was just \\server\nk2 from \\server\nk2\%username% as it was not finding that directory because it did not exist

    So if it is john smith logs on and his log on is jsmith it should create the folder jsmith in \\server\nk2\.  Also will this overwrite previous nk2 files, as it will run at each log on and needs to overwrite each time.

     

    Thanks again.

     

    Set objWshShell = CreateObject("Wscript.Shell")

    Set objShell = CreateObject("Wscript.Shell")

     

     

    strSource = objWshShell.ExpandEnvironmentStrings("%APPDATA%\Microsoft\outlook\*.nk2")

    strTarget = objShell.ExpandEnvironmentStrings("\\SUN-RON-XP\nk2\")

     

     

    Set objFSO = CreateObject("Scripting.FileSystemObject")

     

    On Error Resume Next

    objFSO.CopyFile strSource, strTarget, True

     

    If Err.Number = 0 Then

                    Wscript.Echo "File copied successfully"

    Else

                    Wscript.Echo "Error " & Err.Number & vbTab & Err.Description

    End If

     

  • Sunday, July 05, 2009 6:43 AMNickHunyady Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Couple of pointers,

    Like perhof's script shows you only need one call to Wscript.Shell

    Set objWshShell = CreateObject("Wscript.Shell")
    
    
    strSource = objWshShell.ExpandEnvironmentStrings("%APPDATA%\Microsoft\outlook\*.nk2")
    
    strTarget = objWshShell.ExpandEnvironmentStrings("\\SUN-RON-XP\nk2\")
    

    To get the current user you can add this:

    Set WshNetwork = WScript.CreateObject("WScript.Network")
    strUserName = WshNetwork.UserName

    Then to create a folder it would be:

    objFSO.CreateFolder strTarget & strUserName



     

  • Sunday, July 05, 2009 7:24 AMperhof Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Yes, I'm sorry about that Typo. Even did it twice... sigh. You only need one shell object.
    You can skip the strUsername part completely if you want to since we're already expanding environment strings.
    Just use \\SUN-RON-XP\nk2\%USERNAME%\ for strTarget (why did you remove %username% from my example?) and then do this to create the folder when necessary:

    If not objFSO.FolderExists(strTarget) Then objFSO.CreateFolder(strTarget)

    You said that it doesn't work on both Vista and XP. Is that still so? Where does it NOT work?
    You should be able to put %APPDATA%\Microsoft\outlook in the address bar in both operating systems and end up in the right folders.
    %APPDATA%\Microsoft\outlook\*.nk2 shouldn't work though. That's not a valid path in Explorer.
    • Marked As Answer bytnetplus Sunday, July 05, 2009 1:19 PM
    •  
  • Sunday, July 05, 2009 1:24 PMtnetplus Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Success.

    Thanks so much for all your time.

    It works on vista, windows 7 and XP perfectly.

    I have pasted the final result below.  Although still a lover of batch files as it did this simply in 3 or 4 lines at least VBS is a LITTLE less unknown to me today.

    Thanks again.

     

    REM start of finished script

    Set objWshShell = CreateObject("Wscript.Shell")

    strSource = objWshShell.ExpandEnvironmentStrings("%APPDATA%\Microsoft\outlook\*.nk2")

    strTarget = objWshShell.ExpandEnvironmentStrings("\\server\%USERNAME%\")

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    If not objFSO.FolderExists(strTarget) Then objFSO.CreateFolder(strTarget)

    On Error Resume Next

    objFSO.CopyFile strSource, strTarget, True

    Rem everything below this line can be removed as this is for error checking

    If Err.Number = 0 Then

                    Wscript.Echo "File copied successfully"

    Else

                    Wscript.Echo "Error " & Err.Number & vbTab & Err.Description

    End If