[Windows] 比對檔案時間並拷貝較新檔案的命令行作法
依用Certbot產生SSL憑證的操作步驟並使用dns01的方法取得萬用憑證(wildcard),產生的憑證檔適用於「*.your-domain.com」,其他子域名主機必須去拷貝取得每三個更新一次的憑證檔。找了一下比對檔案時間並拷貝的方法,最終以下列批次檔處理,將批次檔加入排程後,就能自動拷貝新檔到需要的主機資料夾裡了。
批次檔
echo %DATE% %TIME% > c:\util\update-cert.log
net use x: \\certbot-server-IP\your-cert-folder /user:UserName Password >> c:\util\update-cert.log
xcopy /L /D /Y x:\cert.pem c:\certbot\live\your-domain.com\cert.pem |findstr /B /C:"1 " && copy x:\*.pem c:\certbot\live\your-domain.com /y >> c:\util\update-cert.log
重點說明
- xcopy 永遠回傳true,將其輸出再丟給findstr
- /L: Displays files that would be copied.
- /D:m-d-y Copies files changed on or after the specified date. If no date is given, copies only those files whose source time is newer than the destination time.
- /Y: 回應 Yes
- findstr
- /B: Matches pattern if at the beginning of a line.
- /C"1 ":
/C:string
Uses specified string as a literal search string. 若xcopy回傳1 File(s) copied
時會繼續執行&&
後面的命令
##
您可能也會有興趣的類似文章
- [Windows] 去除環境變數DATE與TIME內的空白 (0則留言, 2008/04/20)
- [Batch#1] Windows批次檔-取得系統日期的簡單方法與環境變數字串操作 (初學者的命令行 #5) (2則留言, 2019/12/15)
- [DOS] 以set指令的替換字串功能來解決資料夾重覆附加的小問題 (0則留言, 2016/03/07)
- 如何用批次指令取得上個月的月份-再修訂版 (12則留言, 2008/10/21)
- [Batch#2] 三個方便的Windows批次檔範例 (初學者的命令行 #6) (0則留言, 2019/12/26)
- [Windows] 刪除特定Java process的方法 (0則留言, 2017/08/02)
- [Batch #4] 用批次檔簡化複雜的指令—直接叫用特定的ms-settings (初學者的命令行#8) (0則留言, 2020/08/15)
- [Vista] 設定環境變數SET命令的擴充 (0則留言, 2008/04/19)
- 在命令提示字元取得目前資料夾路徑的方法—使用set /p|初學者的命令行#10 (0則留言, 2021/04/22)
- [Obs#114] 取代Obsidian核心外掛的替代性外掛:Better Commander、Another Quick Switcher、Alternate File Tree、Quiet Outline (0則留言, 2023/02/20)
- 替Prompt加上顏色 (1則留言, 2004/12/03)
- 開始使用Cygwin以取代cmd.exe (2則留言, 2007/01/11)
- 快速找出類別所在jar檔的命令行工具:LibraryFinder (0則留言, 2016/02/18)
- [Obs#113] Obsidian展示程式碼區塊的好工具:HK Code block外掛 (2則留言, 2023/02/11)
- WordPress.com將出售內容資料… (0則留言, 2024/02/29)
Sorry, 很久沒用Google sc…