蔡諝的窝

博客园 首页 新随笔 联系 订阅 管理

1)目的:实验室小网空间因镜像版本太多,容量告警,希望清出一部分空间

具体需求:删除E盘下,所有含rar字符串的文件;

              删除E盘下,所有含hi6620字符串文件夹;

步骤:

(风险请知:如果不check告警的文件file.log和folder.log,而在脚本暂停时继续,有误删的风险)

1.将下面批处理保存为xxx.bat

2.运行脚本

脚本如下:

@echo off

rem 如果是磁盘符,最好加上\符合
set DESC_PATH=E:\

rem 只需要部分字符
set file_str=rar
set folder_str=hi6620

rem 控制开关
set search_file=1
set search_folder=1
set del_flag=1

rem 临时文件,备份记录用,不用修改
set file_log=file.log
set folder_log=folder.log
set workspace=%CD%

rem 只查找文件
if %search_file%==1 (
    cd /d %DESC_PATH%
    rem 搜寻子目录,排除文件夹
    dir /b /s /a-d | find "%file_str%" > %workspace%\%file_log%
)

rem 只查找目录
if %search_folder%==1 (
    cd /d %DESC_PATH%
    rem 目的:希望不统计文件在内
    dir /b /s /a-a | find "%folder_str%" > %workspace%\%folder_log%
)

rem 待考虑引入输入判断yes or no 决定是否删除,这样更人性
echo Warning:%workspace%\%file_log% and %folder_log%, please check ...
pause

cd /d %workspace%
if %del_flag%==1 (
    if %search_file%==1 (
        for /f %%i in (%workspace%\%file_log%) do  (
            if exist %%i del %%i
        )
    )
    if %search_folder%==1 (
        for /f %%i in (%workspace%\%folder_log%) do  (
            if exist %%i rd /s /q %%i
        )
    )
)

cd /d %workspace%
pause

2)check两个临时文件时,建议用命令排除一些你不想看到的条目

比如排除含img或lib或log或tool字符串的条目,将结果存入tmp.log,命令如下:

type folder.log | findstr /v "img lib log tool" > tmp.log

点评:脚本中用的是find命令,只能一次搜索一个字符串,如果该为findstr命令效果会更好些;

posted on 2013-06-30 21:29  蔡諝  阅读(3389)  评论(0编辑  收藏  举报