Looking for a utility for copying files

Hello everyone,

I have a folder that has about 20,000 word perfect documents, although not all of them have a wpd extension.  I have a list (comma delimited txt file) of about 500 file names.  I need a utility that will look at my list, then find the matching file in the WP directory and copy that file to a different location.  my comma delimited list is looks like this:

12345, 896558, 257486, 5485236, 8759632

The file names in the folder that correspond would look like this:

12345.wpd
896558.txt
257486.wpd
5485236.wpd
8759632.txt

At the end of the day, I need all 20,000 files to stay in the original folder, and the 500 or so to be copied to another file.

Can anyone help?

Thanks!!!
DebbieFostAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ThommyCommented:
Can you change format of your list from comma delimited to line breaks...

12345
896558
257486
5485236
8759632

I can provide a solution, if you are able to change your list into a format like above...
0
DebbieFostAuthor Commented:
Yep, I sure can.  I can pretty much put it in any format.
0
prashanthdCommented:
Try the following code...

It copies matching .wpd files, the input should be delimited line break

I can even write a script if input is delimited comma.
inputfile="c:\input.txt"
source_path="c:\wp\" 'ensure "\" is given at end of path
destination_path="c:\wpd\" 'ensure "\" is given at end of path

On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(inputfile, 1)

Do While objTextFile.AtEndOfStream <> True
    strfile=Trim(objtextfile.ReadLine)
    file_path=source_path & strfile & ".wpd"
    If objfso.FileExists(file_path) Then
    	objfso.CopyFile file_path, destination_path
    end if	
Loop

Open in new window

0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

prashanthdCommented:
The above code has a limitation to copy only .wpd files, the following copies .wpd and .txt
inputfile="c:\input.txt"
source_path="c:\wp\" 'ensure "\" is given at end of path
destination_path="c:\wpd\" 'ensure "\" is given at end of path

On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(inputfile, 1)

Do While objTextFile.AtEndOfStream <> True
    strfile=Trim(objtextfile.ReadLine)
    file_path=source_path & strfile & ".wpd"
    If objfso.FileExists(file_path) Then
    	objfso.CopyFile file_path, destination_path
        wscript.echo file_path & " copied"
    else
	file_path=source_path & strfile & ".txt"
    	If objfso.FileExists(file_path) Then
	    objfso.CopyFile file_path, destination_path
	    wscript.echo file_path & " copied"
        end if 
    end if	
Loop

Open in new window

0
ThommyCommented:
No problem with a list file delimited by line breaks.

You can do it in an awful simple batch script!!!
;o)

You just have to adapt the variables for your folders and listfile...


CopyListFiles.cmd
0
ThommyCommented:
Have you already found time to try my script???
0
DebbieFostAuthor Commented:
Sorry for the delayed response, I have been out at a conference and have had very limited access.  I am going to try this tomorrow, or Thursday at the latest and will let you know.  Thanks!!
0
DebbieFostAuthor Commented:
Ok - looks great...but one final request....can you suppress the "FILE COPIED" message that we get after each copy?
0
prashanthdCommented:
To supress remove line no 19  

wscript.echo file_path & " copied"


0
DebbieFostAuthor Commented:
This fails as soon as it fails to fine a file that is not represented in the input file (i..e we have a record 06-89993 in out input file, however there is no corresponding document in the source folder, so the script just fails)

Can we adjust this script so it skips any missing source files?

Also - oddly I remove the above text to suppress the message, however the message still shows (?!)
0
prashanthdCommented:
The script will not fail even if the file does not exist, not sure what could be the reason.

The On Error Resume Next (line no 5) statement skips and moves on even if any errors, so that the script moves next even if an error is encountered.

Could you post the error you are receiving?

