cmd命令,bat脚本

1.cd /d 

D:\>cd mysql

D:\mysql>cd /d C:/TEMP

C:\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\python

3.windows下.bat每隔一段时间(例如15分钟)运行一次某python文件

https://blog.csdn.net/baidu_26408419/article/details/78885206?utm_source=blogxgwz0mysql

4.结束进程

taskkill /f /im Explorer.exe (关闭Explorer.exe进程) 算法

5.rem、@rem和::都是注释的关键字

通常就用@remsql

6.echo、echo off、@

1).echo表示显示此命令后的字符shell

echo 有这个echo就能够把后面的语句打印出来了
pause

 

2.)echo off表示在此语句后全部运行的命令都不显示命令行自己windows

echo 有这个echo就能够把后面的语句打印出来了
echo off
echo 有echo off后后面全部的命令都不显示命令行自己
pause

 


3)@与echo off相象,但它是加在每一个命令行的最前面,表示运行时不显示这一行的命令行(只能影响当前行)
缓存

@echo 有@当前行的命令不显示命令行自己
pause

 

 7.隐藏CMD运行窗口

if "%1" == "h" goto begin
mshta vbscript:createobject("wscript.shell").run("%~fs0 h",0)(window.close)&&exitbash

案列1:查看系统信息spa

@echo off
if "%1" == "h" goto begin
mshta vbscript:createobject("wscript.shell").run("%~fs0 h",0)(window.close)&&exit
:begin

@echo off
start devmgmt.msc
start dxdiag.exe
start taskmgr.exe  

案列2:打开网页,查看磁盘信息操作系统

@echo off
if "%1" == "h" goto begin
mshta vbscript:createobject("wscript.shell").run("%~fs0 h",0)(window.close)&&exit
:begin

@echo off
start www.ip138.com
start diskmgmt.msc

  

 

以前的资料:

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 2

set 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 2

set 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 2

set 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 2

echo.
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+=1
        set s=s%%a&set str%%i_!n!=0
        for %%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=0
           for %%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
相关文章
相关标签/搜索