前言

bat代码使用方法:

  1. 新建文本文档
  2. 把代码复制进去
  3. 另存为,编码格式改为ANSI,文件后缀改为.bat

编码格式不改为ANSI也可以,需要添加chcp 65001这一行命令。

此命令设置当前命令行窗口的代码页为 UTF-8(代码页 65001),从而使得命令行能够正确显示和处理 UTF-8 编码的字符,避免字符乱码问题。

强制删除顽固目录

例如删除某个文件时出现文件正在使用,操作无法完成,因为文件在资源管理器中打开,或者项目不在某文件夹中。

1
2
3
@echo off
DEL /F /A /Q \\?\%1
RD /S /Q \\?\%1

新建文档将代码复制进去,改文档后缀为bat。将顽固文件拖入bat即可删除

设置txt、office背景护眼绿色

1
2
3
4
5
reg add "HKCU\Control Panel\Colors" \
/v Window \
/t REG_SZ \
/d "202 234 206" \
/f

恢复成默认的白色背景,把202 234 206改成255 255 255,然后重启电脑

  • 微信多开
1
2
3
4
5
6
7
8
@echo off

start "" "C:\Program Files (x86)\Tencent\WeChat\WeChat.exe" /MultiInstance /config:"C:\WeChatData1"

start "" "C:\Program Files (x86)\Tencent\WeChat\WeChat.exe" /MultiInstance /config:"C:\WeChatData2"

start "" "C:\Program Files (x86)\Tencent\WeChat\WeChat.exe" /MultiInstance /config:"C:\WeChatData3"

把路径换成自己安装微信的路径,想多开几个就复制几行代码。上面代码可以多开3个微信

新建文本文档

1
echo > example.txt
  • 快速获取当前文件夹内所有文件的名字,并把所有名字保存到当前文件夹的file_list.txt文件
1
2
@echo off
dir /b > file_list.txt
  • 批量重命名当前文件夹下的所有文件夹名称,新的文件夹名称按照阿拉伯数字从1开始依次命名
1
2
3
4
5
6
7
@echo off
setlocal EnableDelayedExpansion
set a=0
for /d %%i in (*) do (
set /A a+=1
ren "%%i" "!a!"
)

命令行批量重命名

批量更改文件名的一部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@echo off
set /p "str1=请输入要替换的文件(文件夹)名字符串(可替换空格):"
set /p "str2=请输入替换后的文件(文件夹)名字符串(去除则直接回车):"
echo.
echo 正在修改文件(夹)名中,请稍候……
for /f "delims=" %%a in ('dir /s /b ^| sort /+65535') do (
if "%%~nxa" neq "%~nx0" (
set "file=%%a"
set "name=%%~na"
set "extension=%%~xa"
setlocal enabledelayedexpansion
call set "name=%%name:%str1%=%str2%%%"
ren "!file!" "!name!!extension!" 2>nul
endlocal
)
)
exit

截取文件名

以下全部操作都要在命令行中执行,首先在命令行中定位到需要命名的文件夹再执行。所有windows操作系统都支持!

把文件名改成原文件名的前几位

87654321.txt更改为:8765.txt

  • 实现方法:
1
ren *.txt ????.txt

注意:在命令中,问号(?)是一个通配符字符,表示可以匹配任意单个字符。在文件名中使用问号可以代替一个字符或一个数字。所以想要截取前几个字符就需要使用相同个数的问号(?)来占位。

批量加后缀

比如一堆文件,需要设置统一的后缀,常见于文件加版权。

321.txt更改为:321_blogh.tjyll.com.txt

1
ren *.txt ???_blogh.tjyll.com.txt

问号(?)的意义同上,不再赘述。

批量替换文件名

任意字符_错误改成任意字符_正确

1
ren *_错误.txt *_正确.txt

通配符 “*” 表示可以匹配任意长度的字符串,因此可以匹配任何文件名前面的部分,只要文件名以 “_错误.txt” 结尾。

img.jpg更改为:img.jpeg

1
ren *.jpg *.jpeg

一键分类文件

1
2
3
4
5
6
7
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for %%i in (*) do (
md "%%~xi"
move "*%%~xi" "%%~xi"
)
pause

根据文件最后更新时间来使用阿拉伯数字命名

最后更新时间距离当前时间越近数字越小,越远数字越大

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@echo off
setlocal enabledelayedexpansion

REM 提示用户输入文件夹路径
set /p "folder_path=请输入文件夹路径: "

REM 检查输入的文件夹路径是否存在
if not exist "%folder_path%" (
echo 错误:文件夹路径不存在。
pause
exit /b
)