For the suppressing you should remove line no 14 also, missed it earlier.
0
DebbieFostAuthor Commented:
There is no error message - we just noticed that the it stops running after the 9th file is copied....we then check and found that the 10th file does not exist
0
DebbieFostAuthor Commented:
Could it be that the "on error resume" is not working correctly?
0
DebbieFostAuthor Commented:
I'm not a VB dude....but is the "Else" statement correct? It looks like it is just trying to copy again instead of skipping.
0
DebbieFostAuthor Commented:
I have done more research - many of the files that copy DO NOT have a .ext (i.e. file name is 12-3456 ) - these appear to not be copying. Can we adjust so that it will copy regardless of whether there is an ext?
0
prashanthdCommented:
Try the following
inputfile="c:\input.txt"
source_path="c:\wp\" 'ensure "\" is given at end of path
destination_path="c:\wpd\" 'ensure "\" is given at end of path

On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(inputfile, 1)

Do While objTextFile.AtEndOfStream <> True
    
    strfile=Trim(objtextfile.ReadLine)
    Set objfolder=objfso.GetFolder(source_path)
    
    For Each cfile In objfolder.Files
        file_path=cfile.path
        'WScript.Echo cfile.Path
        'WScript.Echo LCase(Mid(cfile.name,1,InStr(cfile.name,".")-1))
        
        If LCase(Mid(cfile.name,1,InStr(cfile.name,".")-1))=LCase(strfile) Then
            If objfso.FileExists(file_path) Then
                objfso.CopyFile file_path, destination_path
                Exit For
            End If    
        End If    
    Next
Loop

Open in new window

0
prashanthdCommented:
Made some more changes, try the following
inputfile="c:\input.txt"
source_path="c:\wp\" 'ensure "\" is given at end of path
destination_path="c:\wpd\" 'ensure "\" is given at end of path

On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(inputfile, 1)

Do While objTextFile.AtEndOfStream <> True
    
    strfile=Trim(objtextfile.ReadLine)
    Set objfolder=objfso.GetFolder(source_path)
    
    For Each cfile In objfolder.Files
        file_path=cfile.path
        'WScript.Echo cfile.Path
        'WScript.Echo LCase(Mid(cfile.name,1,InStr(cfile.name,".")-1))
        
        If (LCase(Mid(cfile.name,1,InStr(cfile.name,".")-1))=LCase(strfile)) Or (LCase(Mid(cfile.name,1,InStr(cfile.name,".")))=LCase(strfile)) Then
            If objfso.FileExists(file_path) Then
                objfso.CopyFile file_path, destination_path
                Exit For
            End If    
        End If    
    Next
Loop

Open in new window

0
BillDLCommented:
If prashanthd's VBS approach doesn't work, then a simple revision to Thommy's batch file should work.  Rename attached .TXT file to .CMD and change the SET= lines at the top to reflect the actual paths in use.  Obviously it would be a good idea to first try it on some test files and folders.

The revision is simply the addition of another IF EXIST test which modifies the %%G variable (using %%~dpnG) so that it expands the stored value to ONLY the Drive, Path, and File Name of the line being read from your List File with each pass of the FOR loop.

The tests should just fall through, first testing for the existence of the file as a .TXT file and copying if found, then as a .WPD file, and finally as a file with no extension if the two previous tests failed.  It's a clunky approach, but with only 500 files it shouldn't take too long.

 RevisedCopyListFiles.txt
0
BillDLCommented:
Whoops DON'T use that.  I had some misplaced "'s in it.  Use this instead.

 RevisedCopyListFiles.txt
0
DebbieFostAuthor Commented:
This still does not copy any documents that do not have an extension (older Word Perfect documents do not have extensions)
0
aikimarkCommented:
You could create a batch file (or three) with your Word processor (or similarly sophisticated editor) that would copy the files.

In this example, the target directory is named copytest

copy 12345.* copytest
copy 896558.* copytest
copy 257486.* copytest
copy 5485236.* copytest
copy 8759632.* copytest

Open in new window


