0

I'm working in Windows 10 Professional.

I have a batch file (code below) that checks whether files on the local machine are older than some files on a file server and updates them if necessary by pulling down a zip file and expanding it.

There are four states the user can end up in: • There are no files on the server to deploy locally • There is no zip file on the server to copy • The local files are already current • The local files need to be updated

For each of these states, I'd like a message box telling the user what state they're in, especially because the file copy can take ten minutes.

All of this works perfectly if I execute the batch file from the command line. However if I double-click the batch file from File Explorer, the message boxes are children of the batch process and are killed as soon as the batch script ends — which is often immediately after the message box is invoked. (At the end of the file copy, I'd like to notify the user that the files are in place.)

The batch script below uses msg to try to invoke the message box, and (as mentioned) that works perfectly well if I invoke the script from a command line but fails if I invoke the script from File Explorer. I've also tried creating a vbscript message box and invoking that with either start /WAIT or call, and I've tried mshta javascripting, msg, and pretty much anything else I could find, (the options detailed in Show a popup/message box from a Windows batch file) but the message box always disappears as soon as the script ends, which is immediately.

How can I invoke a message box that is not going to disappear immediately when the script has been invoked from File Explorer?

Thanks!

@echo off
set never=Never
set null=0
:: First, see if there is an environment available online.
IF EXIST "\\FileServer\Client\Application\JRE\release" (
    :: The environment exists. When was it deployed?
    FOR %%A IN ("\\FileServer\Client\Application\JRE\release") do (
        SET tempDateTimeS=%%~tA
        set lastServerUpdate=%tempDateTimeS:~0,10%.%tempDateTimeS:~11,5%
    )
) ELSE (
    set lastServerUpdate=%never%
)
:: Next, see if there is a local environment in place.
IF EXIST "C:\SL\Application\JRE\release" (
    :: The environment exists. when was it deployed on the server?
    FOR %%B IN (C:\SL\Application\JRE\release) DO (
        set tempDateTimeL=%%~tB
        set lastLocalUpdate=%tempDateTimeL:~0,10%.%tempDateTimeL:~11,5%
    )
) ELSE (
    set lastLocalUpdate=%never%
)
:: Finally, see when the last zip file was made on the server.
IF EXIST "\\FileServer\Client\Application.zip" (
    :: Get the update date of the zip file. Is it newer than the deployment?
    FOR %%C IN ("\\FileServer\Client\Application.zip") DO (
        set tempDateTimeZ=%%~tC
        set zipUpdateTime=%tempDateTimeZ:~0,10%.%tempDateTimeZ:~11,5%
    )
) ELSE (
    set zipUpdateTime=%null%
)

IF %lastServerUpdate% == %never% (
    :: There is no code deployed on the server
    msg * There is no deployment available on the server.
) ELSE IF %lastServerUpdate% GTR %zipUpdateTime% (
    :: The zip file on the file server needs to be rebuilt.
    msg * Create a new ZIP file on the server and rerun this batch file.
) ELSE IF %lastServerUpdate% == %lastLocalUpdate% (
    :: The local version matches the newest available on the server.
    msg * The local environment is already current.   
) ELSE (
    msg * Starting local deployment.
    :: Get the zip file from the server.
    xcopy "\\FileServer\Client\Application.zip" "C:\SL" /Y
    :: Unzip it locally.
    "C:\Program Files\7-Zip\7z" x application.zip -o"C:\SL" -aoa
    :: Rename the downloaded zip file to keep it available for future restoration
    ren "C:\SL\Application.zip" "%date%-Application.zip"
    msg * The local environment is ready.
)
BarryTice
  • 11
  • 2
  • Do not use inalid-label-style comments (`::`) within parenthesised code block as they lead to unexpected behaviour; use the `rem` command instead… – aschipfl Jun 04 '20 at 21:44
  • Use `msg * /w "test"` to wait for the message to be closed before continuing commands. Try `msg * test` and `msg * /w test` in the command prompt to see if the prompt will show on either to experiment with this. – Nico Nekoru Jun 04 '20 at 22:32
  • How likely is it that your specific, or more to the point those of any end user, `%lastServerUpdate%` and `%zipUpdateTime%` variable values, will be formatted such that they can be compared using `GTR`? I would suggest that you find another way to determine which if any is the more up to date release/version. In addition to that, even if you decided that it will work like that, those variables as well as `%lastLocalUpdate%` would require the use of delayed expansion anyhow. – Compo Jun 04 '20 at 22:52
  • 2
    BarryTice, as you never used the correct method of updating your [previously closed question](https://stackoverflow.com/q/62196443), _(on exactly the same subject)_, can you please delete it instead. Thank you. – Compo Jun 04 '20 at 23:01
  • Thank you for your inputs! aschipfl, I was unaware of the issue. I will use REM in the future — a nice throwback to Applesoft on my Apple ][+. Neko Musume, if I run my script from a command line, I see a visible difference in the output as it waits for the message prompt to close before continuing. However if I launch my script by double-clicking it, there is no difference in how things respond. My command prompt window closes before I ever see the message prompt. – BarryTice Jun 05 '20 at 04:57
  • Compo, my variables are combing back as yyyy-mm-dd, which is perfect for comparisons. If there is some likelihood that someone else's desktop will return this differently, then I'll rework this. In fact, the more I'm having to mess with this, the more I'm thinking I'll just redo the whole thing in Python and be done with it. This is probably the straw that sends me over that line. :-) – BarryTice Jun 05 '20 at 05:02
  • Compo, the message I received in my previously closed question said, and I quote, "You can edit the question or post a new one." I chose the second option, because I had been told it was a legitimate option. If the only "correct method" is to edit the previous question, then it seems users shouldn't be told that a second option is also valid. – BarryTice Jun 05 '20 at 05:04
  • @BarryTice, the first option, namely editing, is the [preferable one](https://meta.stackoverflow.com/q/255955), because it avoids having an inactive/closed question that serves no purpose (note that "deletion" actually just hides a post, but it is still there, so it just occupies storage for nothing)… – aschipfl Jun 05 '20 at 10:56
  • @BarryTice, "my variables are combing back as yyyy-mm-dd", is not quite true is it? there's a time in there too. My question regarding that is that I do not know if it will always be returned in a specific sortable format. If for instance it retuned as `05/06/2020 12:40 PM` your code would make that `05/06/2020.12:40`, so if the other file was `05/06/2020 6:18 AM` it would then be `05/06/2020.6:18 `. I'm going to assume that the alphabetic result of that comparison would make the 12 PM file newer than the 6 AM file! BTW, what happens when `%DATE%` contains an invalid character for a filename? – Compo Jun 05 '20 at 11:40

0 Answers0