Solved

Help with error trapping in a script

Posted on 2004-09-18
7
196 Views
Last Modified: 2010-05-02
I'm moving some 1600 files from one server to another. The destination server has folder names that match the source file name. The files are word documents i want stuffed into the matching folder name on the destination server. It would be nice also if the directory don't exist on the destination to create it and then stuff the file.  I think the below script will work if you could trap the "file already exist" error.  If the file already exist on the destination server i would just like to skip it and go to the next file and try to move it.  Hope this makes sense.  Below is the script i am working with at this time. If you have a better way please show me.

'  MoveDataFiles.VBS
'  Moves files from one directory to another directory where the destination directory is
'  named the same as the filename of the file.  Example:
'            Source Directory ==>  c:\temp
'           Source Filename  ==>  abc123.doc
'      Destination Directory ==>  abc123
'      Destination Filename ==>  abc123.doc
'  This script assumes that the destination directory already exists.

intCount = 0
strSourcePath = "\\jaxnt51\proceng\pms\pm\"
strDestinationPath = "\\jaxnt54\plant\machines\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strSourcePath)
Set colFiles = objFolder.Files

For Each objFile in colFiles
     intCount = intCount + 1    
     strFileName = objFile.Name
     DirectoryArray = Split(strFileName, ".", -1, 1)
     
     objFSO.MoveFile strSourcePath & strFilename, strDestinationPath & DirectoryArray(0) & "\" & strFileName

Next

Wscript.Echo "File moves completed.  " & intCount & " files moved."


'  End of MoveDataFiles.VBS
0
Comment
Question by:JAYRU
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 52

Assisted Solution

by:Ryan Chong
Ryan Chong earned 150 total points
ID: 12091620
Try check on the FileExists method in FSO, like:

so..


intCount = 0
strSourcePath = "\\jaxnt51\proceng\pms\pm\"
strDestinationPath = "\\jaxnt54\plant\machines\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strSourcePath)
Set colFiles = objFolder.Files

For Each objFile in colFiles
     intCount = intCount + 1    
     strFileName = objFile.Name
     DirectoryArray = Split(strFileName, ".", -1, 1)
 
'Do only file doesn't exist  
  If objFSO.FileExists(strDestinationPath & DirectoryArray(0) & "\" & strFileName) = False Then
     objFSO.MoveFile strSourcePath & strFilename, strDestinationPath & DirectoryArray(0) & "\" & strFileName
 End If

Next
0
 

Author Comment

by:JAYRU
ID: 12091842
ryancys,
I have to wait till Monday to have my friend from IT department to recompile the script.  I don't have VB on this machine.  Unless you want to compile it and email me the file.  If you do name it  MoveDataFiles.vbs.........You may have to change the file extention to something other than vbs.  Not sure if Yahoo lets vbs through.  If not i'll wait till Monday...........thanks.      jayrues@yahoo.com
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12091860
Hi JAYRU,

You don't compile a vbscript, it is simply a text file with the .vbs extension.  You can open up NotePad, paste the code, save the file, change the extension from .txt to .vbs and then you have your script ready to run.

Regards,

Idle_Mind
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:JAYRU
ID: 12091891
hmmmmmmmm.........I did that and it did nothing with no errors.  Thats why I was thinking maybe it had to be compiled.  Now what?
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 150 total points
ID: 12092025
Here is my version:

'  MoveDataFiles.VBS
'  Moves files from one directory to another directory where the destination directory is
'  named the same as the filename of the file.  Example:
'           Source Directory ==>  c:\temp
'          Source Filename  ==>  abc123.doc
'     Destination Directory ==>  abc123
'     Destination Filename ==>  abc123.doc

' Operation tracking counters
filesMoved = 0
filesSkipped = 0
foldersCreated = 0

' Source and Destination folders
strSourcePath = "\\jaxnt51\proceng\pms\pm\"
strDestinationPath = "\\jaxnt54\plant\machines\"

' Create FileSystemOject, get reference to Source folder and collection of source files
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strSourcePath)
Set colFiles = objFolder.Files

' Process each file in Source folder
For Each objFile in colFiles
    ' Strip off extension
    strFileName = objFile.Name
    name = strFileName
    ext = objFSO.GetExtensionName(objfile)
    If ext <> "" Then
        name = Left(name, len(name) - (len(ext) + 1))
    End If
   
    ' Create destination folder if it doesn't exist
    If Not objFSO.FolderExists(strDestinationPath & name) Then
        objFSO.CreateFolder(strDestinationPath & name)
        foldersCreated = foldersCreated + 1
    End If
   
    ' Move file if it doesn't exist
    If Not objFSO.FileExists(strDestinationPath & name & "\" & strFileName) Then
        objFSO.MoveFile strSourcePath & strFileName, strDestinationPath & name & "\" & strFileName
        filesMoved = filesMoved + 1
    Else
        filesSkipped = filesSkipped + 1
    End if
Next

' Give feedback to the user
msg = filesMoved & " files moved." & vbCrLf
msg = msg & filesSkipped & " files skipped." & vbcrlf
msg = msg & foldersCreated & " folders created."
MsgBox msg, vbInformation, "File moves complete"
0
 

Author Comment

by:JAYRU
ID: 12092030
Ryancys,
New revelation.  I started over with the original script i posted and just plugged in your IF EXIST code and it did say it moved 2 files but should of moved some 1600.........something is working anyway........below is the code that did move 2 files but not 1600.

_________________________________
intCount = 0
strSourcePath = "\\jaxnt51\proceng\pms\pm\"
strDestinationPath = "\\jaxnt54\plant\machines\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strSourcePath)
Set colFiles = objFolder.Files

For Each objFile in colFiles
     intCount = intCount + 1    
     strFileName = objFile.Name
     DirectoryArray = Split(strFileName, ".", -1, 1)

'Do only file doesn't exist  
  If objFSO.FileExists(strDestinationPath & DirectoryArray(0) & "\" & strFileName) = False Then
     objFSO.MoveFile strSourcePath & strFilename, strDestinationPath & DirectoryArray(0) & "\" & strFileName
end if

Next

Wscript.Echo "File moves completed.  " & intCount & " files moved."


'  End of MoveDataFiles.VBS
____________________________
0
 

Author Comment

by:JAYRU
ID: 12092128
I don't know what happened but I think it was that I didn't refresh explore but it did move all the files.  I'm  not even sure which one of your scripts i was using at the time but both scripts are very valueable to me and if you think its fair i am going to up the points to 300 and then split the points...........EE and its experts never let me down.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question