1.删除指定路径(下面的例子是脚本文件所在目录下的file文件夹)下指定天数(下面的例子是3天前)以前的文件。
set delpath=%~dp0file
set days=-3
forfiles /p %delpath% /s /m * /d %days% /c "cmd /c rd /s /q @path" 
如果删除不干净,可以再加一句,变成:
set delpath=%~dp0file
set days=-3
forfiles /p %delpath% /s /m * /d %days% /c "cmd /c rd /s /q @path"
forfiles /p %delpath% /s /m * /d %days% /c "cmd /c del /s /q @path" 
再完善一下
 说明:设置每天清理脚本文件所在目录下的file文件夹下的所有3天前的文件,记录每次的清理时间,并且不显示命令行窗口:
@echo off
if "%1"=="h" goto begin
mshta vbscript:createobject("wscript.shell").run("""%~nx0""h",0)(window.close)&&exit
:begin
set delpath=%~dp0file
set days=-3
forfiles /p %delpath% /s /m * /d %days% /c "cmd /c rd /s /q @path"
forfiles /p %delpath% /s /m * /d %days% /c "cmd /c del /s /q @path"
timeout 86400
echo %date% %time%>>%~dp0\delFiles.log
start /min %~df0 & exit 
2.批量修改文本文档的内容
 说明:修改当前文件所在目录下的modify目录中格式为.xml的所有文件,将"    <user>admin</user>"替换为"    <user>java_t_t</user>"(按行为单位进行替换,本来可以用一个循环完成,但是在win10系统上测试的时候(win7上测试可以,也许是系统设置的问题),发现刚刚修改完的文件会被外层的for循环再次读取,导致程序陷入死循环,所以使用两个for,避免陷入死循环)
@echo off
set nowDir=%~dp0
set modifyDir=%nowDir%\modify
set modifyLog=%nowDir%\modify.log
echo.>%modifyLog%
for /r %modifyDir% %%i in ("*.xml") do (
    echo start modify    %%i>>%modifyLog%
    for /f "delims=" %%j in (%%i) do (
        if "%%j" EQU "    <user>admin</user>" (echo     ^<user^>java_t_t^</user^>) else (echo %%j)
    )>>%%~dpi%%~ni.tmp
)
for /r %modifyDir% %%i in ("*.tmp") do (
    del %%~dpi%%~ni.xml
    ren %%i %%~ni.xml
    echo finished modify %%~dpi%%~ni.xml>>%modifyLog%
) 
 扩展知识:https://blog.csdn.net/xhhjin/article/details/7373524
BAT脚本文件清理与文本修改操作
        
                  
                  
                  
                  
                            
本文介绍了BAT脚本的两个应用。一是设置每天清理脚本所在目录下file文件夹中3天前的文件,记录清理时间且不显示命令行窗口;二是批量修改当前目录下modify目录中.xml文件内容,将特定文本替换,为避免win10系统死循环使用两个for循环。
          
      
          
                
                
                
                
              
                
                
                
                
                
              
                
                
              
            
                  
					650
					
被折叠的  条评论
		 为什么被折叠?
		 
		 
		
    
  
    
  
            


            