GetLastWriteTime not accurate while files existing - vb.net

I am trying to get the last modified time for files in a directory. I loop through the directory and print the modified date. The output shows out of 10 files (Did this on other folders too with different number of files). 10 files appeared in the command prompt. All of them printed 12/31/1600.
How could I fix it so that it would print the correct date?
Dim strFilepath = "C:\Test" 'Test folder contains 10 files for test
Dim File As System.IO.FileInfo() = directory.GetFiles()
Dim File1 As System.IO.FileInfo
Dim strLastModified As String
For Each File1 In File 'Loops the GetLastWriteTime
strLastModified = System.IO.File.GetLastWriteTime(strFilepath & File.ToString()).ToShortDateString()
Console.WriteLine(strLastModified)'Prints all 10 files but with the 12/31/1600 date
'Files do exist, code goes into file, it loops through it but wrong date.

Jim gave you already the reason why your date is wron with his link to the dup.
You concat strFilepath and File.ToString() incorrectly because you are missing a backslash \ between them and thus giving something like:
C:\TestYourFile.txt.
Additionally you are using the wrong variable in the For Each.
It should be File1 instead of File (Thanks #Mark).
Solution 1:
That´s the reason why there is the Path.Combine function.
So Change
strLastModified = System.IO.File.GetLastWriteTime(strFilepath & File.ToString()).ToShortDateString()
To
strLastModified = System.IO.File.GetLastWriteTime(Path.Combine(strFilepath, File1.ToString())).ToShortDateString()
Solution 2:
Like Mark commented you could just use the FullName property which makes it even easier:
strLastModified = System.IO.File.GetLastWriteTime(File1.FullName).ToShortDateString()

Related

Merge pdf files with VBA and Foxit

I use Foxit Phantompdf, the complete version and ACCESS.
In our program, we have to save multiple pdf files, some of them should be merged in single files when saved.
Here is the code I use;
Dim phApp As PhantomPDF.Application
Dim n1 As String
Dim n2 As String
n1 = "c:\Temp\F3769-190136-GROUPE OCÉAN.pdf"
n2 = "c:\Temp\f3769-190136-GROUPE OCÉAN- facture.pdf"
Set phApp = CreateObject("PhantomPDF.Application")
Dim phCreator As PhantomPDF.Creator
Set phCreator = phApp.Creator
***'Call phCreator.CombineFiles("c:\Temp\F3769-190136-GROUPE OCÉAN.pdf|c:\Temp\f3769-190136-GROUPE OCÉAN- facture.pdf", "c:\Temp\F3769-190136-GROUPE OCÉAN.pdf", COMBINE_ADD_CONTENTS)***
Call phCreator.CombineFiles("""c:\Temp\" & n1 & "|'" & n2 & """" & ", " & """c:\Temp\F3769-190136-GROUPE OCÉAN.pdf"""" &", COMBINE_ADD_CONTENTS)
phApp.Exit
When I try it with the complete files names (in bold) the code works perfectly.
However, when I try to use variables, I get a
"Argument not optional"
error.
Can somebody help me ?
Thanks
Your string definitions in call line is incorrect.
You have defined n1 and n2 with the c:\temp already, and in your string conversion you add this again. I do not know if this is the route cause to your issue.
Furthermore I do not know the actual needed syntax for this phcreator.combine()
But is it not possible using only:
call pHcreator.combine(n1 & "|" & n2, …
The 'Argument not option' might imply you should add another input to your pHcreator, I would guess it could have something to do with FOXIT's combine function page settings. Try adding a input variable integer at the end of the function maybe?
But the fact that it works when writing strings in clear text would maybe suggest that the string manipulations is not correct?
I'm not a vba professional, but interested in the outcome, working myself with Foxit and also want to combine with vba. I'm currently not using version 9 som I seem to be out of luck, and only upgrading it I know what I want to do is possible.
I tried it, but got the same error message. So I took advantage of the fact that the function works when the file names are in plain text. I copied the files to be merged in a temporary folder and rename them. The renamed files are the then used in the merge function. It works perfectly, but Foxit adds a table of content page, and I don't know, yet, how to remove it. Here is my solution:
Private Sub Command4_Click()
Dim addi As String 'file to be merged to main file
Dim princi As String 'main file
Dim phApp As PhantomPDF.Application
'A temporary folder, in this case c:\t2, should be present
'In this example c:\Temp is the working folder
addi = "c:\Temp\filetomerge.pdf" 'full path of file to be merged
princi = "c:\Temp\mainfile.pdf" 'full path of main file
'fadd,pdf and fmain.pdf are the temporay files used in Foxit's function
FileCopy addi, "c:\t2\fadd.pdf" 'temporary file to be merged in temporary folder
FileCopy princi, "c:\t2\fmain.pdf" 'temporary main file in temporary folder
'Merge action
Set phApp = CreateObject("PhantomPDF.Application")
Dim phCreator As PhantomPDF.Creator
Set phCreator = phApp.Creator
Call phCreator.CombineFiles("c:\t2\fmain.pdf|c:\t2\fadd.pdf", "c:\t2\fmain.pdf", COMBINE_ADD_CONTENTS)
phApp.Exit
'Save merged file in working folder under main file name
Kill princi
FileCopy "c:\t2\fmain.pdf", princi
'delete temporary files
Kill "c:\t2\fadd.pdf"
Kill "c:\t2\fmain.pdf"
End Sub

How to filter file extensions

I'm trying to separate files in a folder by extension, using the code below:
Dim file_list3 As String() = Directory.GetFiles(path, "*.xls")
Dim file_list4 As String() = Directory.GetFiles(path, "*.xlsm")
but all of the ".xls" files end up in list 4 along with the ".xlsm" files. How can I keep the subset of just ".xls" flies out of the list of ".xlsm" files?
Thanks.
I found the answer:
Dim file_list3 = Directory.GetFiles(path, "*.xls").Where(Function(item) item.ToLower().EndsWith(".xls"))
Dim file_list4 = Directory.GetFiles(path, "*.xlsm").Where(Function(item) item.ToLower().EndsWith(".xlsm"))
Thanks.
I finally managed to reproduce the behavior documented.
It seems that I have set this command on my working pc
fsutil 8dot3name set 0
this command disables the creation of the old 8.3 short filenames required at MS-DOS time.
With this configuration the command Directory.GetFiles(path, "*.xls") returns only the files that have exactly the extension specified and not other extensions that starts with XLS.
Applying the command
fsutil 8dot3name set 1
I expected to have the same result explained by the OP, but this is not the case with files already in the folder because they were created when the 8dot3name flag was disabled.
But a new file named test.xlsm started to appear between the files returned by the search pattern *.xls
So, to fix your problem you could use the fsutil command as explained in the article How to disable 8.3 file name creation on NTFS partitions and perhaps strip away the old 8.3 entries with the strip option or just use a slight variation of your code above
Dim all_files = Directory.EnumerateFiles(path, "*.xls")
Dim file_list3 = all_files.Where(Function(x) Path.GetExtension(x).ToLower = ".xls").ToList()
Dim file_list4 = all_files.Where(Function(x) Path.GetExtension(x).ToLower = ".xlsm").ToList()

Get folder name in specified directory?

There is a one folder located in a directory, lets say its in
C:\Users\User\AppData\Roaming\Mozilla\Firefox\Profiles\
it's different on everyuser, for example 1rituum9.default named folder.
Tried this without any luck, it messages empty.
Dim filepath As String = "%Appdata%\Mozilla\Firefox\Profiles\"
Dim fi As New IO.FileInfo(filepath)
MessageBox.Show(fi.Name)
What is the correct way to get the folder name in specified directory ?
You can try this:
Dim filepath As String = Environment.GetEnvironmentVariable("appdata") & "\Mozilla\Firefox\Profiles\"
Dim di As New IO.DirectoryInfo(filepath)
MessageBox.Show(di.GetDirectories()(0).Name)
if there are more than one dirs u might need something like:
For Each Dir As IO.DirectoryInfo In di.GetDirectories()
ListBox1.Items.Add(Dir)
Next
Edit: Fixed Code Line 1 - see comments

How to import part of a file name to a field

I have a file that i import into access 2007 and i was wondering if i can take part of that file name and put it into a field in access? For example here is one example of a file name:
"20140211_agent_statistics.csv"
I have done some research on this but cant seem to find the answer when numbers change all of the time. I just need to grab the numbers on this file name. However, these numbers change all of the time. Does anyone have a solution for this? Thank you in advance. Any help and code is much appreciated i am very new to vba.
Working on a few assumptions:
You are importing this by code so it picks up the file name?
The numbers are the date so probably always 8 characters long?
If you import by code you will assign the file name to a variable, in case you don't here is how to:
Dim strFileO as String, strFileLoc as String
strFileLoc = "C:\YourFolder\" ' Folder where file is saved
strFileO = Dir(strFileLoc & "*.csv")
Above will pick up any .csv file in the folder, you should move them once imported
Once you have the strFileo then to get the date:
Dim lDate as Long
lDate = Left(strFileO,8)
'Or if the numbers aren't always 8 characters:
lDate = Left(strFileO. InStr(strFileO,"_") - 1) ' Assumes numbers followed by "_"

Visual Basic: File is said to be in the wrong folder

Ok, here is my story:
I am building a fileviewer, and i am trying to delete the selected file in the listview.
when i try to delete it, it gave me an error saying the file wasnt found. I looked at my desktop and the file was there. here is the original code:
dim f as string = lv1.focuseditem.text
my.computer.filesystem.deletfile(f)
lv1.update()
this gave me that error. My updated code is supposed to show me where the computer thinks my file is:
Dim file As String = lv1.FocusedItem.Text
Dim testFile As System.IO.FileInfo
testFile = My.Computer.FileSystem.GetFileInfo(file)
Dim folderPath As String = testFile.DirectoryName
MsgBox(folderPath)
this shows a messagebox that shows the path of:
C:\Users\tgs266\Desktop\SIOS\SIOS\SIOS\obj\Debug\test.txt
but the real file location is:
C:\Users\tgs266\Desktop\test.txt
please help
How are you getting the filenames for the ListView? Is it just the filename and no path?
If, for example, lv1.FocusedItem.Text is "test.txt", and that is the value you use (without the path), by default the program will look in the directory it's executing in. This is most likely why you're seeing C:\Users\tgs266\Desktop\SIOS\SIOS\SIOS\obj\Debug\test.txt as the location, instead of what you expected.
If all the files are on your desktop, you can use Environment.GetFolderPath in conjunction with the Environment.SpecialFolder Enumeration to get the file, like this:
Dim file As String = lv1.FocusedItem.Text
Dim testFile As System.IO.FileInfo
testFile = My.Computer.FileSystem.GetFileInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\" + file)
Dim folderPath As String = testFile.DirectoryName
MsgBox(folderPath)
However, if you're going to have files scattered throughout your system, you'd be better off storing the full path as #Plutonix indicates in his comment.
It looks like your code is looking in your applications path on the server while you want to look at the users desktop location.