I get a partial answer from one of your previous questions and answers(with an error) but not complete. The My.Computer.Network.DownloadFile(URi, destination filename, , , , , ) MSDN says that the next to last parameter is the timeout parameter, 1000 ms default, i.e., one second and then the last parameter is to indicate overwrite True or False. However there is no indication that you can use some parameter somewhere to overwrite the destination file. How can this be done?
You can also check the file existence before you execute the download.
If File.Exists("my_zip_file_location") Then
'delete the file'
File.Delete("my_zip_file_location")
'Download it again'
My.Computer.Network.DownloadFile("your_new_zip_file_location")
End If
Related
I am trying to copy the file from ftp to local and I am using the code below to do my file copy function and in doing so i am getting an error that files already exist is there any way that i can bypass this and if the file exist it just say do nothing otherwise do the file copy or overwrite the file copied.
Docopy = True
If Docopy Then
' I want some function here so he can tell that file exist
' Write now i am using Kill(CopyTo) but it only works once
My.Computer.Network.DownloadFile(CopyFrom, CopyTo, UserName, Password)
End If
There's some extra parameters you can use for that:
My.Computer.Network.DownloadFile(CopyFrom, CopyTo, UserName, Password, False, 100, True)
5th parameter shows a UI or not
6th parameter is a timeout parameter
7th parameter is an overwrite parameter
Reference: https://msdn.microsoft.com/en-us/library/ms127879(v=vs.110).aspx
You need to check if the file exists before you do the copy.
Docopy = True
If Docopy Then
' I want some function here so he can tell that file exist
If FileExists("ThisFilenameToTest.txt") = True Then Kill(CopyTo)
'copy the file
My.Computer.Network.DownloadFile(CopyFrom, CopyTo, UserName, Password)
End If
I have a file exists under this path:
//path/folder1/folder2/datafile.gdp
It is an input to an external program being called from vba in this manner:
Sub RunProgram()
Dim wsh As Object
SetCurrentDirectory "\\path\"
ChDir "\\path\folder1\folder2\" 'Set Directory
Set wsh = VBA.CreateObject("WScript.Shell")
check = CurDir
Statusnum = wsh.Run(Command:="program.exe ""\\path\folder1\folder2\datafile.gdp""", WindowStyle:=1, waitonreturn:=True)
But on the final line, including \path\folder1\folder2\ before the input file name appears to cause the external program to want to write some files into a duplicated directory that doesn't exist, causing an error. It used to work in this format before the .exe was updated by an external company. It now wants to write some files here, all prefixed with the name of my input file:
\\path\folder1\folder2\PATH\FOLDER1\FOLDER2\
Hoping to fix this, I changed the final line of the code to this, following some comments on a previous post here on SO:
Statusnum = wsh.Run(Command:="program.exe ""datafile.gdp""", WindowStyle:=1, waitonreturn:=True)
Since the directory is set correctly prior to calling the .exe, I thought removing the path for the input file would solve the issue.
The program now launches, but doesn't appear to load the input file with it and no longer runs calculations automatically in the background as it should. Instead, it launches and the main .exe window pops up to the user as if it had just been launched for setting up a new project, calculations don't occur automatically.
To check which directory the VBA code was try to pull my datafile.gdp from, I created these loops directly before calling the .exe:
If Len(Dir("\\path\folder1\folder2\datafile.gdp")) = 0 Then
FileIsMissing1 = True 'I use Excel-VBA watches to break if true
End If
If Len(Dir("datafile.gdp")) = 0 Then
FileIsMissing2 = True
End If
Bizarrely, neither of these loops causes a break. The file only exists in
\\path\folder1\folder2\datafile.gdp
Not in the duplicated directory... so why are both of these statements satisfied? Does entering the directory make no difference even when the current directory has been set? The current directory seems to be impacting the line:
Statusnum = wsh.Run(Command:="program.exe ""\\path\folder1\folder2\datafile.gdp""", WindowStyle:=1, waitonreturn:=True)
But not these if loops, and I'm not sure why.
I have a macro that reads out external file properties like date created. The file from where I read is stored on a server. Unfortunately the date returned is not the correct one when running the macro the first time. Only when I open the file or when I run the macro several times, the correct updated date created is returned.
Does anyone have an idea how to solve that issue except from opening the file or looping through until the date is correct?
Here is the code:
strFilename = "<FILENAME>"
Workbooks.Open strFilename
Workbooks("strFilename").Close
Set oFS = CreateObject("Scripting.FileSystemObject")
lastcreatedLTVfile = CDate(Format(oFS.GetFile(strFilename).DateCreated, "dd.mm.yyyy"))
Do you want DateCreated or do you actually want DateLastModified? In your question you say "correct updated date" so I guess you should be using DateLastModified.
I am using ASPUpload to do file uploads for a web form, the current form i am working on updates a record in the database so you have the option to change the filename, i have the following check
If Upload.Form("imageFilename-bound") <> "" And Upload.Files = null Then
skipUpload = true
End If
when the page is loaded, the filename in database is made to be the value of a read-only text box imageFilename-bound while the file input field imageFilename is used to select a file. so the Psudo Code of the above is
IF [our previous image name] is not empty AND [we have not received a
file to upload] THEN {We are not uploading a file}
now since there is the option that the user doesn't want to change the file and file upload has only recently been suggested (after the form was completed) i don't want to change to much so anything to do with file uploading (including validation of size, type etc) i have put in a If skipUpload = false Then condition.
If i remove the And Upload.Files = null part i don't get any errors however when updating and i do go to change the image it doesn't do it (as imageFilename-bound is not a blank string thus the if statement is correct) however when i add the And Upload.Files = null part i get a 500 error and thus i can't actully see the problem (i am forced to test on a machine which has no error logging)
So my question is, how do i check with ASPUpload if my file input field did receive a file?
i have got the page to work with this code
skipUpload = false
If Upload.Form("imageFilename-bound") <> "" Then
skipUpload = true
For Each File in Upload.Files
skipUpload = false
Next
End If
Psudo:
IF [our previous image name] is not empty THEN
{We are not uploading a file}
FOR EACH [Submitted file]
{we actually are going to be uploading}
this does what i want but i feel this is very inelegant, having to use a Loop to check if i have submitted any file. i am still looking for a better answer
I want to copy, rename, and move a file to a new location. According to http://msdn.microsoft.com/en-us/library/36xbexyf(v=vs.90).aspx the following code should do so:
This example copies the file Test.txt to the directory TestFiles2 and
renames it NewFile.txt.
My.Computer.FileSystem.CopyFile _
("C:\UserFiles\TestFiles\test.txt", _
"C:\UserFiles\TestFiles2", "NewFile.txt", FileIO.UICancelOption.DoNothing)
However, when I type this code it only sees the "NewFile.txt" parameter as the Boolean parameter that handles overwrites. I believe this is a mistake on the websites part.
If I can't use "My.Computer.FileSystem.CopyFile" (unless I am doing something wrong) to copy rename and move a file, is there a better way then this:
' Rename the file to be copied the name you want it to be in the new location
My.Computer.FileSystem.RenameFile("C:\OriginalFile.txt", "OriginalFileTemporaryName.txt")
' Copy the file to the new location and overwrite if it exists there
My.Computer.FileSystem.CopyFile ("C:\OriginalFileTemporaryName.txt", "C:\UserFiles\TestFiles2", True)
' Rename the original file back to it's original name
My.Computer.FileSystem.RenameFile("C:\OriginalFileTemporaryName.txt", "OriginalFile.txt")
The problem I have with the above is that I might end up renaming the original file a temporary name that already exists in the original files location. I want to avoid that. I also do not want to rename the copied file in the destination folder because I do not see an option to force an overwrite for "My.Computer.FileSystem.RenameFile"
Is this even a half decent solution?
Is there not a better way to do this?
I think you have syntax error. You are trying to give the target file name as two parameters - directory name and file name, when you should give both as one string.
My.Computer.FileSystem.CopyFile ("C:\UserFiles\TestFiles\test.txt", "C:\UserFiles\TestFiles2\NewFile.txt")
Like that. Third parameter could be boolean (True/False) - whether or not you wouldd like to overwrite the target file if it exists.
If you want to first check whether the file exists, take a look at System.IO.File class, it has "Exists" method, which accepts file name and returns boolean. It has also methods to manipulate files which you can use instead of FileSystem class, although there is no performance advantage.
It certainly looks like a mistake on the website. That shouldn't stop us, though; just put the directory and filename into the same parameter:
My.Computer.FileSystem.CopyFile("C:\UserFiles\TestFiles\test.txt", "C:\UserFiles\TestFiles2\NewFile.txt", FileIO.UICancelOption.DoNothing)
(If you want to force an overwrite, change the third parameter to True. I don't know that you can overwrite and pass CancelOption.DoNothing in the same call.)