VBA comparing files before moving to another folder - excel

I have been trying to use a code that ensures that there is a match from the input and output folders before moving input files into another folder. Here is a snippet:
'Select first Excel file within that Outlook_ImportedClaimsFiles folder path
OutlookImportFN = Dir(OutlookImportPath & "\*.xls")
'Select first Excel file within the SAS_Outputs folder path
SASOutputFN = Dir(SASOutputsPath & "\*.xls")
'Cycle through all files in the source folder path until there are no more left to cycle through
Do While OutlookImportFN <> ""
Counter = Counter + 1
'Create full path name of the source file
sFilePathName = OutlookImportPath & "\" & OutlookImportFN
'Create full path name of the destination file and add the date and time the file was moved
dFilePathName = OutlookRunPath & "\" & Format(Now, "yyyymmdd") & "_" & OutlookImportFN
The problem is that in the first folder (OutlookImportFN), there is a batch/holding file that contains the filenames for all the files in that folder that is used in the overall process. When flagging the first file, this file comes up first (this file shouldn't be flagged/selected at all). Because of this, when doing the comparison, the files don't match (unless I start with the 2nd file in the list). How do I either start with the 2nd file in the Input folder when comparing to the 1st file in the Output folder, or skip over this holding file in the Input folder when doing the comparison? I've tried a few things but nothing seems to work. Thanks in advance for your insight!

You need to add an If statement which makes sure the file isn't that batch file:
Do While OutlookImportFN <> ""
Counter = Counter + 1
If Not OutlookImportFN = "MyBatchFile" Then
'Create full path name of the source file
sFilePathName = OutlookImportPath & "\" & OutlookImportFN
'Create full path name of the destination file and add the date and time the file was moved
dFilePathName = OutlookRunPath & "\" & Format(Now, "yyyymmdd") & "_" & OutlookImportFN
End If

Related

To create automatic datewise folder and move the file in it

I am stuck in a situation where I would like to create an automatic date-wise folder on the FTP account. Also, it requires moving one to two files in an updated folder every day. i.e. If it is 28-Dec-22 then the automatic folder with the mentioned format below should be created and the file then should be moved to the updated date folder.
the format of the date will be like this
2023/01/01
This means on the FTP and on the root directory there will be a year folder then in the year folder there will be a month folder and in the month folder, there will be date-wise folders (month and date will folder will be two digits only.
At the start of next month, it requires creating an automatic month folder (within the year folder) and then the normal date folders will be created. that will be 2023/02/01 and so on.
I am using the code mentioned below to move the file from source to destination and it also creates the date-wise folder but in this code, it can only send the file to one selected destination and this cannot be moved into the updated date-wise folder.
Sub moveAllFilesInDateFolderIfNotExist()
Dim DateFold As String, fileName As String, objFSO As Object
Const sFolderPath As String = "E:\Uploading\Source"
Const dFolderPath As String = "E:\Uploading\Destination"
DateFold = dFolderPath & "\" & Format(Date, "ddmmyyyy") ' create the folder if it does not exist
If Dir(DateFold, vbDirectory) = "" Then MkDir DateFold
fileName = Dir(sFolderPath & "\*.*")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Do While fileName <> ""
If Not objFSO.FileExists(DateFold & "\" & fileName) Then
Name sFolderPath & "\" & fileName As DateFold & "\" & fileName
End If
fileName = Dir
Loop
End Sub
Can anyone please help me in this situation?

Searching file names from list optimisation

I'm currently trying to find files in a folder and move them into their seperate destination folders. I have a column of FILENAME next to a column of the respective FOLDER. The files are names as FILENAME_Ver_X and there should be 2 of each in the folder of different file types.
At the moment my code goes through each file, reads if it contains the FILENAME string and if so moves it to the folder. However this is quite slow as there is a massive number of files so I was wondering if there was a better way to search through it all.
Set objFolder = fso.GetFolder(Viewables)
Do While r <= maxRows
d = Rng(r, 1) & "_Ver_"
e = Rng(r, 2)
For Each f1 In objFolder.Files
If InStr(1, Left(f1.Name, Len(d)), d) > 0 Then
tPath = ActiveWorkbook.Path & "\" & e & "\"
fso.MoveFile (Viewables & f1.Name), tPath
End If
Next
r = r + 1
Loop
Maybe you just use vba to cycle through folder but then use windows level move command once per folder
The move command would allow wild cards so the vba could supply the original path and the move command logic
So you end up running the move command once per folder
You could get log the path and count of files in path before and after for auditing

Len() as argument for Dir()

So I am looping through a folder and have set a few arguments to filter the loop. I am trying to do so for file length as well but I am getting a run time error 13 type mismatch. Guessing there's no way to use file length as an argument for Dir()? What's a good workaround?
path = subF & "\"
extension = "*.xlsm"
partialName = "C*"
file = Dir(path & partialName & Len(file) = 13 & extension) '<- error
I can do
length = Len(file)
If length = 13 Then
inside the loop but without going into details, I need to do so outside of the loop.
Neither this or this is helping. And SO seems to only discuss file length with python not excel :/
You want filenames that are a total of 13 characters long to include a period and a four letter file extension, so you really want filenames that are 8 characters long and starts with a C. The file mask you are currently using involved the asterisk which is the wildcard character for any number of characters. Switch to seven (7) question marks after the C and you should be filtering your Dir function for file names that are eight characters long starting with a C with an .xlsm file extension.
path = subF & "\"
extension = ".xlsm"
partialName = "C???????"
file = Dir(path & partialName & extension) '\C???????.xlsm
Remove the * wildcards and use the '?' wildcard instead:
path = subF & "\"
partialName = "C"
extension = ".xlsm"
file = Dir(path & partialName & String(7, "?") & extension)
If you want 13 char between "C" and ".xlsm" use string(13, "?") instead.
Edit in response to comment:
Minimizing variables -
path = subF & "\C" & String(7, "?") & ".xlsm"
file = Dir(path)
or
file = Dir(subF & "\C???????.xlsm")

Search Multiple file extension

I have a vba syntax which search for only pdf files.I wanted to know what changes do I need to make in the below code so that it can search docx and similar ext files as well.
If pfile <> "" And Right(pfile, 3) = "pdf" Then
Set obMail = Outlook.CreateItem(olMailItem)
First change your Dir command to look for folder & "\*.*. While you've neglected to incude that important postion of your code, a Dir list isn't going to return anything that is outside of its file mask. Next, pull the file extension off the right hand end and compare it to a list of desired file extensions.
dim folder as string, pfile as string, ext as string
folder = "c:\temp"
pfile = Dir(folder & "\*.*")
do while cbool(len(pfile))
ext = chr(32) & lcase(trim(right(replace(pfile, chr(46), space(99)), 99))) & chr(32)
if cbool(instr(1, " pdf docx doc xls xlsx ", ext, vbTextCompare)) then
'do something with the matching file
end if
pfile = Dir
loop

Why can't I get the workbook's directory?

I'm trying to find the current directory of the workbook, since the location will change periodicly. But, the returned path is in the temporary folder and not where the file is located. I've tried it both of these ways.
folderPath = Replace(ThisWorkbook.FullName, templateBook, "")
OR
folderPath = ThisWorkbook.path
They both work the first time I run the macro, but when I close the workbook and move it I get the same problem.
NOTE: I'm using excel 2007.
I used the following code:
folderPath = CurDir("M") & "\" & Month & " " & Year
MkDir (folderPath)
I think it was not working the other ways because there is protection on the shared drive (M).

Resources