简体   繁体   中英

Update a batch file based on the last modified timestamp on a file in a network share

I have a batch file on my network share, say \\Sids\\Awesome\\Network\\Share.bat. I have copies of this bat file on multiple computers. I want to set up the file in such a way that I can update Share.bat on my network share, and when I run Share.bat on a local machine, it can check the last modified time for the copy in the network share and update it self. Any suggestions on how to do this?

All my search results have led to doing some sort of directory listing and then picking the file. Unfortunately, this is not possible on a network share. For now, I have a work around by writing a small app in C# that is called from within the script. If the app believes an update it necessary, it exits with an error and I update the script if the app exited with an error code. I'd like to remove this dependency.

Hardcode a version number (second line here). Get the server version and the local version and compare them. If the version numbers are different, copy the file from the server and restart it.

@echo off
REM version=5.1
for /f "tokens=2 delims==" %%a in ('findstr /b /c:"REM version=" "\\Sids\Awesome\Network\%~nx0"') do set sv=%%a
for /f "tokens=2 delims==" %%a in ('findstr /b /c:"REM version=" "%~f0"') do set lv=%%a
echo Server: %sv%
echo Local:  %lv%
if "%sv%" == "%lv%" goto :continue
echo updating...
(xcopy /y "\\Sids\Awesome\Network\%~nx0" >nul || goto :eof) & "%~f0"
[should never arive at this line]
:continue
echo current version.

you can omit the >nul , if you want to see the "1 file(s) copied" message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM