Board logo

标题: [文件操作] 批处理怎样遍历并且删除每个用户文件夹下指定格式文件? [打印本页]

作者: ppappa2001    时间: 2012-2-16 22:06     标题: 批处理怎样遍历并且删除每个用户文件夹下指定格式文件?

大家好, 小弟学艺不精.. 有如下需求请大侠帮忙.

需求: 在多用户登录的电脑上, 能删除Win7每个用户文件夹下的缓存文件.  以下是我写的代码, 遇到错误, 请大家指正. 谢谢

#1, 脚本需要遍历 C:\users\   
#2, 判断 C:\Users\xxxx\AppData\Local\Microsoft\outlook\ 文件夹下是否存在 outlook.ost 和 *.pst文件.
#3, 如果存在, 并且删除掉.
#4, 再循环 判断和删除下一个用户文件夹的 邮件缓存文件.
#5, 对删除成功的文件记录到文本日志文件.
  1. @echo off
  2. ::枚举用户文件夹名字写入list.txt
  3. dir /ad /b c:\users > list.txt
  4. Set RootFolder = C:\Users\
  5. Set SubFolder = \AppData\Local\Microsoft\Outlook\
  6. echo %RootFolder%
  7. echo %SubFolder%
  8. ::读取list.txt 连接成绝对路径
  9. for /f %%i in (list.txt) do (
  10. echo %RootFolder% & %%i &  %SubFolder%
  11. :: 连接成为绝对路径, (路径显示不正确.)
  12. Set MyPath = %RootFolder% & %%i & %SubFolder%
  13. ::判断路径是否存在, 如果存在就做删除操作.
  14. if exist %MyPth% (del /s /f %MyPath%outlook.ost)
  15. )
  16. PAUSE
复制代码
运行结果:

ECHO is off.
ECHO is off.
The syntax of the command is incorrect.
作者: find    时间: 2012-2-16 22:08

把等号两边的空格全部删掉
作者: ppappa2001    时间: 2012-2-16 23:00

回复 2# find


    谢谢Find..   但是执行结果还是不对.  可能是路径连接的地方有问题. 我把变量 这样写echo %RootFolder%&%%i&%SubFolder%, 还是不对... 请指教

list.txt 文件内容是:
All Users
Default
Default User
admin
Public

命令行执行结果如下:

C:\Users\
\AppData\Local\Microsoft\Outlook\
C:\Users\
'All' is not recognized as an internal or external command,operable program or batch file.
'\AppData\Local\Microsoft\Outlook\' is not recognized as an internal or external command,operable program or batch file.
'All' is not recognized as an internal or external command,operable program or batch file.
'\AppData\Local\Microsoft\Outlook\' is not recognized as an internal or external command,operable program or batch file.
C:\Users\
'Default' is not recognized as an internal or external command,operable program or batch file.
'\AppData\Local\Microsoft\Outlook\' is not recognized as an internal or external command,operable program or batch file.
'Default' is not recognized as an internal or external command,operable program or batch file.
'\AppData\Local\Microsoft\Outlook\' is not recognized as an internal or external command,operable program or batch file.
C:\Users\
'Default' is not recognized as an internal or external command,operable program or batch file.
'\AppData\Local\Microsoft\Outlook\' is not recognized as an internal or external command,operable program or batch file.
'Default' is not recognized as an internal or external command,operable program or batch file.
'\AppData\Local\Microsoft\Outlook\' is not recognized as an internal or external command,operable program or batch file.
C:\Users\
'admin' is not recognized as an internal or external command,operable program or batch file.
'\AppData\Local\Microsoft\Outlook\' is not recognized as an internal or external command,operable program or batch file.
C:\Users\
'Public' is not recognized as an internal or external command,operable program or batch file.
'\AppData\Local\Microsoft\Outlook\' is not recognized as an internal or external command,operable program or batch file.
'Public' is not recognized as an internal or external command,operable program or batch file.
'\AppData\Local\Microsoft\Outlook\' is not recognized as an internal or external command,operable program or batch file.
作者: find    时间: 2012-2-16 23:21

