help with checking a directory

sspreyer

Registered User.
Local time
Today, 09:20
Joined
Nov 18, 2013
Messages
251
hi ,all

i struggling to check a directory see if a file exists

current the code i have the user selects the file from the file picker it then copies the file to another folder to stop people linking it to there personal drive, but the issues is i need to check that the file they are selecting doesn't already exist in the folder it's being copied too. Some how i need to cross referance path1 to .selected items (1)

Code:
Dim fDialog As Office.FileDialog
   Set fd = Application.FileDialog(msoFileDialogFilePicker)
   Dim varFile As Variant

Dim path1 As String
path1 = "C:\Users\ss\Desktop\Test Folder" ' [COLOR="Red"]help here this is the folder i want to check if the file the user picks is already one named the same if so message sorry name in use exit sub[/COLOR]

   ' Set up the File Dialog. '
   Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    fd.InitialFileName = Application.CurrentProject.Path
   With fDialog

      ' Allow user to make multiple selections in dialog box '
      .AllowMultiSelect = False

      ' Set the title of the dialog box. '
      .Title = "Please select a Image"

      ' Clear out the current filters, and add our own.'
      .Filters.Clear
      .Filters.add "All Files", "*.*"

      ' Show the dialog box. If the .Show method returns True, the '
      ' user picked at least one file. If the .Show method returns '
      ' False, the user clicked Cancel. '
      If .Show = True Then
      
      
      
      
      
      
      
      
      ' This section takes the selected image and copy's it to the generated path'
      ' the string takes the file location, navigates to the image folder, uses the combo box selection to decide the file category, then uses the name from the filedialog to finish the path'
     FileCopy .SelectedItems(1), path1 & "\" & Dir(Trim(.SelectedItems.Item(1)))
 Me.Cert.Value = path1 & "\" & Dir(Trim(.SelectedItems.Item(1)))
 Me.Last_Scan_Date = Now()
 

 
 Else

End If

'ADD this line
Cancel = True
      End With
End Sub

thanks in advance

Shane
 
usage:
if FileExists(sMyFile) then


Code:
Public Function FileExists(ByVal pvFile) As Boolean
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
FileExists = FSO.FileExists(pvFile)
Set FSO = Nothing
End Function
 
sorry can't see how that will help me. i'm just check if a file exists not the file i selected.

E.G
i select file from the picker (the path could be anywhere the user likes)

i need to check the .selected (1) item is not present in the file directory path1 = "C:\Users\ss\Desktop\Test Folder" if is present please rename file exit sub

using your method how due get it to work
Code:
if FileExists(.selected (1))[COLOR="Red"] then it only check the selected path not the destination path1= C:\Users\ss\Desktop\Tes[/COLOR]t Folder"

thanks
shane
 
you cant pick it , if it doesnt exist.

finally got it lol!!!!! abit of a slow moment
Code:
         If FileExists(path1 & "\" & Dir(Trim(.SelectedItems.Item(1)))) Then
         MsgBox "already exists"
         Exit Sub
         End If
         
     
     FileCopy .SelectedItems(1), path1 & "\" & Dir(Trim(.SelectedItems.Item(1)))
 Me.Cert.Value = path1 & "\" & Dir(Trim(.SelectedItems.Item(1)))
 Me.Last_Scan_Date = Now()
 
inside your code, test first
if there is actual selection made
or the user just cancel it

Code:
...
...
If .Show = True Then
	'check here, if not blank continue with copy
	Dim sFileName As String
	sFileName = FileNameOnly(.SelectedItems(1)) & ""
	If (.SelectedItems(1)) <> "" Then
		If Dir(path1 & "\" & sFileName) <> "" Then
			MsgBox sFileName & " already exists in " & path1 & "." & vbCrLf & _
				"Please rename the file first."
		Else
			FileCopy (.SelectedItems(1)), path1 & "\" & sFileName
			MsgBox "1 file copied"
		End If
	End if
End If
End Sub


Public Function FileNameOnly(strPathFile As String) As String
    FileNameOnly = strPathFile
    If InStr(strPathFile, "\") <> 0 Then
        FileNameOnly = Mid(strPathFile, InStrRev(strPathFile, "\") + 1)
    End If
End Function
 

Users who are viewing this thread

Back
Top Bottom