Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB9.0 VB.NET
For i As Integer = 0 To _dtCol.Rows.Count
                        '
    Dim nAcctNo As String = String.Empty
        nAcctNo = _dtCol.Rows(i).Item(0)
 
    If nAcctNo <> String.Empty Then
 
        Dim AcctNo As String = nAcctNo & "_JAN12.pdf"
        Dim sPath As String = AppSettings.Get("UnZipDir")
        Dim tPath As String = AppSettings.Get("ZipDir")
 
        Dim nRec As String = sPath & "\" & AcctNo
        '-- sample nRec is "C:\eSOA\Unzip\100001600019_JAN12.pdf

        Dim tRec As String = tPath & "\" & AcctNo
        '-- sample tRec is "C:\eSOA\NewZip\100001600019_JAN12.pdf

        If Dir(nRec) <> "" Then ' RECIEVE ERROR HERE "File not found."
            System.IO.File.Copy(nRec, tRec, True)
        End If
 
    End If
 
Next
 
I could not determine what the error really is?
Please help me.
 
[Edit - Fixed code block]
Posted 2-Apr-12 23:14pm
Edited 3-Apr-12 6:24am
Kschuler32.9K
v2
Comments
OriginalGriff at 3-Apr-12 4:22am
   
is there an error message?
If so where does it occur?
If not, what happens that shouldn't or doesn't happen that should?
Alan Tuscano at 3-Apr-12 4:53am
   
Hi OriginalGriff,
The error is "File not Found.!"
from the code below, i've tried analysing it over and ver but can't see the problem, yet it still throw an error.
 
If nAcctNo <> String.Empty Then
 
Dim AcctNo As String = nAcctNo & "_JAN12.pdf"
Dim sPath As String = AppSettings.Get("UnZipDir")
Dim tPath As String = AppSettings.Get("ZipDir")
 
Dim nRec As String = sPath & "\" & AcctNo
-- sample nRec is "C:\eSOA\Unzip\100001600019_JAN12.pdf
Dim tRec As String = tPath & "\" & AcctNo
-- sample tRec is "C:\eSOA\NewZip\100001600019_JAN12.pdf
 
ERROR HERE
If Dir(nRec) <> "" Then ----------->>>>>>> RECIEVE ERROR HERE "File not found."
 
System.IO.File.Copy(nRec, tRec, True)
 
End If
 
End If
Alan Tuscano at 3-Apr-12 5:06am
   
the situation is, I'll be copying records(pdf) from Unzip folder to Newzip folder, that will go with a validation on what filename from Unzip folder is to be cpied to Newzip folder.
 
I've tried copying 100001600019_JAN12.pdf(real file) into my Drive C:\,
Drive C:\100001600019_JAN12.pdf and tested it and if was successfully copied into Newzip folder,
 
But when i tried this, "C:\eSOA\Unzip\100001600019_JAN12.pdf "
error "File not found." appears.
ryanb31 at 3-Apr-12 10:37am
   
File not found means it can't find the file. There really isn't much too it. Either the file is not there or the account does not have permissions and therefore cannot "see" it.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Have you debugged and stepped through this? The error says the file doesn't exist...so that means at some point you are setting an nRec file that isn't really there.
 
I'm wondering about your loop. You have
For i As Integer = 0 To _dtCol.Rows.Count
but I'm thinking you really want
For i As Integer = 0 To _dtCol.Rows.Count - 1
 
If you have 5 rows in your table, your loop would run 0, 1, 2, 3, 4, 5. Is there really a row with an index 5?....but this still doesn't make sense to me because then I think you'd get the error at this line:
nAcctNo = _dtCol.Rows(i).Item(0) because it wouldn't find row 5.
 
You really need to debug and step through to see what is happening. Set a breakpoint and walk through the loop using F10. Check and make sure those variables for nRec and tRec are getting set the way you think they should.
  Permalink  
Comments
losmac at 3-Apr-12 12:15pm
   
My 5!
I agree with you. In theory: in a 6. step of loop (i = 5) nAcctNo is equal String.Empty, but nRec is equal C:\eSOA\Unzip\_JAN12.pdf. That's why Dir() raised error.
Kschuler at 3-Apr-12 12:22pm
   
I'm not sure how nAcctNo would equal String.Empty...I would have though it would have had a fit with there not being a row(5) and given a object not set to an instance exception on the line when nAcctNo is set. That's why I wasn't sure what was going on. It's tough to help without being able to see it debugging.
Alan Tuscano at 3-Apr-12 21:15pm
   
Here's a much better code.
 
For i As Integer = 0 To _dtCol.Rows.Count
Dim nAcctNo As String = String.Empty
nAcctNo = _dtCol.Rows(i).Item(0).ToString
 
If nAcctNo <> String.Empty Then
'
Dim AcctNo As String = nAcctNo & "_JAN12.Pdf"
Dim sPath As String = AppSettings.Get("UnZipDir")
Dim tPath As String = AppSettings.Get("ZipDir")
 
'Test Account number : 100183500018
'nAcctNo : Test Account Number
'Test Month and Year : _JAN12
'File Format : pdf
'sPath : "C:\eSOA\Unzip"
'tpath : "C:\eSOA\Newzip"
'
'Above test values is existing, the filename, source and target dir.
'
'At Step by step debug, this is the source path and file
Dim nRec As String = sPath & "\" & AcctNo '--> C:\eSOA\Unzip\100183500018_JAN12.pdf"
'At Step by Step debug, this is the Target path and filename
Dim tRec As String = tPath & "\" & AcctNo
'At step by step debug, when i reach this line below
'it doesn't go inside this loop,
If Dir(nRec) <> "" Then
MsgBox("File exists")
'This will copy the above test values to the target directory(folder)
System.IO.File.Copy(nRec, tRec, True)
 
End If
 
'But when I tried this,
If Dir("C:\eSOA\Unzip\100183500018_JAN12.pdf") <> "" Then
MsgBox("File exists")
'Record was successfully copied to target directory
System.IO.File.Copy("C:\eSOA\Unzip\100183500018_JAN12.pdf", tRec, True)
 
End If
 
End If
Next i
 

As for the row count, i would have a different error "Index out of bound" or something like that. so, row count is not my problem.
Kschuler at 4-Apr-12 9:08am
   
If it works when you hard code the string file name, then my first guess is that when you are using variables instead it isn't being set properly. When you debug with a breakpoint directly on the Dir() function line is th nRec variable really holding the correct value? Perhaps somehow you are missing the root of the path or got spaces in the string somehow, or are missing a slash? If in debug it looks exactly like you are expecting, my next suggestion would be to use a different method to determine if the file is there. I'm not familiar with the Dir() function, I always use System.IO.File.Exists(strFilePath). You could try that instead and see if you get the same results as dir().

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 319
1 Maciej Los 295
2 Sergey Alexandrovich Kryukov 230
3 CPallini 205
4 Sinisa Hajnal 166
0 OriginalGriff 7,760
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,785


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 3 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100