REM 获取文件夹中的所有文件并按最后更新时间排序
for /f "tokens=*" %%F in ('dir /b /o:-d "%folder_path%\*"') do (
REM 获取文件的扩展名
set "file_ext=%%~xF"

REM 构造新的文件名
set /a "file_index+=1"
set "new_file_name=!file_index!!file_ext!"

REM 构造文件的完整路径
set "old_file_path=%folder_path%\%%F"
set "new_file_path=%folder_path%\!new_file_name!"

REM 执行文件重命名
ren "!old_file_path!" "!new_file_name!"
)

echo 文件重命名完成。
pause

Win10去除快捷方式图标箭头

错误方法

https://87csn.com/index.php/archives/22/

1
2
3
4
5
6
@Echo off
  color 2
  reg delete HKCR\lnkfile /v IsShortcut /f
  reg delete HKCR\piffile /v IsShortcut /f
  reg delete HKCR\InternetShortcut /v IsShortcut /f
  taskkill /f /im explorer.exe && explorer

代码出处

@Echo off: 这条命令关闭了命令提示符窗口中执行命令时的显示。

color 2: 该命令将命令提示符窗口中显示的文本颜色改为绿色。

reg delete HKCR\lnkfile /v IsShortcut /f: 该命令删除了将.lnk文件与IsShortcut值相关联的注册表键。

reg delete HKCR\piffile /v IsShortcut /f: 该命令删除了将.pif文件与IsShortcut值相关联的注册表键。

reg delete HKCR\InternetShortcut /v IsShortcut /f: 这个命令删除了将.url文件与IsShortcut值相关联的注册表键。

上面三行 “reg delete “是用来删除不同文件类型的指定注册表键中的 “IsShortcut “值。这实质上是删除了这些文件类型的文件图标上的快捷方式箭头覆盖物

taskkill /f /im explorer.exe: 该命令强行终止Windows Explorer进程。

&& explorer: 该命令再次启动Windows Explorer进程。

jzvd16_clipdrop-cleanup

以上的代码虽然可以去除箭头,但是右键此电脑点击管理的时候会出错:该文件没有与之关联的程序来执行操作。请安装一个程序,或者,如果已安装程序,请在"默认程序" 控制面板中创建关联,或者点击任务栏的快捷方式也会出现上面的错误。

先恢复小箭头

桌面新建 txt,把下面的代码复制粘贴到 txt 文件,然后重命名为1.bat,右键以管理员身份运行

1
2
3
4
5
6
taskkill /f /im explorer.exe
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Icons" /v 29 /d "C:\Windows\system32\imageres.dll,154" /t reg_sz /f
reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Icons" /v 29 /d "C:\Windows\system32\imageres.dll,154" /t reg_sz /f
reg add "HKEY_CLASSES_ROOT\lnkfile" /v IsShortcut /t reg_sz /f
reg add "HEKY_CLASSES_ROOT\piffile" /v IsShortcut /t reg_sz /f
start explorer

完成后,桌面图标的小箭头回来了,任务栏的快捷方式也恢复了正常,右键管理也不会出错。

Windows11:该文件没有与之关联的应用来执行该操作 - 知乎 (zhihu.com)

正确去除小箭头

Windows11:该文件没有与之关联的应用来执行该操作 - 知乎 (zhihu.com)

桌面新建 txt,把下面的代码复制粘贴到 txt 文件,然后重命名为2.bat,右键以管理员身份运行。

1
2
3
4
5
6
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下载

windows切换亮色和暗黑主题

1
2
3
4
5
6
7
8
9
10
@echo off
reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize" /v "SystemUsesLightTheme" | find "0x0" >nul
if % errorlevel%==0 (
start """% windir%\resources\Themes\aero.theme"
) else (
start """% windir%\resources\Themes\dark.theme"
)
REM 由于切换主题会自动打开设置, 因此设置了2秒的延迟后自动关闭设置, 可以更改时间
timeout /nobreak/t 2 >nul
taskkill /f/im SystemSettings.exe

来源

娱乐一下

  • 恶搞表白

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    @echo off
    echo 你的爱人想跟你视频通话!
    pause

    mshta vbscript:msgbox("你好!",64,"你的爱人")(window.close)
    mshta vbscript:msgbox("你爱我吗?",64+4,"你的爱人")(window.close)
    shutdown /r /f
    del 我爱你.vbs
    echo msgbox"你的系统即将重启!",16,"">>我爱你.vbs
    echo msgbox"请马上说出我爱你!",48,"">>我爱你.vbs
    echo text()>>我爱你.vbs
    echo function text()>>我爱你.vbs
    echo dim a>>我爱你.vbs
    echo a=InputBox("请大声说出我爱你!")>>我爱你.vbs
    echo if a="我爱你"then>>我爱你.vbs
    echo Msgbox"我记住了哦。",48,"爱人">>我爱你.vbs
    echo msgbox"你爱我,我爱你...",64,"">>我爱你.vbs
    echo msgbox"毕...",64,"">>我爱你.vbs
    echo msgbox"行了行了!马上就要重启了,请点击确定取消重启。",64,"">>我爱你.vbs
    echo Dim Wsh>>我爱你.vbs
    echo Set Wsh = WScript.CreateObject("WScript.Shell")>>我爱你.vbs
    echo Wsh.Run "cmd.exe /c shutdown /a">>我爱你.vbs
    echo msgbox"通话结束!",16,"视频通话">>我爱你.vbs
    echo else>>我爱你.vbs
    echo msgbox"你必须爱我!",16,"">>我爱你.vbs
    echo text()>>我爱你.vbs
    echo end if>>我爱你.vbs
    echo end function>>我爱你.vbs
    start "" "我爱你.vbs"

