Click here to Skip to main content
15,880,427 members

Moving Files from Network Folder Using VBScript

ralphraptor asked:

Open original thread

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.

VB
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
Tags: VBScript, Scheduler

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900