Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy files from list 1

Status
Not open for further replies.

ipeca

IS-IT--Management
Jan 11, 2007
17
0
0
US
Hello all,

I'm hoping someone can help me out and save my hair....

What I am trying to do, is copy a list of jpgs contained in a txt file from folder A to Folder B, so that I can burn them to a cd.

This script worked ok with a move ... not sure what I did, or didn't do, to get it to quit working.

Code:
Const ForReading = 1

strComputer = "."
set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\scripts\copy.txt", ForReading)

Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    arrParts = Split(strLine, ",")
    strFile = "C:\\scripts\\" & arrParts(0)
    Set colItems  = objWMIService.ExecQuery _
        ("Select * From CIM_Datafile Where Name = '" & strFile & "'")
    For Each objItem in colItems
        strNewName = "C:\\scripts2\" & arrParts(1)
        object.CopyFile strNewName
    Next
Loop

objFile.Close

any ideas?

thnx
 
Hi JPeca

All looks fine here except what's highlighted in Red


strFile = "C:\\scripts\\" & arrParts(0)
Set colItems = objWMIService.ExecQuery _
("Select * From CIM_Datafile Where Name = '" & strFile & "'")
For Each objItem in colItems
strNewName = "C:\\scripts2\" & arrParts(1)
object.CopyFile strNewName
Next
Loop

objFile.Close
 
I was under the impression the WMI query was needed to maniuplate the FSO in Windows. I'm probably wrong/misinformed as I am a VBscript noob and still unsure how the WMI plays into the scripting language, other than doing a good job of confusing the hell out of me.

The input file currently has a list of test files that I would like to copy from folder A to folder B. What it is really going to be used for is to have the script go through the list and copy a slew of jpg files from a mainframe (going to map a drive in windows) to another location, so I can burn the selected images to a cd. Although I'm starting to wonder if it wouldn't just be easier to write this in C.
 
No WMI is not needed for FSO operations...that's what FileSystemObject is for....unfortunately the scripting center keeps showing examples of using WMI for a lot of FS operations which is not necessarily the best option when working locally....performance is one...... though it may come in handy for remote operations.

If you are working with local files/directories....stick with FileSystemObject

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
>The input file currently has a list of test files

I cannot figure out from your code what your input file looks like, so you will need to provide a sample from that text file. Does it contain names only? Full paths? And "arrParts = Split(strLine, ",")" implies a comma delimiter in each line... which separates what?
 
Im sorry your query syntax is fine, and dm$ever is correct.

You don't need wmi to use file operations, just the FileSystemObject. More details are required on file format and error if any was generated
 
Ok after sleeping on this overnight, and literally dreaming about the code....(not sure if that's a good sign...especially when I dreamt the code that works, and can't recall it) taking the advice to abandon WMI all together....I came up with a completely different script.

Code:
Option Explicit

Dim oFSO, sFile, oFile, sText
Dim objFSO, objFileCopy
Dim strFilePath, strDestination

'Set the read of the input file and prompt for location
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = InputBox("Enter path to text document with files to be copied:")

'Set the source and destination variables for the copy
strFilePath = sText
strDestination ="c:\scripts2\(sText)"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileCopy = objFSO.GetFile(strFilePath)

'Open the read, get the file name of the file to be copied, and copy it to new location
If oFSO.FileExists(sFile) Then
Set oFile = oFSO.OpenTextFile(sFile, 1)
 Do While Not oFile.AtEndOfStream
  sText = oFile.ReadLine
   If Trim(sText) <> "" Then
    objFileCopy.Copy (strDestination)
   End If
 Loop
oFile.Close
Else
WScript.Echo "The file was not there."
End If

List of the input file (which is a txt file in the scripts directory).
Code:
classpaths.java
cms_restart.vbs
lab1.vbs
lab2.vbs
test5.vbs
test.vbs

The logic of the vbscript is to have the script prompt you to enter location of input file (that works), then read the input file, copy the file listed in input file to new location, and reiterate through EOF.

The problem I'm having with the new code is getting the files to copy from the list. I don't know how to add the source path to the read, ie C:\<...source directories..>\file.toCopy



 
Your code has some things out of sequence, here's a modified version (not tested). Just make sure strSource and strDestination are set correctly.
Code:
Option Explicit

Dim oFSO, sFile, oFile, sText
Dim objFSO, objFileCopy
Dim strFilePath, strDestination, strSource
Const ForReading = 1, ForWriting = 2, ForAppending = 8

strSource = "c:\source folder\" 'Enter your source path here
strDestination = "c:\scripts2\" 'Enter your destination path here

'Set the read of the input file and prompt for location
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = InputBox("Enter path to text document with files to be copied:")

'Open the read, get the file name of the file to be copied, and copy it to new location
If oFSO.FileExists(sFile) Then
   Set oFile = oFSO.OpenTextFile(sFile, ForReading)
   Do While Not oFile.AtEndOfStream
      sText = oFile.ReadLine
      If (Trim(sText) <> "") And _
         oFSO.FileExists(strSource & "\" & sText) Then
        	
         oFSO.CopyFile strSource & "\" & sText, strDestination
      Else
         WScript.Echo "Couldn't find " & strSource & "\" & sText
      End If
   Loop
   oFile.Close
Else
   WScript.Echo "The file was not there."
End If
 
That worked, thanks.

I would say there was a little more than out of sequence code, I completely hosed the nested if statement. Thanks for helping a n00b.

Much to learn I have.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top