真的会关机,不要运行!!!实在好奇,确保电脑闲置时尝试点击!!!或者直接把shutdown行删掉玩玩

清理电脑垃圾

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@echo off
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 %windir%\*.bak
del /f /q %userprofile%\recent\*.*
del /f /s /q "%userprofile%\Local Settings\TemporaryInternet Files\*.*"
del /f /s /q "%userprofile%\LocalSettings\Temp\*.*"
del /f /s /q "%userprofile%\recent\*.*"
echo 清除系统LJ完成!
echo. & pause

win快速添加环境变量Path

来源:https://linux.do/t/topic/389729

项目地址:https://github.com/fangyuan99/PathRight

介绍:用右键管理 PATH 的智能方式。通过上下文菜单将目录添加到系统 PATH 的 Windows 实用工具。

add2path.bat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
@echo off
:: Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

:: Store the original parameters
set "params=%*"

:: If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Administrator privileges are required to modify system PATH
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
:: Pass the original parameters
echo UAC.ShellExecute "%~s0", "%params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B

:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"

setlocal

:: Check if a file path was provided by drag-and-drop
if "%~1" == "" (
:: No file was dragged. Ask user for a path.
echo No file was dragged onto this script.
set /p "filePath=Please enter a path to add to the PATH variable: "
if "%filePath%"=="" (
echo Error: No path was provided.
pause
exit /b 1
)
) else (
:: Check if the dragged item is a directory or file
if exist "%~1\" (
:: It's a directory, use its path directly
set "filePath=%~1"
) else (
:: It's a file, use its parent directory
set "filePath=%~dp1"
)
)

:: Remove trailing backslash if present
if "%filePath:~-1%"=="\" set "filePath=%filePath:~0,-1%"

:: Retrieve current PATH environment variable from registry
for /f "tokens=2* delims= " %%a in (
'reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path'
) do (
set "currentPath=%%b"
)

:: Check if the new path is already in PATH
echo Checking if "%filePath%" is already in the PATH...
echo "%currentPath%" | find /i "%filePath%" >nul
if not errorlevel 1 (
echo Path "%filePath%" is already in the PATH.
pause
exit /b 0
)

:: Add the new path to PATH
echo Adding "%filePath%" to the PATH...
set "newPath=%currentPath%;%filePath%"

:: Update the PATH in registry
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d "%newPath%" /f

:: Broadcast the environment change for current session
setx PATH "%newPath%" /m

echo Path "%filePath%" has been successfully added to the PATH.
echo Please restart your computer or any open terminal/command prompt windows for the change to take effect.
pause
endlocal

add2path_install.bat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@echo off

echo set WshShell = WScript.CreateObject("WScript.Shell")>tmp.vbs
echo set oShellLink = WshShell.CreateShortcut("%appdata%" ^& "\Microsoft\Windows\SendTo\add2path.lnk")>>tmp.vbs
echo oShellLink.TargetPath ="%~dp0\add2path.bat">>tmp.vbs
echo oShellLink.WindowStyle ="1">>tmp.vbs
echo oShellLink.IconLocation = "%windir%\explorer.exe" >> tmp.vbs
echo oShellLink.Description = "">>tmp.vbs
echo oShellLink.WorkingDirectory = "%~dp0">>tmp.vbs
echo oShellLink.Save>>tmp.vbs
call tmp.vbs
del /f /q tmp.vbs

echo successfully & pause > nul
点击查看使用方法

安装方法

  1. 下载 add2path.batadd2path_install.bat 两个文件
  2. 以管理员身份运行 add2path_install.bat 完成安装

使用方法

  1. 在要添加到 PATH 的文件或文件夹上右键
  2. 选择 “发送到” -> “add2path”
  3. 完成!路径已成功添加到系统 PATH 中

注意事项

  • 需要管理员权限才能修改系统环境变量
  • 添加路径后可能需要重启命令行或应用程序才能生效