Solved

Help with error trapping in a script

Posted on 2004-09-18
7
192 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
  • 4
  • 2
7 Comments
 
LVL 49

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 85

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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 85

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now