cmd命令,bat脚本
1.cd /d
D:\>cd mysqlD:\mysql>cd /d C:/TEMPC:\Temp>cd /? 显示当前目录名或改变当前目录。CHDIR [/D] [drive:][path] CHDIR [..] CD [/D] [drive:][path] CD [..].. 指定要改成父目录。键入 CD drive: 显示指定驱动器中的当前目录。 不带参数只键入 CD,则显示当前驱动器和目录。使用 /D 开关,除了改变驱动器的当前目录之外, 还可改变当前驱动器。如果命令扩展被启用,CHDIR 会如下改变:当前的目录字符串会被转换成使用磁盘名上的大小写。所以, 如果磁盘上的大小写如此,CD C:\TEMP 会将当前目录设为 C:\Temp。CHDIR 命令不把空格当作分隔符,因此有可能将目录名改为一个 带有空格但不带有引号的子目录名。例如:cd \winnt\profiles\username\programs\start menu与下列相同:cd "\winnt\profiles\username\programs\start menu"在扩展停用的情况下,你必须键入以上命令。C:\Temp>cd
2.建立多级目录
md IBM\SPSS\Modeler\18.0\
3.windows下.bat每隔一段时间(例如15分钟)运行一次某python文件
https://blog.csdn.net/baidu_26408419/article/details/78885206?utm_source=blogxgwz0
4.结束进程
taskkill /f /im Explorer.exe (关闭Explorer.exe进程)
之前的资料:
dos 批处理命令(forfiles)
Windows Server 2003内置的命令行文件,不适合于XP系统
1. 用批处理文件删除当前目录下 7 天以前的扩展名为bkf文件(以当前系统时间为基准)
示例: forfiles /m *.bkf /d -7 /c "cmd /c del @file /f"
2. forfiles 自动删除7天前文件 (以当前系统时间为基准)
示例: forfiles /p "d:\test" /s /m *.* /d -7 /c "cmd /c del @path"
d:\test换成你要的目录路径; /d -7 指删除7天以前文件。
3. 删除所有的空目录(以删除d:\test目录下为例) :
dir /ad/b/s d:\test\ |sort /r >d:\kill.txt
For /f "tokens=*" %%i in (d:\kill.txt) DO rd "%%i"
del d:\kill.txt
4. 先删7 天以前文件,再删所有的空目录 ;把以下复制到bat文件中。
@echo off
forfiles /p "d:\test" /s /m *.* /d -7 /c "cmd /c del @path"
dir /ad/b/s d:\test\ |sort /r >d:\kill.txt
For /f "tokens=*" %%i in (d:\kill.txt) DO rd "%%i"
del d:\kill.txt
5. forfiles 命令的用法及参数:
forfiles /p <目标目录名> /d <天数> /c <执行的命令>
/p 指定了要在哪个目录里查找文件,默认是当前工作目录。
/d 指定一个日期或天数,用于比较文件的最后修改日期是否符合条件。
/c 对每个找到的文件执行的命令。
例1.要把在C盘根目录下最后修改日期大于或等于2010年1月7日的文件复制到D盘根目录下:
forfiles /p "c:\" /d "2007-7-1" /c "cmd /c copy @path d:\"
例2.删除在C盘backup目录下最后修改日期在10天前的文件:
forfiles /p "c:\backup" /d -10 /c "cmd /c echo deleting @file ... && del /f @path"
6. forfiles /p 包含要删除文件的完整路径(如:F:\Logfiles) /m *.log -d -7 /c "cmd /c del /f
@path"
解释一下相关参数及命令
/p <Path> : 指定开始搜索文件的位置,如果不指定则默认为当前目录。
/m <SearchMask> : 文件查找所使用的通配符如代码中的"*.log"则为所有日志文件,当然也可以指定诸
如"manmee_*.log"这样以manmee开头的所有日志文件。如果不指定此参数则默认为"*.*"。
/d [{+|-}][{<Date>|<Days>}] : 指定想选择文件的最后修改时间,上文中用了 "/d -7" 表示所有以当
天为基础,7天以前的文件。当然这里还可以指定具体时间,例如:"/d -01/7/2010"这样所有早于2010年
1月7日的文件。注意指定的时间必须是"MM/DD/YYYY"的格式。
/c 对所有文件以此执行指定的命令,命令体须在双引号(")内,默认是"cmd /c echo @file"。上文中用
到的是"cmd /c del /f @path"删除指定文件。(这里的@file 和 @path 为变量,下文中将解释。)
下面说一下上文中用到的参数:
@PATH : 表示文件的完整的路径。
@File : 表示文件名称。
接下来我们看看删除文件夹的操作,如果你看了上面的介绍,相信这命令你一看就能看懂。
forfiles /p 包含文件夹的路径(如:F:\) /m 文件夹名称(如:LogFiles) -d 0 /c "cmd /c if @ISDIR
== true rd /s/q @path"
注意这里的"包含文件夹的路径"不能包含要删除的文件夹,如以上代码所表示的就是,在F盘中查找名为
LogFiles的文件或文件夹(不能指定查找文件夹,不过在删除时我们做了判断)。
还有就是这里出现了一个新的参数"@ISDIR"他用于判断当前文件类型是否是"文件夹类型",如果是则为
true否则为false。
相信到这大家就明白了,最后再将代码保存为批处理文件,然后加入计划任务定期执行即可。
看了上面的例子,觉得在 Windows Server 2003 下面要删除老文件很简单吧。
但如果操作系统是 Windows 2000/XP 就比较麻烦,因为它们没有forfiles命令,只能靠自己写批处理来
实现了。
下面是我写的批处理文件内容:
@echo off
rem ******************************
rem * 按时间删除文件目录的批处理 *
rem ******************************
rem 设置临时目录的路径
set tempDir=%tmp%\remove_%date:~0,10%
if not exist %tempDir% md %tempDir%
rem 设置处理日期的脚本文件的路径
set scriptFile=%tempDir%\get_date.vbs
rem 获得要保留的天数
set days=%~1
if "%days%" == "" goto printUsage
rem 获得目标目录的路径
set dirPath=%~2
if "%dirPath%" == "" set dirPath=.
rem 获得要操作的文件形式
set fileSpec=%~3
if "%fileSpec%" == "" set fileSpec=*.*
rem 生成计算日期的脚本文件并获得删除的截止日期
echo d=date()-%1 > %scriptFile%
echo s=right("0000" ^& year(d),4) ^& "-" ^& right("00" ^& month(d),2) ^& "-" ^& right("00"
^& day(d),2) >> %scriptFile%
echo wscript.echo s >> %scriptFile%
for /f %%i in ('cscript /nologo %scriptFile%') do set lastDate=%%i
rem 处理目标目录里的每个对象
for /f "tokens=1,2,3* delims=<> " %%i in ('dir "%dirPath%\%fileSpec%" /a /-c /tc') do call
:proc "%%i" "%%j" "%%k" "%%l"
goto :done
rem 处理目标目录里对象的过程
:proc
rem 获得对象的创建日期并判断是否为有效格式
set fileDate=%~1
echo %fileDate% | findstr "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]" > nul
if errorlevel 1 goto end
rem 获得对象的类型
set fileType=%~3
if "%fileType%" == "" goto end
rem 获得对象的名称
set fileName=%~4
if "%fileName%" == "" goto end
if "%fileName%" == "." goto end
if "%fileName%" == ".." goto end
if "%fileName%" == "字节" goto end
if "%fileName%" == "可用字节" goto end
rem 判断对象日期是否小于或等于删除的截止日期
if "%fileDate:~0,10%" leq "%lastDate%" (
echo deleting "%fileName%" ...
if "%fileType%" == "DIR" ( rd /s /q "%dirPath%\%fileName%" ) else ( del /q /f "%dirPath%\%
fileName%" )
)
goto end
:error
echo An error occurred during backuping.
:done
rd /s /q %tempDir%
goto end
:printUsage
echo Usage: %0 ^<Days^> [Work directory] [Target file specification (can include wildcards)]
goto end
:end
主要是利用Windows的脚本功能来计算要删除文件的截止日期,然后for加dir命令来提取文件的日期进行
判断。
关于forfiles和for的详细信息,可以在Windows的帮助与支持中找到。
bat脚本的例子:
:: 2016-7-20 版本2.0 :: 大更新,更换统计算法,实现模块调用 :: 支持对隐藏文件、休眠文件的判定 :: 收集第三方大文件目录,如www、inetpub :: 解决 超过10GB大文件,由于32位子系统限制导致的文件精度统计:: 2016-7-19 版本1.0 :: 一键统计 系统主要目录容量,快速判定大文件,便于工单处理 :: 以MB为单位输出,便于阅读 :: 交流 EM:dingxiaoguang666@163.com@echo off&setlocal enabledelayedexpansion mode con: cols=80 lines=31 color 0a title Windows 大文件快速判定 by:Ckey echo Windows 大文件快速判定 set /a s=1024*1024*1024 set dir=C:\windows\syswow64 echo. echo --------------------------------- echo 文件夹:%dir% for /f "tokens=3 delims= " %%i in ('dir /s /a /-c "%dir%" ^|findstr 个文件') do (set z=%%i) call:division %z% %s% quot 2set dir=C:\windows\winsxs echo. echo 文件夹:%dir% for /f "tokens=3 delims= " %%i in ('dir /s /a /-c "%dir%" ^|findstr 个文件') do (set z=%%i) call:division %z% %s% quot 2set dir=C:\windows\temp echo. echo 文件夹:%dir% for /f "tokens=3 delims= " %%i in ('dir /s /a /-c "%dir%" ^|findstr 个文件') do (set z=%%i) call:division %z% %s% quot 2set dir=%temp% echo --------------------------------- echo 文件夹:临时文件 for /f "tokens=3 delims= " %%i in ('dir /s /a /-c "%dir%" ^|findstr 个文件') do (set z=%%i) call:division %z% %s% quot 2 echo. echo 文件夹:IE缓存 for /f "tokens=3 delims= " %%i in ('dir /s /a /-c "%dir%" ^|findstr 个文件') do (set z=%%i) call:division %z% %s% quot 2echo. echo --------------其他------------------- rem IF not EXIST "C:\hiberfil.sys1" (ECHO.)&PAUSE>nul&EXIT dir /A:H c:\ |findstr .sys :www IF not EXIST "C:\www" (goto int) >nul set dir=C:\www echo. echo 检测站点目录 %dir% for /f "tokens=3 delims= " %%i in ('dir /s /a /-c "%dir%" ^|findstr 个文件') do (set z=%%i) call:division %z% %s% quot 2 :int IF not EXIST "C:\inetpub" (goto pa) >nul set dir=C:\inetpub echo. echo 检测站点目录 %dir% for /f "tokens=3 delims= " %%i in ('dir /s /a /-c "%dir%" ^|findstr 个文件') do (set z=%%i) call:division %z% %s% quot 2 :pa echo. echo 检测完毕、任意键退出 EM:dingxiaoguang666@163.com pause >nul:division setlocal set str1=%1 set str2=%2 if "%~4" neq "" set u=%4 for %%i in (str1 str2) do if "!%%i:~,1!" == "-" set /a d+=1 if "%d%" == "1" (set d=-) else set "d=" set l=00000000&for /l %%i in (1 1 7) do set "l=!l!!l!" set "var=4096 2048 1024 512 256 128 64 32 16 8 4 2 1" for /l %%i in (1 1 2) do (set "str%%i=!str%%i:-=!"set /a "n=str%%i_2=0"for %%a in (!str%%i:.^= !) do (set /a n+=1set s=s%%a&set str%%i_!n!=0for %%b in (%var%) do if "!S:~%%b!" neq "" set/a str%%i_!n!+=%%b&set "S=!S:~%%b!"set /a len%%i+=str%%i_!n!)set str%%i=!str%%i:.=! ) if !str1_2! gtr !str2_2! (set /a len2+=str1_2-str2_2) else set /a len1+=str2_2-str1_2 for /l %%i in (1 1 2) do (set str%%i=!str%%i!!l!for %%j in (!len%%i!) do set " str%%i=!str%%i:~,%%j!" ) for /f "tokens=* delims=0" %%i in ("!str2!") do set s=%%i&set "str2=0%%i" set len2=1 for %%j in (%var%) do if "!S:~%%j!" neq "" set/a len2+=%%j&set "S=!S:~%%j!" set /a len=len2+1 if !len1! lss !len2! set len1=!len2!&set "str1=!l:~-%len2%,-%len1%!!str1!" set /a len1+=u&set str1=0!str1!!l:~,%u%! set str=!str1:~,%len2%! set "i=0000000!str2!"&set /a Len_i=Len2+7 for /l %%i in (1 1 9) do (set "T=0"for /l %%j in (8 8 !Len_i!) do (set /a "T=1!i:~-%%j,8!*%%i+T"set Num%%i=!T:~-8!!Num%%i!&set /a "T=!T:~,-8!-%%i")set Num%%i=!T!!Num%%i!set "Num%%i=0000000!Num%%i:~-%Len%!" ) for /L %%a in (!len2! 1 !Len1!) do (set "str=!L!!str!!str1:~%%a,1!"set "str=!str:~-%Len%!"if "!str!" geq "!str2!" (set M=1&set i=0000000!str!for /l %%i in (2 1 9) do if "!i!" geq "!Num%%i!" set "M=%%i"set sun=!sun!!M!&set str=&set T=0for %%i in (!M!) do (for /l %%j in (8 8 !Len_i!) do (set /a "T=3!i:~-%%j,8!-1!Num%%i:~-%%j,8!-!T:~,1!%%2"set "str=!T:~1!!str!"))) else set sun=!sun!0 ) if defined u if "%u%" gtr "0" set sun=!sun:~,-%u%!.!sun:~-%u%! endlocal&set %3=%d%%sun% echo 总大小: %quot% GB