回复 3# ppappa2001


连接的时候不需要使用&符号
作者: ppappa2001    时间: 2012-2-16 23:26

本帖最后由 ppappa2001 于 2012-2-16 23:30 编辑

去掉过后, 路径对了..


但是在判断文件是否存在时候,  删除动作没执行. 也没报错..

C:\Users\
\AppData\Local\Microsoft\Outlook\
C:\Users\All\AppData\Local\Microsoft\Outlook\
C:\Users\Default\AppData\Local\Microsoft\Outlook\
C:\Users\Default\AppData\Local\Microsoft\Outlook\
C:\Users\admin\AppData\Local\Microsoft\Outlook\
C:\Users\Public\AppData\Local\Microsoft\Outlook\

修改以后的代码如下:
  1. @echo off
  2. ::枚举用户文件夹名字写入list.txt
  3. dir /ad /b c:\users > list.txt
  4. Set RootFolder=C:\Users\
  5. Set SubFolder=\AppData\Local\Microsoft\Outlook\
  6. echo %RootFolder%
  7. echo %SubFolder%
  8. ::读取list.txt 连接成绝对路径
  9. for /f %%i in (list.txt) do (
  10. echo %RootFolder%%%i%SubFolder%
  11. :: 连接成为绝对路径, (路径显示不正确.)
  12. Set MyPath = %RootFolder%%%i%SubFolder%
  13. ::判断路径是否存在, 如果存在就做删除操作.
  14. if exist %MyPth% (del /s /f %MyPath%outlook.ost)
  15. )
  16. PAUSE
复制代码

作者: find    时间: 2012-2-16 23:41

回复 5# ppappa2001


等号两边的空格还没有删完
作者: ppappa2001    时间: 2012-2-16 23:43

回复 6# find

