Winodws中的Bat批处理,优雅的文件执行处理方式。

批处理文件,在DOS和Windows(任意)系统中,.bat文件是可执行文件,由一系列命令构成,其中可以包含对其他程序的调用。这个文件的每一行都是一条DOS命令(大部分时候就好像我们在DOS提示符下执行的命令行一样),你可以使用DOS下的Edit或者Windows的记事本(notepad)等任何文本文件编辑工具创建和修改批处理文件。

收集的一些常用高效的Bat

  • 当前子目录文件移动到目录

    for /f "tokens=* delims=" %%i in ('dir /b /a-d /s "*"') do (move "%%i" "%%~dpi./../")
    
    pause
  • 添加其所在文件夹的名称前缀

    @echo off
    rem 在多个子文件夹里的jpg文件的名称前面/开头添加其所在文件夹的名称前缀
    mode con lines=3000
    
    set "folder=D:\天翼云盘下载\新建文件夹"
    if not exist "%folder%" (echo;"%folder%" no found&goto end)
    if "%folder:~-1%" equ "\" set "folder=%folder:~,-1%"
    for /f "delims=" %%a in ('dir /ad/b "%folder%\"') do (
        pushd "%folder%\%%~nxa\"
        for /f "delims=" %%b in ('dir /a-d/b *.jpg 2^>nul') do (
            echo;"%%~nxa\%%~nxb" --^> "%%~nxa_%%~nxb"
            ren "%%b" "%%~nxa_%%~nxb"
        )
        popd
    )
    :end
    echo;%#% +%$%%$%/%@% %z%
    pause
    exit
  • 文件新建文件夹并取名

    md tbwind
    for %%f in (*.*) do md tbwind\%%f
    for %%f in (*.*) do move %%f tbwind\%%f\
    xcopy tbwind\*.* /s
    rd tbwind /s/q
  • 去掉小箭头

    /* ---去掉小箭头----*/
    reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Icons" /v 29 /d "%systemroot%\system32\imageres.dll,197" /t reg_sz /f
    taskkill /f /im explorer.exe
    attrib -s -r -h "%userprofile%\AppData\Local\iconcache.db"
    del "%userprofile%\AppData\Local\iconcache.db" /f /q
    start explorer
    pause
    复制上面的代码。新建一个文本文件。粘贴后另存为.bat文件,然后以管理员身份打开。
    ok!
    /* ---恢复小箭头----*/
    reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Icons" /v 29 /f
    taskkill /f /im explorer.exe
    attrib -s -r -h "%userprofile%\AppData\Local\iconcache.db"
    del "%userprofile%\AppData\Local\iconcache.db" /f /q
    start explorer
    Pause
    同理,将上述代码另存为.bat文件,管理员身份打开。就可恢复小箭头了。
  • 目录中文件移出

    @echo off
    set son=%cd%
    [url=home.php?mod=space&uid=251666]@rem[/url] 获取上级目录,暂时没使用到
    pushd %son%
    cd ..
    set parent=%cd%
    popd
    
    for /d %%i in (*) do (
    move /y %son%\%%i\*.* %son%
    rd %son%\%%i
    )
    @Rem pause
  • 清理文件

    @echo off
    pause
    echo 正在清理系统垃圾文件,请稍等......
    del /f /s /q %systemdrive%\*.tmp
    del /f /s /q %systemdrive%\*._mp
    del /f /s /q %systemdrive%\*.log
    del /f /s /q %systemdrive%\*.gid
    del /f /s /q %systemdrive%\*.chk
    del /f /s /q %systemdrive%\*.old
    del /f /s /q %systemdrive%\recycled\*.*
    del /f /s /q %windir%\*.bak
    del /f /s /q %windir%\prefetch\*.*
    rd /s /q %windir%\temp & md %windir%\temp
    del /f /q %userprofile%\cookies\*.*
    del /f /q %userprofile%\recent\*.*
    del /f /s /q "%userprofile%\Local Settings\Temporary Internet Files\*.*"
    del /f /s /q "%userprofile%\Local Settings\Temp\*.*"
    del /f /s /q "%userprofile%\recent\*.*"
    echo 清理系统垃圾完成!
    echo. & pause
  • win10图片查看器

    Windows Registry Editor Version 5.00
    ; Change Extension's File Type 
    [HKEY_CURRENT_USER\Software\Classes\.jpg] 
    @="PhotoViewer.FileAssoc.Tiff"
    ; Change Extension's File Type 
    [HKEY_CURRENT_USER\Software\Classes\.jpeg] 
    @="PhotoViewer.FileAssoc.Tiff"
    ; Change Extension's File Type 
    [HKEY_CURRENT_USER\Software\Classes\.gif] 
    @="PhotoViewer.FileAssoc.Tiff"
    ; Change Extension's File Type 
    [HKEY_CURRENT_USER\Software\Classes\.png] 
    @="PhotoViewer.FileAssoc.Tiff"
    ; Change Extension's File Type 
    [HKEY_CURRENT_USER\Software\Classes\.bmp] 
    @="PhotoViewer.FileAssoc.Tiff"
    ; Change Extension's File Type 
    [HKEY_CURRENT_USER\Software\Classes\.tiff] 
    @="PhotoViewer.FileAssoc.Tiff"
    ; Change Extension's File Type 
    [HKEY_CURRENT_USER\Software\Classes\.ico] 
    @="PhotoViewer.FileAssoc.Tiff"
  • 多文件批量分成多个小文件夹

    @echo off&setlocal enabledelayedexpansion
    ::文件分类管理:按每5个文件装进一个文件夹整理。move为移动 copy为复制
    echo;&echo;    开始整理……&echo;
    set newf=0
    set numf=0
    set conf=0
    if not exist New!newf! md New!newf!
    for /f "tokens=*" %%a in ('dir /b /a-d') do (
        if !numf! geq 5 set numf=0&set /a newf+=1&md New!newf!
        if not "%%~na"=="%~n0" move "%%~a" New!newf!&set /a numf+=1
        set /a conf=!newf!*5+!numf!
    )
    echo;&echo;    整理完成,共 !conf! 个文件,请注意检查!&pause>nul
最后修改:2021 年 11 月 24 日
如果觉得我的文章对你有用,请随意赞赏