Using VBA at Excel, I save data of a table at a ".dat" file, at a network folder.
How can check if the folder is available?
My idea would be if folder A is not available, save the file at folder B
Something like
if {{ "\\PC2\Shared" is not available }} then then nA = "D:\data.dat" else nA = "\\PC2\Shared\data.dat"
You can use the FileSystemObject to check if a folder exists.
Const FolderA As String = "\\Some path"
Const FolderB As String = "\\Some path"
Dim folderPath As String
With CreateObject("Scripting.FileSystemObject")
If .FolderExists(FolderA) Then folderPath = FolderA Else folderPath = FolderB
End With
'use folderPath to append the file name and save
Related
I have a series of excel spreadsheets that need to change their filename and save to an alternate path.I have run into a wall trying to attempt this on my own.
The original filename: A0001_lp_profile.csv (the A000# will not always be A0001)
The new filename: A0001.RecentlyOpenedFiles.LNK.xlsx
The first five characters will not change during the rename.
The original path: E:\Backup\VSNMM-01691\Cases\VSNMM-01691.A0001\Exports_fileFolderOpening
The new path: E:\Backup\VSNMM-01691\Reports
VSNMM-01691 will not always be in the path and changes quite often to something in a similar format. The "E:\Backup\" will always be the beginning of the path.
This should work, just make sure that i got your paths and filename patterns right:
Dim file As Variant
Dim wb As Workbook
Dim originalpath As String, newpath As String
originalpath = "E:\Backup\VSNMM-01691\Cases\VSNMM-01691.A0001\Exports_fileFolderOpening\"
newpath = "E:\Backup\VSNMM-01691\Reports\"
file = Dir(originalpath)
'loop through files in original folder
While (file <> "")
'match filename with pattern (# means one number, so 4 single numbers) change this if i did it wrong
If file Like "A####_lp_profile.csv" Then
'open the csv file
Set wb = Workbooks.Open(originalpath & file)
'save in new path as xlsx (without the Fileformat, the file will be unreadable
wb.SaveAs newpath & Left(file, 5) & ".RecentlyOpenedFiles.LNK.xlsx", ThisWorkbook.FileFormat
'close workbook in original path, discard changes
wb.Close False
'clear variable
Set wb = Nothing
End If
'next file
file = Dir
Wend
I need to figure out how to put a code in MS Access vba for attaching all the files in a selected folder.
Right now I can do just one from a specific location:
PathLocation = "C:\Test\test.PDF"
If Not IsNull(PathLocation) Then
txtAttach = PathLocation
Set objAttachment = objMailDocument.CREATERICHTEXTITEM("strFileAttachment")
Set objEmbedObject = objAttachment.EMBEDOBJECT(1454, "", txtAttach, "strFileAttachment")
End If
But what I really want is to collect all the stuff from the Test folder.
You need the Dir- statement to run through all files in the directory:
Dim PathLocation As String
Dim fileName As String
Dim filePath as String
PathLocation = "C:\Test\"
If Not IsNull(PathLocation) Then
Set objAttachment = objMailDocument.CREATERICHTEXTITEM("strFileAttachment")
fileName = Dir$(PathLocation & "*.*", 0)
Do While fileName <> ""
filePath = PathLocation & fileName
Set objEmbedObject = objAttachment.EMBEDOBJECT(1454, "", filePath, "")
fileName = Dir$()
Loop
End If
To run through e.g. just PDFs replace the *.* in the code above by *.pdf
Hope this should help,there is an example attached at the end as well.
http://bytes.com/topic/access/insights/916710-select-file-folder-using-filedialog-object
I can't find how to search for a folder (directory) that contains a string and copy all the folders (directory) that contain that string to another directory. So you search for the name of the directory, not the files in the directory.
For example: 'KTNPRK' in E:\ gives:
E:\KTNPRK1, E:\AZNKTNPR76, etc...
Here is an example of how to move the directories:
Dim sSourcePath As String
Dim sDestPath As String
Dim sTextToFind As String
sSourcePath = "D:\Temp"
sDestPath = "D:\Temp1"
sTextToFind = "test"
For Each sDir In Directory.GetDirectories(sSourcePath, "*" & sTextToFind & "*")
Directory.Move(sDir, Path.Combine(sDestPath, Path.GetFileName(sDir)))
Next
In order to copy all of the files in the folders, the loop can be changed to:
Dim sFullDestDir As String
sFullDestDir = Path.Combine(sDestPath, IO.Path.GetFileName(sFullSourceDir))
' Create the directory if it doesn't exist
If Not Directory.Exists(sFullDestDir) Then
Directory.CreateDirectory(sFullDestDir)
End If
' Copy the files in the directory
' If subfolders are to be copied, this logic can be turned into a recursive method.
For Each sFileName As String In Directory.GetFiles(sFullSourceDir)
File.Copy(sFileName, Path.Combine(sFullDestDir, IO.Path.GetFileName(sFileName)))
Next
I am trying to copy some files to folder. I am using the following statement to check if the source fie exists
If My.Computer.FileSystem.FileExists(fileToCopy) Then
But I donot know how to check if file exists in the folder before copying.
Please advise.
Thanks and best regards,
Furqan
Dim SourcePath As String = "c:\SomeFolder\SomeFileYouWantToCopy.txt" 'This is just an example string and could be anything, it maps to fileToCopy in your code.
Dim SaveDirectory As string = "c:\DestinationFolder"
Dim Filename As String = System.IO.Path.GetFileName(SourcePath) 'get the filename of the original file without the directory on it
Dim SavePath As String = System.IO.Path.Combine(SaveDirectory, Filename) 'combines the saveDirectory and the filename to get a fully qualified path.
If System.IO.File.Exists(SavePath) Then
'The file exists
Else
'the file doesn't exist
End If
'In Visual Basic
Dim FileName = "newfile.xml" ' The Name of file with its Extension Example A.txt or A.xml
Dim FilePath ="C:\MyFolderName" & "\" & FileName 'First Name of Directory and Then Name of Folder if it exists and then attach the name of file you want to search.
If System.IO.File.Exists(FilePath) Then
MsgBox("The file exists")
Else
MsgBox("the file doesn't exist")
End If
I have a folder that contains a lot of pictures. I need to copy the pictures in that folder based on the input by the user and copy it into a new folder:
User enters input.
The code needs to search for the pictures in the folder based on the input.
If found, the pictures is than move to a new folder/another folder.
How do I do this?
This is an example of how to do it. I don't know what your "user input" is, so I just made an assumption. Rectify as appropriate.
Sub CopySomeFiles()
Dim FSO, sourceFolder, currentFile, filesInSourceFolder
Dim strSourceFolderPath As String
Dim strDestinationFolderPath As String
Dim strUserInput As String
Set FSO = CreateObject("Scripting.FileSystemObject")
' Figure out which file to copy from where to where
strUserInput = InputBox("Please enter name of file to copy.")
strSourceFolderPath = "C:\MySourceFolder"
strDestinationFolderPath = "C:\MyDestinationFolder"
Set sourceFolder = FSO.GetFolder(strSourceFolderPath)
Set filesInSourceFolder = sourceFolder.Files
' Look at all files in source folder. If name matches,
' copy to destination folder.
For Each currentFile In filesInSourceFolder
If currentFile.Name = strUserInput Then
currentFile.Copy (FSO.BuildPath(strDestinationFolderPath, _
currentFile.Name))
End If
Next
End Sub