Get the Last Modified Date of a Remote File

Get the last modified date of a remote file

From php's article:

// outputs e.g. somefile.txt was last modified: December 29 2002 22:16:23.

$filename = 'somefile.txt';
if (file_exists($filename)) {
echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));

filemtime() is the key here. But I'm not sure if you can get the last modified date of a remote file, since the server should send it to you... Maybe in the HTTP headers?

How can I fetch the last-modified value of a remote file?

You can use the System.Net.Http.HttpClient class to fetch the last modified date from the server. Because it's sending a HEAD request, it will not fetch the file contents:

Dim client = New HttpClient()
Dim msg = New HttpRequestMessage(HttpMethod.Head, "")
Dim resp = client.SendAsync(msg).Result
Dim lastMod = resp.Content.Headers.LastModified

You could also use the If-Modified-Since request header with a GET request. This way the response should be 304 - Not Modified if the file has not been changed (no file content sent), or 200 - OK if the file has been changed (and the contents of the file will be sent in the response), although the server is not required to honor this header.

Dim client = New HttpClient()
Dim msg = New HttpRequestMessage(HttpMethod.Get, "")
msg.Headers.IfModifiedSince = DateTimeOffset.UtcNow.AddDays(-1) ' use the date of your copy of the file
Dim resp = client.SendAsync(msg).Result
Select Case resp.StatusCode
Case HttpStatusCode.NotModified
' Your copy is up-to-date
Case HttpStatusCode.OK
' Your copy is out of date, so save it
File.WriteAllBytes("C:\robots.txt", resp.Content.ReadAsByteArrayAsync.Result)
End Select

Note the use of .Result, since I was testing in a console application - you should probably await instead.

Get last modified date of a remote file

Generally, you can check the file time by sending HEAD request and parsing/looking HTTP header response for a Last-Modified filed. The remote server should support it and DropBox does not support this feature for direct links (only via API). But DropBox have another feature, the headers have the etag field. You should store it and check in the next request. If it changed - the file has been changed too. You can use this tool to check the remote file headers.

How to get the modified time of a remote file in batch script

Try a dir.

Or use a for loop differently.

for %%A in (\\server\C$\Temp\LogFiles\abcd) do echo %%~tA

A working example if pasted into a command prompt.

for %A in (\\\C$\windows\win.ini) do echo %~tA

To the other question you sneeked in

more +50 skips first 50 lines.

This VBScript does what you want.

Set Arg = WScript.Arguments
Set WshShell = createObject("Wscript.Shell")
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
Set rs = CreateObject("ADODB.Recordset")
With rs
.Fields.Append "LineNumber", 4

.Fields.Append "Txt", 201, 5000
LineCount = 0
Do Until Inp.AtEndOfStream
LineCount = LineCount + 1
.Fields("LineNumber").value = LineCount
.Fields("Txt").value = Inp.readline

.Sort = "LineNumber ASC"

If LCase(Arg(1)) = "t" then
If LCase(Arg(2)) = "i" then
.filter = "LineNumber < " & LCase(Arg(3)) + 1
ElseIf LCase(Arg(2)) = "x" then
.filter = "LineNumber > " & LCase(Arg(3))
End If
ElseIf LCase(Arg(1)) = "b" then
If LCase(Arg(2)) = "i" then
.filter = "LineNumber > " & LineCount - LCase(Arg(3))
ElseIf LCase(Arg(2)) = "x" then
.filter = "LineNumber < " & LineCount - LCase(Arg(3)) + 1
End If
End If

Do While not .EOF
Outp.writeline .Fields("Txt").Value

End With

To use

Filter reads and writes standard in and standard out only. These are only available in a command prompt.

filter <inputfile >outputfile
filter <inputfile | other_command
other_command | filter >outputfile
other_command | filter | other_command


filter cut {t|b} {i|x} NumOfLines

Cuts the number of lines from the top or bottom of file.

t - top of the file
b - bottom of the file
i - include n lines
x - exclude n lines


filter cut t i 5 < "%systemroot%\win.ini"

How do you obtain modified date from a remote file (Java)?

Any decent webserver will put this information in the Last-Modified response header. You can obtain it by URLConnection#getLastModified(). Here's an example:

URLConnection connection = new URL("").openConnection();
long lastModified = connection.getLastModified();
System.out.println(lastModified); // 1649805134000

You can then easily convert it the desired date type. E.g. Instant:

Instant instant = Instant.ofEpochMilli(lastModified);
System.out.println(instant); // 2022-04-12T23:12:14Z

Or the legacy java.util.Date:

Date date = new Date(lastModified);
System.out.println(date); // Tue Apr 12 19:12:14 AST 2022

Batch Script Get Last Modified Date of Remote File


This has got to be the most common bug with Windows batch development. You are attempting to expand a variable that was set within the same code block. But the variable is expanded when the entire code block is parsed, so you are getting the value that existed prior to the block of code being executed. That obviously doesn't work.

Type HELP SET or SET /? from the command prompt and read the section dealing with Delayed Expansion. That shows you one way to solve the problem.

But in your case you don't need the variable at all, so you don't need delayed expansion. Simply use the FOR variable directly when you append to your bodyText:

@echo off

set mainDir=\\\c$
set txtFile=%mainDir%\tmp.txt
set txtFile2=%mainDir%\tmp2.txt
set "bodyText=^<p^>Hello,^<br /^>^<br /^>"

if exist %txtFile% (
for %%X in (%txtFile%) do set "bodyText=%bodyText%tmp.txt file updated as of %%~tX^<br /^>"
) else (
set "bodyText=%bodyText%Warning: Issues finding %txtFile%.^<br /^>"

if exist %txtFile2% (
for %%X in (%txtFile2%) do set "bodyText=%bodyText%tmp2.txt file updated as of %%~tX^<br /^>"
) else (
set "bodyText=%bodyText%Warning: Issues finding %txtFile2%.^<br /^>"

set "bodyText=%bodyText%^</p^>"

echo %bodyText% > %mainDir%\mylog.txt


There is a lot more room for simplification that should make the code easier to maintain. Since you are preparing an HTML file, there is no reason to worry about additional line breaks, so you don't have to put all of the text into one variable. You can use multiple ECHO statements. I would structure your code something like the following (untested, but the concepts are sound):

@echo off
set "mainDir=\\\c$"
set "br=^<br /^>"
set "p=^<p^>"
set "/p=^</p^>"
>"%mainDir%\mylog.txt" (
echo %p%Hello,%br%%br%"
for %%F in (
) do (
if exist %%F (
echo %%~nxF file updated as of %%~tF%br%"
) else (
echo Warning: Issues finding %%~nxF.%br%"
echo %/p%

How to check the modify time of a remote file

You can use the HTTP "HEAD" method to just get the file's headers.

var request = WebRequest.Create(uri);
request.Method = "HEAD";

Then you can extract the last modified date and check whether to download the file or not.

Just be aware that not all servers implement Last-modified properly.

Bash - How to compare two files last modified date, when they are remote?

Try this:

remote=$(ssh user@server "stat -c %Y /path/to/remote_file")
[[ -z "$remote" ]] && exit 1 # stop on error ($remote is empty)
local=$(stat -c %Y /path/to/local_file)

if [[ $remote -gt $local ]]; then
echo remote file is newer
echo local file is newer

Related Topics

Leave a reply