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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename Files on Remote Computer under Vista 1

Status
Not open for further replies.

Schweiger

Programmer
Apr 26, 2002
122
CH
Hi

I've got troubles moving files on a remote computer from one directory to another.
My environment is Windows Vista Business where the VB runs and Windows Vista Home where the share is located on. If I use Windows XP on the remote computer it works.
To enable my NAS I've changed SMB Authentification on Vista.
Here my code:

Code:
    Dim temp As String
    
    temp = Dir("\\SRV\Test\NEW\*.*")
    Do While temp <> ""
[COLOR=blue]        Name "\\SRV\Test\NEW\" & temp As "\\SRV\Test\ARCHIV\" & temp[/color blue] ' runtime error 52 
        temp = Dir("\\SRV\Test\NEW\*.*")
    Loop

The first file is moved. Then I get runtime error 52:Bad file or filename on the line where the file is renamed. The cause is that Dir returns the allready moved file again.
I know, I can use Dir() in the loop then all files are moved but when the routine is called again within the next seconds Dir returns the first moved file again. It seems like if Vista needs some time to get aware that the files aren't on the old location any more.

thanks for any help

schweiger
 
I suggest you do not mess with the files under Dir$'s 'spell' while you are using it.
Use Dir$ to get the filenames into a string array and (when that is finished) do the actions required in a separate loop using the filenames retrieved.
 
I Changed it to an Array but it doesn't work.
The files are moved correctly but if I start the sub again within some seconds I get the same error and ary(ii) still contains the first file that has been moved. If I wait about 10 seconds and start the sub again it works.

Code:
    Dim temp As String
    Dim ary(10) As String
    Dim i As Integer
    Dim ii As Integer
    
    i = 0
    ary(i) = Dir("\\SRV\Test\NEW\*.*")
    Do While ary(i) <> ""
        i = i + 1
        ary(i) = Dir()
    Loop
    i = i - 1
    For ii = 0 To i
        Name "\\SRV\Test\NEW\" & ary(ii) As "\\SRV\Test\ARCHIV\" & ary(ii) ' runtime error 52
    Next ii

I tried with FSO too. This works a little better. The files aren't in the catalog any more but the counter of files is still set to the amount of files that have been moved. I could just try to access the first file there and if I get an error I could ignore but it's annoying to debug such code.
 
Try something like...
[tt]
Public Sub DoSomeWork()
Dim ArrayOfFiles() As String, ForLoopCounter As Integer
Dim LowerBoundOfArray As Integer, UpperBoundOfArray As Integer
Dim OrigPath As String
OrigPath = "\\SRV\Test\NEW\"
ArrayOfFiles = Split(GetFilesFromDir(OrigPath & "*.*"),",")
LowerBoundOfArray = LBound(ArrayOfFiles)
UpperBoundOfArray = UBound(ArrayOfFiles)
For ForLoopCounter = LowerBoundOfArray To UpperBoundOfArray
If Trim(ArrayOfFiles(ForLoopCounter))<>"" Then
Name OrigPath & ArrayOfFiles(ForLoopCounter) As "\\SRV\Test\ARCHIV\" & ArrayOfFiles(ForLoopCounter)
End If
Next ForLoopCounter
End Sub

Public Function GetFilesFromDir(PathPattern As String) As String
Dim ReturnString As String, FoundFiles As String
ReturnString = Dir(PathPattern)
Do While ReturnString <> ""
'Ignore the current directory and the encompassing directory.
If ReturnString <> "." And ReturnString <> ".." Then
'Use bitwise comparison to make sure ReturnString is Not a directory.
If (GetAttr(InitialPath & ReturnString) And vbDirectory) <> vbDirectory Then
FoundFiles=FoundFiles & ReturnString & ","
End If
End If
Loop
GetFilesFromDir=FoundFiles
End Function
[/tt]
Or something like that

It returns all files found in directory instead of just processing 10/11 at a time.

Good Luck

 
thanks vb5prgrmr I know that my code fragments aren't very generic. They should only show the problem.
Nevertheless I implemented your version and I get the same error on the line GetAttr.
Dir just reports that the file is there on the location where it has been before it was moved, and I just tested it, it is at the same time at the location it has been moved to.
Waiting 10 seconds and it is vanished where it has been moved from.
 
Well from the sounds of it you are wanting to have near real time response to files appearing in your source directory. If so, see strongm's post in thread222-1021982 where he links to two other threads where he has given two different versions of readdirectorychangesw API. I believe they should work over a network, could be wrong as I have not tested this but if we are lucky strongm will pipe in and clear everything up. Otherwise, there is no need to hit your server/share/net directory so hard, so often. You could wait minutes, hours, or only do it once a day as you have not stated the reason why you are hitting it so hard.

 
It is a problem of the refreshing of cached folder contents of part of the OS, and a delay until the system refreshes it.

I added in the following code an extra check if the file still exists, in case the routine is called again right away. It may help:



Code:
[COLOR=blue]
Public Function FilesList(sSourceFldr As String) As String()
    '(Add your own error handling)
    Dim oFolder     As Variant '(or use FSO Scripting.Folder object)
    Dim oFile       As Variant '(or use FSO Scripting.File object)
    Dim aryFiles()  As String
 
    With CreateObject("Scripting.FileSystemObject")
        Set oFolder = .GetFolder(sSourceFldr)
        '=======================================================================
        For Each oFile In oFolder.Files
            If .FileExists(.BuildPath(oFile.ParentFolder, oFile.Name)) Then
                If ((Not aryFiles) = -1) Then
                    ReDim aryFiles(0)
                Else
                    ReDim Preserve aryFiles(UBound(aryFiles) + 1)
                End If
                aryFiles(UBound(aryFiles)) = .BuildPath(oFile.ParentFolder, oFile.Name)
            End If
        Next oFile        
        '=======================================================================
    End With
    
    FilesList = aryFiles
End Function[/color]

Code:
Public Sub TestIt()
    Dim aryFiles()  As String
    Dim vFile       As Variant
    
    aryFiles = FilesList("C:\MyFolder")
    
    'See if array is already dimensioned
    If ((Not aryFiles) <> -1) Then
        For Each vFile In aryFiles
            Debug.Print CStr(vFile)
        Next vFile
    End If
End Sub
 
>readdirectorychangesw API. I believe they should work over a network
>strongm will pipe in


And, as if by magic ...

It'll work against remote folders, yes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top