添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

由于对接zabbix监控的需要,需要监控指定目录下文件数量是否超标、文件总大小是否超标,为此需要写个脚本获取:

  • 指定目录或指定文件的文件数量,如指定文件则返回1
  • 指定目录或指定文件的文件磁盘占用空间

Bat/cmd脚本实现原理如下:

dir /s 目录或文件
2020/03/08  17:49               139 file_size.tmp
2020/03/08  17:48               733 file_statics.bat
2020/03/07  10:48                26 md5_tst.txt
2020/03/07  10:48                49 md5_tst.txt.md5
               7 个文件          1,784 字节
     所列文件总数:
               7 个文件          1,784 字节
               2 个目录 88,523,546,624 可用字节

解析最后2行:文件数量、文件大小即可,具体代码如下:

1、file_size.bat

@echo off & setlocal enabledelayedexpansion
rem 参数去引号
set arg1=%~1
set arg2=%~2
rem 第一个参数必须是size或count
if "%arg1%" == "size" goto file_statics
if "%arg1%" == "count" goto file_statics
goto usage
:file_statics
rem 指定目录或文件路径不能为空
if "%arg2%" == "" goto usage
if not exist %arg2% goto usage
rem 利用dir /s 统计文件数量、大小,输出到file_size.tmp
dir /s %arg2% | find "字节" > file_size.tmp
rem 解析file_size.tmp得到文件数量、大小
set total=0
for /f %%i in (file_size.tmp) do (
    set /a total+=1
set /a total=%total%-1
set count=0
for /f "tokens=1,2,3,*" %%i in (file_size.tmp) do (
    set /a count+=1
    if !count! equ %total% (
        set file_count=%%i
        set file_size=%%k
if "%arg1%" == "count" echo %file_count:,=%
if "%arg1%" == "size" echo %file_size:,=%
goto :eof
:usage
echo "%0 size|count filepath|folderpath"
exit /b 1

2、用法演示

D:\bat_cmd>file_statics.bat count d:\bat_cmd
D:\bat_cmd>file_statics.bat size d:\bat_cmd
D:\bat_cmd>file_statics.bat size d:\bat_cmd\file_statics.bat
D:\bat_cmd>file_statics.bat count d:\bat_cmd\file_statics.bat

3、注意事项

由于脚本file_size.bat中有中文,文件保存时请注意编码格式,vscode请用gb2312.

本文链接:https://blog.csdn.net/wzq9706/article/details/11924157 @echo off setlocal enabledelayedexpansion for /f "delims=" %%i in ('dir /a-d /b *.*') do ( echo %%i:文件大小=%%~zi字节&echo. pause set arg="333.txt" certutil -hashfile %arg% MD5 | find /v ":" > %para1%.md5 set /p md5=<%para1%.md5 echo %md5% ::获取文件大小 FOR %%A IN (%arg%) DO set size=%%~zA ::字节为单位 echo size is: %size% [code=plain] 官网给出了 highlight.js, 并且也有对应的css 可以直接下载引入到你的项目中。 [支持197 languages and 248 styles https://highlightjs.org/static/demo/](https://highlightjs.org/static/demo/) 查看 F12 即可知道css 的下载地址 [/code]
web页面 js 加载和显示Markdown .md文件总结 w1047667241: 在线编辑,还有 kodbox, 是我用的最爽的一款 私人服务器文档管理器。 配合 zerotier 可实现远程管理文档,实时同步