Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VBScript Scheduler

I have a simple script that maps a shared folder from one of our servers. And then move all the files from one of the folders in it to the local machine. I created an entry in windows scheduler to run this script every 10 minutes. The script works fine when I am logged in the machine.

The problem that I have is that, the script doesnt move the files when I am not logged in the machine.

I did a lot of searching but have not found any solution to this yet. Thanks for your help in advance.

Below is my script.

Set objNetwork = CreateObject("WScript.Network")
sUserName = objNetwork.UserName
Dim aNetworkDrives(4, 2)
'
aNetworkDrives(0, 0) = "Z:"
aNetworkDrives(0, 1) = "<a href="file://pronto.grahamgroup.local/gdb">\\pronto.grahamgroup.local\gdb</a>"
'
sPersistent = "FALSE"
sUsername = "MyUserNameHere"
sPassword = "MyPasswordHere"

'
'Map network drive(s)
'
Set oCheckDrive = objNetwork.EnumNetworkDrives()

On Error Resume Next

For iNetDriveCounter = 0 To Ubound(aNetworkDrives, 1) - 1 Step 1

bAlreadyConnected = False

For iCheckCounter = 0 To oCheckDrive.Count - 1 Step 1

IF oCheckDrive.Item(iCheckCounter) = aNetworkDrives(iNetDriveCounter, 0) THEN 

bAlreadyConnected = True

END IF

Next

IF bAlreadyConnected = False THEN

objNetwork.MapNetworkDrive aNetworkDrives(iNetDriveCounter, 0),
    aNetworkDrives(iNetDriveCounter, 1), sPersistent, sUsername, sPassword

END IF

Next

'

'Move files

'

sSource= "Z:\francistestfolder" 

sDestination= "C:\Documents and Settings\francisp\Desktop\test map\b\" 

getFolder sSource, sDestination

Function getFolder(ByVal sSource, ByVal sDestination)

    Dim fso, file, files

Set fso = CreateObject("Scripting.FileSystemObject") 

    If fso.FolderExists(sSource) Then

        For Each file In fso.GetFolder(sSource).Files

            'WScript.Echo file

sDestinationFileName = sDestination & file.Name

            If fso.FileExists(sDestinationFileName) Then

                sDestinationFileName = RenameFile(sDestinationFileName)

            End If

            file.Move(sDestinationFileName)

        Next

Else 

    Exit Function

End If 

End Function

Function RenameFile(ByVal sFileName)

RenameFile = sFileName & Year(Now) & Month(Now) & Day(Now) & Hour(Now) & 
    Minute(Now) & Second(Now)

End Function
Posted 22-Nov-09 16:42pm
Edited 23-Nov-09 7:11am
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Found a little different script for copying.

Check if this can help you
http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/filesfolders/files/

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This sounds like you need to create a Windows Service. A windows service will run even when you are not logged in. Of course to do that, you would have to have Admin privileges and the VBScript would have to be in C# or VB.
 
The following code (in C#) can be used to create a windows service to move files every 10 seconds and rename files if the file being moved already exists in the destination folder.
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Timers;
namespace Cron
{
    partial class Service2 : ServiceBase
    {
        private Timer ti = new Timer();
        private string fileTime;
        private string[] theCommands = new string[2];
        private EventLog eventLog1 = new EventLog();
        private FileStream fs;
 
        public Service2()
        {
            InitializeComponent();
            //This is a timer where code can run once the timer is up.
            ti.Elapsed += new ElapsedEventHandler(ti_Elapsed);
            //This sets the 10 second interval.  Every 1000 is one second.
            ti.Interval = 10000;
            ti.Enabled = true;
            //This sets up the Event log which is viewable in the 
            if (!System.Diagnostics.EventLog.SourceExists("CronMessage"))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                   "CronMessage", "CronLog");
            }
            eventLog1.Source = "CronMessage";
            eventLog1.Log = "CronLog";
        }
        void ti_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                MoveFiles(theCommands[0], theCommands[1]);
            }
            catch (Exception ex)
            {
                eventLog1.WriteEntry(ex.Message, EventLogEntryType.Error);
            }
        }
        public void MoveFiles(string sourceFolder, string destFolder)
        {
            try
            {
                DateTime dt = DateTime.Now;
                string year = dt.Year.ToString("0000");
                string month = String.Format("{0:MMM}", dt);
                string day = dt.Day.ToString("00");
                string hour = dt.Hour.ToString("00");
                string min = dt.Minute.ToString("00");
                string sec = dt.Second.ToString("00");
                string time = year + month + day + "_" + hour + min + sec;
                
                if (Directory.Exists(destFolder))
                {
                    string[] files = Directory.GetFiles(sourceFolder);
                    foreach (string file in files)
                    {
                        string name = Path.GetFileName(file);
                        string dest = Path.Combine(destFolder, name);
                        if (File.Exists(dest))
                        {
                            name = Path.GetFileNameWithoutExtension(file) + time + Path.GetExtension(file);
                        }
                        dest = Path.Combine(destFolder, name);
                        File.Move(file, dest);
                    }
                    //This checks for sub-folders and moves those files also.
                    //If this is not desired, this can be commented out.
                    string[] folders = Directory.GetDirectories(sourceFolder);
                    foreach (string folder in folders)
                    {
                        string name = Path.GetFileName(folder);
                        string dest = Path.Combine(destFolder, name);
                        MoveFiles(folder, dest);
                    }
                }
                else
                {
                    throw new Exception("Folder does not exist!\nPlease check the file: " + fs.Name);
                }
            }
            catch (Exception ex)
            {
                eventLog1.WriteEntry(ex.Message, EventLogEntryType.Error);
            }
        }
        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            eventLog1.WriteEntry("Cron Backup service started.");
            //The text file contains the source and destination
            //folders is opened here.
            fs = File.OpenRead("C:\\Path\\to\\text\\file");
            StreamReader sr = new StreamReader(fs);
            sr.ReadLine();//This ignores the [Source Folder] line.
            theCommands[0] = sr.ReadLine();
            sr.ReadLine();//This ignores the blank line.
            sr.ReadLine();//This ignores the [Destination Folder] line.
            theCommands[1] = sr.ReadLine();
            fs.Close();
        }
        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
            eventLog1.WriteEntry("Cron Backup service stopped.");
        }
    }
}
 
The text file can contain a UNC Path (i.e. \\servername\somefolder) and should work as long as the Local System account has access to that folder.
 
Example of Text file:
 
[Source Folder]
\\ServerName\some_Folder
 
[Destination Folder]
C:\Some_Folder

 
If people have enough interest in this, I can expound on the subject.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Marcin Kozub 310
1 OriginalGriff 248
2 Sergey Alexandrovich Kryukov 215
3 Richard MacCutchan 202
4 Praneet Nadkar 197
0 OriginalGriff 8,048
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 25 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100