Board logo

标题: [文件操作] 来个大神99批处理怎样根据硬盘空间复制文件 [打印本页]

作者: smss    时间: 2019-2-15 18:28     标题: 来个大神99批处理怎样根据硬盘空间复制文件

需求如下
当D盘大于100G则复制A文件到D
当D盘小于100G则复制A文件到E
作者: happyxxdhaha    时间: 2019-2-15 21:29

  1. for /f "delims== tokens=2" %i in ('wmic logicaldisk where "caption='d:'" get freespace /value') do @if %i gtr 100000000000 (copy a.txt d:\) else (copy a.txt e:\)
复制代码

作者: Batcher    时间: 2019-2-16 10:11

回复 2# happyxxdhaha


    比较这么大的数字会出错吧,你试试。
作者: smss    时间: 2019-2-16 12:46

happyxxdhaha 发表于 2019-2-15 21:29

测试无效
作者: smss    时间: 2019-2-16 17:01

回复  happyxxdhaha
比较这么大的数字会出错吧,你试试。
Batcher 发表于 2019-2-16 10:11
  1. @echo off
  2. Setlocal enabledelayedexpansion
  3. for /f "skip=1 tokens=1-6" %%a in ('wmic logicaldisk get caption^,drivetype^,freespace') do (if "%%b"=="3" (set "size=%%c"
  4. )
  5. )
  6. if %size% gtr 107374182400 (copy a.txt d:\) else (copy a.txt e:\)
  7. pause
复制代码
凑了一段 可以用 还能再精减吗
对截取不理解
作者: happyxxdhaha    时间: 2019-2-17 01:21

本帖最后由 happyxxdhaha 于 2019-2-17 02:22 编辑
回复  happyxxdhaha


    比较这么大的数字会出错吧,你试试。
Batcher 发表于 2019-2-16 10:11
  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "delims== tokens=2" %%i in ('wmic logicaldisk where "caption='d:'" get freespace /value') do set freespace=%%i
  3. set _100g=107374182400
  4. rem 获取字符串长度
  5. set n=
  6. :loop1
  7. set /a n+=1
  8. if "!freespace:~%n%,1!"=="" (goto next1) else (goto loop1)
  9. :next1
  10. rem %n%为已获取到的字符串长度,100G的字符串长度为12,两者相比较
  11. if %n% equ 12 (call :equ) else (if %n% gtr 12 (call :gtr) else (call :lss))
  12. endlocal
  13. goto:eof
  14. :gtr
  15. set /a v=%n%-12
  16. rem z0的第一个参数长度最小,第二个参数长度最大,第三个参数%v%为两个字符串长度的差
  17. call :z0 %_100g% %freespace% %v%
  18. goto:eof
  19. :lss
  20. set /a v=12-%n%
  21. call :z0 %freespace% %_100g% %v%
  22. goto:eof
  23. :equ
  24. call :z0 %freespace% %_100g% 0
  25. goto:eof
  26. :z0
  27. setlocal
  28. if %3 equ 0 goto next2
  29. rem 将长度最小的字符串前面补0,使两个字符串长度相同,然后再进行字符串比较
  30. set str=%1
  31. set n=
  32. :loop2
  33. set /a n+=1
  34. set str=0%str%
  35. if %n% equ %3 (goto next2) else (goto loop2)
  36. :next2
  37. rem %str%为补0之后的字符串
  38. if "%2" gtr "%str%" (copy a.txt d:\) else (copy a.txt e:\)
  39. endlocal
  40. goto:eof
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2