Question

How to download a file from a URL file type document

  • 28 August 2023
  • 4 replies
  • 233 views

Badge +1

Hello All,

In one customer I have a requirement to download file from URL file type document.

 

For example:

 

If I copy these three documents it will download the URL paths to a specific folder.

Here, however, the customer wants to download the files from a directory, not a URL.

What's the best way to handle this?

Appreciate any guidance please.

 

Thanks in advanced.


4 replies

Userlevel 6
Badge +14

Hi @Supeshala 

 

If you need a one click solution, then writing a macro for the Copy file to command is the way to go. I have tried writing a simple script but however there were few problems.

  1. COPY_TO_PATH client script value did not work for some reason. This could be a bug. Therefor I had to hardcode the folder location. This is not ideal for your scenario. But it is a good starting point to think of alternatives.
  2. There seems to be a timing issue in the macro where script runs before the file is actually downloaded, making the script useless. Again, this could be a bug. As a workaround I select all the files and copy to a folder 1st time, and then select one file and copy again. Then macro ran again and since now the files exists in the folder, it downloaded the files. Again, not an ideal one click solution.
  3. Macros are not supposed to be running when there are multiple records selected. But in my case it ran probably because all of my documents were in the same class. You can test with this.

Below is the script I used. 

PS- I have asked ChatGPT to generate this and it did a really good job. Obviously, I had to do some changes.

'created by AMSALK with the help from ChatGPT
Public Sub Download_files()
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")

' Specify the path to the folder containing .url files
FolderPath = "C:\Users\amsalk\OneDrive - IFS\Desktop\URL file\download"

'FolderPath = "Copy Path - " & ClientScriptValues.Item("COPY_TO_PATH").Value 'This does not work in IFS cloud
'nCopyFileCount = ClientScriptValues.Item("COPY_TO_FILE_COUNT").Value 'This does not work too in IFS cloud

' Get a reference to the folder
Set objFolder = objFSO.GetFolder(FolderPath)

' Loop through .url files in the folder
For Each objFile In objFolder.Files

If LCase(objFSO.GetExtensionName(objFile.Name)) = "url" Then
' Read the .url file
Set objTextStream = objFSO.OpenTextFile(objFile.Path)
strURL = objTextStream.ReadLine
strURL = objTextStream.ReadLine 'This is called twice since actual URL is mentioned in the second line of the .url file

objTextStream.Close

' Extract the URL from the .url file
If InStr(strURL, "URL=") = 1 Then
strURL = Mid(strURL, 5)
End If

' Download the file
' Get the filename from the URL
pdfFilename = objFSO.GetFileName(strURL)

' Construct the local path for the PDF file
pdfLocalPath = objFSO.BuildPath(objFolder.Path, pdfFilename)

' Download the PDF using PowerShell
downloadCommand = "powershell -Command (New-Object System.Net.WebClient).DownloadFile('" & strURL & "', '" & pdfLocalPath & "')"
objShell.Run downloadCommand, 0, True
End If
Next

MsgBox "Download completed!"

End Sub

 

Userlevel 4
Badge +11

I just smile. T h x, Amila :)

Userlevel 7
Badge +30

@Amila Samarasinghe If you think there is a timing issue when running macros, you should report it. The support for macros for the Copy File To… command has probably not been tried much, so there might be problems with it in some cases. Or it broke because of … things… :)

Badge +1

Thank you very much Amila for the great ideas.

Reply