Notifications
Clear all

[Closed] getFileModDate on a ftp file?

hey guys,

I’m using a function to download a file from a serveur to my computer like this (not the whole code):
while not reader.EndOfStream do
(
local textline = reader.ReadLine()
FTP_NameFile = (filterstring (textline as string) ” “)
append FTP_ListFile FTP_NameFile

webClient.downloadfile theUri theFile
)

it works pretty well. I can read the file in local to see it the name already exists and download the file if it doesn’t.
I’d like to check the modification date to be more precise. In local it’s pretty easy, we can use getFileModDate(), but does it work online with dotnet?

I need a little help here
Thanks!

7 Replies

probably FtpWebResponse.LastModified

thanks!
But could you help me use it? I’m no good at dotnet… sorry.

In case if you need to check modification date of multiple files in remote directory read this answer on stackoverflow

otherwise it is quite trivial to use .net from maxscript


--  https://stackoverflow.com/questions/27604772/how-to-get-the-last-modified-date-of-files-on-ftp-server 
-- Uri serverUri = new Uri("ftp://mypath/myfile.txt");
-- FtpWebRequest request = (FtpWebRequest)WebRequest.Create (serverUri);
-- request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
-- FtpWebResponse response = (FtpWebResponse)request.GetResponse ();
-- DateTime lastModifiedDate = response.LastModified;

url = "ftp://ftp.matrox.com/robots.txt"
uri = dotNetObject "system.uri" url
wr = (dotNetClass "System.Net.WebRequest").create uri
wr.method = (dotNetClass "System.Net.WebRequestMethods+Ftp").GetDateTimestamp

response = wr.GetResponse()
response.LastModified.ToString()

Thanks for your answer. I read the article but I cannot make it work… Here’s my code:

(
local Uri = dotnetObject “System.Uri” (“ftp://myserver.com/_DATA/”)
local req = (dotnetclass “system.net.webrequest”).create (Uri)
local FTP_NameFile = “”
local FTP_ListFile = #()

req.credentials = dotnetObject "System.Net.NetworkCredential" mylogin mypassword
req.timeout = 10000
req.method = "LIST"
req.UsePassive = true
req.KeepAlive = false

local response = req.getresponse()
local responseStream = response.GetResponseStream()
local reader = dotnetobject  "System.IO.StreamReader" responseStream

--get all the files in the folder
while not reader.EndOfStream do 
(
	--gives: "-rw-rw-r--   1 admin    osf        16839 Oct 13 08:59 image.jpg"
	local textline = reader.ReadLine()
		
	--only keep the name file (ex: image.jpg)
	FTP_NameFile = (filterstring (textline as string) " ")
	FTP_NameFile = FTP_NameFile[FTP_NameFile.count]
	
	--add the file to the array 
	append FTP_ListFile FTP_NameFile
)


	--try to print the last modified date of each file
	for i = 1 to FTP_ListFile.count do 
	(
		local NewUri = dotnetObject "System.Uri" ("ftp://myserver.com/_DATA/"+FTP_ListFile[i])	
		
		request = (dotnetclass "system.net.webrequest").create (NewUri)
		request.Method = (dotnetclass "system.net.webrequest").Ftp.GetDateTimestamp
		response = request.GetResponse()
		--print it
		print (response.LastModified) as string
	)

response.Close()
reader.close()

)

Thank you for your help

Note: I cannot parse the reader.ReadLine() because it doesn’t give me the year…

1 Reply
(@serejah)
Joined: 10 months ago

Posts: 0

maybe because of this?

(dotnetclass “system.net.webrequest”).Ftp.GetDateTimestamp
– Unknown property: “Ftp” in dotNetClass:System.Net.WebRequest

-- ftp://ftp.isc.org/pub/usenet/control/5col/

(
	local ftp = "ftp://ftp.isc.org/pub/usenet/control/5col/"
    local Uri = dotnetObject "System.Uri" ftp
    local req = (dotnetclass "system.net.webrequest").create (Uri)
    local FTP_NameFile = ""
    local FTP_ListFile = #()

-- 	req.credentials = dotnetObject "System.Net.NetworkCredential" mylogin mypassword
	req.timeout = 5000
	req.method = "LIST"
	req.UsePassive = true
	req.KeepAlive = false

	local response = req.getresponse()
	local responseStream = response.GetResponseStream()
	local reader = dotnetobject  "System.IO.StreamReader" responseStream

	--get all the files in the folder
	while not reader.EndOfStream do 
	(
		--gives: "-rw-rw-r--   1 admin    osf        16839 Oct 13 08:59 image.jpg"
		local textline = reader.ReadLine()
			
		--only keep the name file (ex: image.jpg)
		FTP_NameFile = (filterstring (textline as string) " ")
		FTP_NameFile = FTP_NameFile[FTP_NameFile.count]
		
		--add the file to the array 
		append FTP_ListFile FTP_NameFile
	)

	
	--try to print the last modified date of each file
	for file in FTP_ListFile do 
	(
		local NewUri = dotnetObject "System.Uri" (ftp + file)	
		
		request = (dotnetclass "system.net.webrequest").create NewUri
		request.Method = (dotNetClass "System.Net.WebRequestMethods+Ftp").GetDateTimestamp
		response = request.GetResponse()
		format "% : %\n" ((dotNetObject "system.string" file).padright 25) (response.LastModified.ToString())
	)
	
	response.Close()
	reader.close()
	
)
5col.announce.gz          : 20.03.1997 19:57:09
5col.forsale.gz           : 20.03.1997 19:57:11
5col.general.gz           : 20.04.1996 15:15:20
5col.gz                   : 23.08.1997 7:15:47
5col.org.hso.gz           : 11.04.1997 17:49:27
5col.stamps.gz            : 07.02.1998 22:49:22

Thank you but it doesn’t seem to work on my server; it throws me that error:

– Error occurred in anonymous codeblock; filename: ; position: 1467; line: 43
– Runtime error: .NET runtime exception: The remote server returned an error: (530) Not logged in.
– MAXScript callstack:
– thread data: threadID:19708

– [stack level: 0]
– In file loop; filename: ; position: 1357; line: 41
– member of: anonymous codeblock
– Parameters:
– file: “addtofav_HOVER.jpg”
– Locals:
– request: dotNetObject:System.Net.FtpWebRequest
– file: “addtofav_HOVER.jpg”
– NewUri: dotNetObject:System.Uri
– Externals:
– owner: CodeBlock:anonymous
– response: dotNetObject:System.Net.FtpWebResponse
– ftp: “ftp://myserver.com/_ICON/


The script gets all the files on the folder but once it’s in the for loop it crashes.
Note: I had to use this line btw

req.credentials = dotnetObject “System.Net.NetworkCredential” mylogin mypassword

Are you sending credentials in for loop?