in my test, this copies files with different file types and with no file type
0
DebbieFostAuthor Commented:
BillDl - can you add an error log to your script? It seems to work fine (I batch added extensions), but I need to have a log to show me what was skipped?
0
BillDLCommented:
Hi Debbie

I would be inclined to write my own batch file from scratch rather than re-use Thommy's one.  There's nothing wrong with his batch file, it's roughly what my approach was going to be when I first looked at the question, but I just prefer my own layout and modifying someone else's to "pretty it up" isn't right.

The batch file SHOULD copy files without extension.  Although I didn't test it fully with a file list, I did do a quick command line check to test if the COPY command would, eg.

copy "c:\_source\123456" "c:\_dest\"
and
copy "c:\_source\123456" "c:\_dest\123456"

They both work fine.  If you follow this with an error return code check:
echo %errorlevel%
it should show a Zero for success, anything other than that is a failure that can be tested and can be used in a batch file.

I will need to leave this until a little later today though.
0
BillDLCommented:
I just read back through your question again.

How do you know which file in the source folder needs to have a *.wpd or *.txt extension applied when copied to the destination folder?

You mean that your File List HAS the extensions for the files you need to copy, but the ACTUAL files in the source folder DON'T have extensions?

Is my understanding correct?
0
DebbieFostAuthor Commented:
BillDl - the copy works fine now (I batched added extensions  - so don't worry about that ).

My issue is that my input file contained 516 records.....the source document folder contains 8600 documents to be selected from....and our destination folder contains only 501 documents....so I need to know which records in the input file were not copied (there are 15 missing - which would just mean that they do not have documents in the source folder - but I need to know WHICH ones are missing).

So my final request is to have a log that shows me the ones that were not copied (or error-ed).
0
aikimarkCommented:
you can port the result of the copy command to a file.

In this example, the copy results are appended to the copyresult.txt file
copy 123456*.* copytest 1>>copyresult.txt

Open in new window


The contents of the copyresult.txt file after the above command executes are:
123456.pdf
123456A.pdf
123456B.pdf
        3 file(s) copied.

Open in new window

0
prashanthdCommented:
Try the following code, will display files which are not in source dir.
inputfile="c:\input.txt"
source_path="c:\wp\" 'ensure "\" is given at end of path
destination_path="c:\wpd\" 'ensure "\" is given at end of path

On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(inputfile, 1)

Do While objTextFile.AtEndOfStream <> True
    
    strfile=Trim(objtextfile.ReadLine)
    Set objfolder=objfso.GetFolder(source_path)
    
    file_exists=False
    For Each cfile In objfolder.Files
        file_path=cfile.path
        'WScript.Echo cfile.Name
        If InStr(LCase(cfile.Name),LCase(strfile))>0 Then
            If objfso.FileExists(file_path) Then
                objfso.CopyFile file_path, destination_path
                'WScript.Echo cfile.Name & " copied"
                If Err.Number<>0 Then
                    WScript.Echo cfile.Name &" error while copying file"
                End If
                file_exists=True
                Exit For
            End If    
        End If    
    Next
    If file_exists=False Then
        WScript.Echo strfile & " file not in source"
    End If
    
Loop

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ThommyCommented:
Sorry, could not return sooner back to your question!!!

I have tested my batch script and it works fine.

Do you have any problems with it???
@ECHO OFF

SET ListFile=c:\input.txt
SET FromFolder=c:\Folder1
SET ToFolder=c:\Folder2

FOR /F %%G IN (%ListFile%) DO (
  if EXIST %FromFolder%\%%G.txt (
    @echo Copy %FromFolder%\%%G.txt to  %ToFolder%\%%G.txt
    copy %FromFolder%\%%G.txt %ToFolder%
  )
  if EXIST %FromFolder%\%%G.wpd (
    @echo Copy %FromFolder%\%%G.wpd to %ToFolder%\%%G.wpd
    copy %FromFolder%\%%G.wpd %ToFolder%
  )
)

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.