重新改过了.
  1.     @echo off
  2. ::枚举用户文件夹名字写入list.txt
  3. dir /ad /b c:\users > list.txt
  4. Set RootFolder=C:\Users\
  5. Set SubFolder=\AppData\Local\Microsoft\Outlook\
  6. echo %RootFolder%
  7. echo %SubFolder%
  8. ::读取list.txt 连接成绝对路径
  9. for /f %%i in (list.txt) do (
  10. echo %RootFolder%%%i%SubFolder%
  11. :: 连接成为绝对路径, (路径正确如: C:\Users\Default\AppData\Local\Microsoft\Outlook\)
  12. Set MyPath=%RootFolder%%%i%SubFolder%
  13. echo %MyPath%outlook.ost
  14. echo ..
  15. ::判断路径是否存在, 如果存在就做删除操作.
  16. if exist %MyPath%outlook.ost (del /s /f %MyPath%outlook.ost)
复制代码
结果:

C:\Users\
\AppData\Local\Microsoft\Outlook\
C:\Users\All\AppData\Local\Microsoft\Outlook\
C:\Users\Public\AppData\Local\Microsoft\Outlook\outlook.ost
..
C:\Users\Default\AppData\Local\Microsoft\Outlook\
C:\Users\Public\AppData\Local\Microsoft\Outlook\outlook.ost
..
C:\Users\Default\AppData\Local\Microsoft\Outlook\
C:\Users\Public\AppData\Local\Microsoft\Outlook\outlook.ost
..
C:\Users\admin\AppData\Local\Microsoft\Outlook\
C:\Users\Public\AppData\Local\Microsoft\Outlook\outlook.ost
..
C:\Users\Public\AppData\Local\Microsoft\Outlook\
C:\Users\Public\AppData\Local\Microsoft\Outlook\outlook.ost
作者: ppappa2001    时间: 2012-2-16 23:44

文件还是没删掉....
作者: ppappa2001    时间: 2012-2-16 23:45

而且很纳闷, 为啥第19行输出的 都变成了 Public

C:\Users\Public\AppData\Local\Microsoft\Outlook\outlook.ost
作者: find    时间: 2012-2-16 23:46

回复 7# ppappa2001


for /f "delims=" %%i in (list.txt) do (
作者: ppappa2001    时间: 2012-2-16 23:49

改了还是一样的结果喃?
作者: find    时间: 2012-2-16 23:51

  1. @echo off
  2. set RootFolder=C:\Users
  3. set SubFolder=AppData\Local\Microsoft\Outlook
  4. for /f "delims=" %%i in ('dir /ad /b C:\users') do (
  5.     echo %RootFolder%\%%i\%SubFolder%\outlook.ost
  6.     if exist "%RootFolder%\%%i\%SubFolder%\outlook.ost" (
  7.         del /f /q "%RootFolder%\%%i\%SubFolder%\outlook.ost"
  8.     )
  9. )
复制代码

作者: ppappa2001    时间: 2012-2-16 23:56

完美   多谢find
作者: ppappa2001    时间: 2012-2-17 00:00

本帖最后由 ppappa2001 于 2012-2-17 00:12 编辑

回复 12# find

我把文件名字改成了通配符格式.
  1. @echo off
  2. set RootFolder=C:\Users
  3. set SubFolder=AppData\Local\Microsoft\Outlook
  4. for /f "delims=" %%i in ('dir /ad /b C:\users') do (
  5. REM  echo %RootFolder%\%%i\%SubFolder%\*.ost
  6.     if exist "%RootFolder%\%%i\%SubFolder%\*.ost" (
  7.         dir /b %RootFolder%\%%i\%SubFolder%\*.ost>>Delete.log
  8.         del /f /q "%RootFolder%\%%i\%SubFolder%\*.ost"
  9.     )
  10. )
复制代码
能不能在第12行的 删除操作之前, 把删除的文件路径写入日志文件喃?

我增加了删除之前枚举文件的命令. 如果删除成功, 那么删掉的文件就是被枚举的文件. 但是如果进程被占用, 那么文件并没有被删除, 而文件名还是被写入日志, 就不符合实际..  有啥办法只把删掉的文件写入日志..??
作者: applba    时间: 2012-2-17 01:03

你的思路本来就有问题。
作者: applba    时间: 2012-2-17 01:13

  1. pushd c:\users
  2. dir /s /b outloook.* *.pst
复制代码

作者: apang    时间: 2012-2-17 01:28

本帖最后由 apang 于 2012-2-17 01:30 编辑

回复 14# ppappa2001
  1.           for /f "delims=" %%j in ('dir /a-d /b "%RootFolder%\%%i\%SubFolder%\*.ost"') do (
  2.                 del /f /q "%%~dpnxj"&&echo 已删除%%~dpnxj >>delete.log
  3.           )      
  4.     )
复制代码
第10行及以下,改成这样试试
作者: ppappa2001    时间: 2012-2-17 01:31

我知道我思路有问题, 但是怎样才能取得 被删除的文件列表喃?
作者: ppappa2001    时间: 2012-2-17 02:05

本帖最后由 ppappa2001 于 2012-2-17 02:08 编辑

回复 17# apang


    改了以后 路径不对, 实际的路径 是: C:\Users\admin\AppData\Local\Microsoft\outlook;   程序当前路径是 c:\DOCUMENT\Desktop\

   另外能否解释下为什么要这样写喃? %%~dpnxj 这个变量的引入不是很理解.

Could Not Find c:\DOCUMENT\Desktop\test - Copy (2).ost
Could Not Find c:\DOCUMENT\Desktop\test - Copy (3).ost
Could Not Find c:\DOCUMENT\Desktop\test - Copy (4).ost
Could Not Find c:\DOCUMENT\Desktop\test - Copy (5).ost
Could Not Find c:\DOCUMENT\Desktop\test - Copy (6).ost
The system cannot find the path specified.
The system cannot find the file specified.
The system cannot find the file specified.
Could Not Find c:\DOCUMENT\Desktop\test - Copy (2).ost
Could Not Find c:\DOCUMENT\Desktop\test - Copy (3).ost
Could Not Find c:\DOCUMENT\Desktop\test - Copy (4).ost
Could Not Find c:\DOCUMENT\Desktop\test - Copy (5).ost
Could Not Find c:\DOCUMENT\Desktop\test - Copy (6).ost
The system cannot find the path specified.
作者: find    时间: 2012-2-17 09:30

回复 14# ppappa2001
  1. @echo off
  2. for /f "delims=" %%i in ('dir /s /b "C:\Users\*.ost"') do (
  3.     del /f /q "%%i" && echo,%%i>>Delete.log
  4. )
复制代码

作者: apang    时间: 2012-2-17 10:36

回复 19# ppappa2001
  1. @echo off
  2. set RootFolder=C:\Users
  3. set SubFolder=AppData\Local\Microsoft\Outlook
  4. for /f "delims=" %%i in ('dir /ad /b C:\users') do (
  5. REM  echo %RootFolder%\%%i\%SubFolder%\*.ost
  6.     for /f "delims=" %%j in ('dir /a-d /b /s "%RootFolder%\%%i\%SubFolder%\*.ost"') do (
  7.         del /f /q "%%~dpnxj"&&echo 已删除%%~dpnxj >>delete.log
  8.     )
  9. )
复制代码

作者: 594850072    时间: 2012-2-17 10:39

学习了
作者: ppappa2001    时间: 2012-2-17 10:50

非常感谢楼上各位大侠的帮助.   代码调试通过了, 最后从格式上做了些美化. 执行的结果会写入当前路径 以当前日期为文件名的日志, 内容如下, 并附上代码, 谢谢大家.
\\\\\\\\\\\\\\\\\\\\\\\\\2012/02/17 周五10:43:39.41, Deleting Files for Administrator On admin-MOBL, IP: 192.168.10.1\\\\\\\\\\\\\\\\\\\\\\\\\

Deleting C:\Users\Administrator\AppData\Local\Microsoft\Outlook\test - Copy (2).ost 0
Deleting C:\Users\Administrator\AppData\Local\Microsoft\Outlook\test - Copy (3).ost 0
Deleting C:\Users\Administrator\AppData\Local\Microsoft\Outlook\test - Copy (4).ost 0
Deleting C:\Users\Administrator\AppData\Local\Microsoft\Outlook\test - Copy (5).ost 0
Deleting C:\Users\Administrator\AppData\Local\Microsoft\Outlook\test - Copy (6).ost 0

Deleting C:\Users\Administrator\AppData\Local\Microsoft\Outlook\test - Copy - Copy (2) - Copy.pst 0
Deleting C:\Users\Administrator\AppData\Local\Microsoft\Outlook\test - Copy - Copy (2).pst 0
Deleting C:\Users\Administrator\AppData\Local\Microsoft\Outlook\test - Copy - Copy - Copy - Copy - Copy.pst 0
Deleting C:\Users\Administrator\AppData\Local\Microsoft\Outlook\test - Copy - Copy - Copy - Copy.pst 0
Deleting C:\Users\Administrator\AppData\Local\Microsoft\Outlook\test - Copy - Copy - Copy.pst 0
Deleting C:\Users\Administrator\AppData\Local\Microsoft\Outlook\test - Copy - Copy.pst 0
Deleting C:\Users\Administrator\AppData\Local\Microsoft\Outlook\test - Copy.pst 0

删除了12个文件,一共为0KB
============================Below Files can't be Removed============================        
      

\\\\\\\\\\\\\\\\\\\\\\\\\2012/02/17 周五10:43:39.41, Deleting Files for All Users On admin-MOBL, IP: 192.168.10.1\\\\\\\\\\\\\\\\\\\\\\\\\

\\\\\\\\\\\\\\\\\\\\\\\\\2012/02/17 周五10:43:39.41, Deleting Files for Default On admin-MOBL, IP: 192.168.10.1\\\\\\\\\\\\\\\\\\\\\\\\\

\\\\\\\\\\\\\\\\\\\\\\\\\2012/02/17 周五10:43:39.41, Deleting Files for Default User On admin-MOBL, IP: 192.168.10.1\\\\\\\\\\\\\\\\\\\\\\\\\

\\\\\\\\\\\\\\\\\\\\\\\\\2012/02/17 周五10:43:39.41, Deleting Files for admin On admin-MOBL, IP: 192.168.10.1\\\\\\\\\\\\\\\\\\\\\\\\\
  1. @echo off
  2. ipconfig
  3. @echo -
  4. for /f "tokens=2 delims=:" %%i in ('ipconfig^|findstr "Address"') do set ip=%%i
  5. cls
  6. REM @echo ==================[你的IP地址是:%ip%]===============
  7. REM  ====================================================
  8.    @ECHO OFF
  9. :: The Program only works on Windows English Version
  10. :: For Example
  11. :: The current date is: 05/27/2009 Wed
  12. :: Enter the new date: (mm-dd-yy)
  13.     ECHO.|DATE >temp1
  14.     FOR /F "eol=T tokens=5 delims=() " %%i in (temp1) do set date_format=%%i
  15.     FOR /F "eol=E tokens=5,6,7,8 delims=/- " %%a in (temp1) do (
  16.         IF /I %date_format% EQU mm-dd-yy ( set month=%%a
  17.                                            set day=%%b
  18.                                            set year=%%c
  19.                                            set week=%%d )
  20.         IF /I %date_format% EQU dd-mm-yy ( set day=%%a
  21.                                            set month=%%b
  22.                                            set year=%%c
  23.                                            set week=%%d )
  24.         IF /I %date_format% EQU yy-mm-dd ( set year=%%a
  25.                                            set month=%%b
  26.                                            set day=%%c
  27.                                            set week=%%d )      )
  28.    
  29.     SET current_date=%year%-%month%-%day%
  30.    
  31.     IF EXIST temp1 DEL /Q temp1
  32.     REM ECHO Current date = %current_date%
  33.     REM ====================================================
  34.      
  35.    
  36.     set RootFolder=C:\Users
  37.     set SubFolder=AppData\Local\Microsoft\Outlook
  38.     for /f "delims=" %%i in ('dir /ad /b C:\users') do (
  39.     REM  echo %RootFolder%\%%i\%SubFolder%\*.ost
  40.     REM  echo %current_date%.log
  41.     REM ====================================
  42.     setlocal enabledelayedexpansion
  43.     set n=&set ds=0
  44.     echo \\\\\\\\\\\\\\\\\\\\\\\\\%date%%time%, Deleting Files for %%i On %computername%, IP:%ip%\\\\\\\\\\\\\\\\\\\\\\\\\>>%current_date%.log
  45.     echo. >>%current_date%.log
  46.     if exist "%RootFolder%\%%i\%SubFolder%\*.ost" (              
  47.         for /f "delims=" %%j in ('dir /b/s %RootFolder%\%%i\%SubFolder%\*.ost') do (set /a n=!n!+1
  48.         echo Deleting %%j %%~zj>>%current_date%.log
  49.         del /q/f "%%j"
  50.         set /a ds=!ds!+%%~zj
  51.         )
  52.         
  53.         echo.>>%current_date%.log
  54.         for /f "delims=" %%j in ('dir /b/s %RootFolder%\%%i\%SubFolder%\*.pst') do (set /a n=!n!+1
  55.         echo Deleting %%j %%~zj>>%current_date%.log
  56.         del /q/f "%%j"
  57.         set /a ds=!ds!+%%~zj
  58.         )
  59.         
  60.         echo.>>%current_date%.log
  61.         echo 删除了!n!个文件,一共为!ds!KB>>%current_date%.log
  62.         
  63.         echo ============================Below Files can't be Removed============================>>%current_date%.log        
  64.         dir /d /b %RootFolder%\%%i\%SubFolder%\*.ost >>%current_date%.log     
  65.         dir /d /b %RootFolder%\%%i\%SubFolder%\*.pst >>%current_date%.log           
  66.         echo. >>%current_date%.log      
  67.         echo. >>%current_date%.log
  68.     )
  69.     REM End "If Exist on line58"
  70. )
  71. REM End "For on line48"
复制代码





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