写bat脚本,检索目录及子目录下的所有zip或rar,全部解压,然后删掉压缩文件
时间: 2023-12-14 16:03:02 浏览: 28
您可以尝试以下代码:
```
@echo off
setlocal enabledelayedexpansion
set "searchPath=C:\your\search\path"
set "outputPath=C:\your\output\path"
for /r "%searchPath%" %%i in (*.zip *.rar) do (
set "file=%%i"
set "fileName=%%~ni"
set "fileExt=%%~xi"
set "outputFolder=!outputPath!\!fileName!"
echo Extracting "!file!" to "!outputFolder!"...
if not exist "!outputFolder!" mkdir "!outputFolder!"
7z x "!file!" -o"!outputFolder!" -y >nul
echo Deleting "!file!"...
del "!file!" >nul
)
echo All done!
pause
```
请将代码中的 `searchPath` 和 `outputPath` 变量替换为您实际的路径。此脚本使用 `7z` 命令行工具来解压缩文件,如果您没有安装该工具,则需要先下载并安装。
相关问题
写bat脚本,检索目录及子目录下的所有文件,都移动到检索根目录下
可以使用以下的bat脚本实现:
```
@echo off
setlocal enabledelayedexpansion
set "root=%~dp0" // 设置检索根目录为当前脚本所在目录
for /r "%root%" %%a in (*) do (
set "filePath=%%~fa"
set "fileName=%%~nxa"
move "!filePath!" "%root%!fileName!" > nul
)
echo 操作完成!
pause
```
解释:
1. `setlocal enabledelayedexpansion`:启用变量的延迟环境扩展,以便在循环中使用变量。
2. `set "root=%~dp0"`:将脚本所在目录赋值给变量 `root`。
3. `for /r "%root%" %%a in (*) do (...)`:递归遍历 `root` 及其子目录下的所有文件,并将每个文件的路径赋值给变量 `filePath`,将文件名赋值给变量 `fileName`。
4. `set "filePath=%%~fa"`:使用 `%%~fa` 获取文件的完整路径。
5. `set "fileName=%%~nxa"`:使用 `%%~nxa` 获取文件的名称及后缀。
6. `move "!filePath!" "%root%!fileName!" > nul`:将当前文件移动到根目录下,并将移动操作的输出重定向到空设备。
7. `echo 操作完成!`:在操作完成后输出提示信息。
8. `pause`:暂停脚本的执行,等待用户按下任意键退出。
写bat脚本,检索目录及子目录下的ZIP或RAR文件,如果目录下同时含有后缀为为azw3,epub,mobi文件,都移动到检索根目录下
可以使用下面的代码实现这个功能:
```
@echo off
setlocal enabledelayedexpansion
set root_dir=C:\test
set move_dir=%cd%
for /r "%root_dir%" %%f in (*.zip, *.rar) do (
set has_azw3=false
set has_epub=false
set has_mobi=false
for /r "%%~dpf" %%g in (*.azw3) do set has_azw3=true
for /r "%%~dpf" %%g in (*.epub) do set has_epub=true
for /r "%%~dpf" %%g in (*.mobi) do set has_mobi=true
if !has_azw3! == true if !has_epub! == true if !has_mobi! == true (
move "%%f" "%move_dir%"
)
)
```
其中,`root_dir` 表示要检索的目录,`move_dir` 表示要移动文件到的目录。`for /r` 命令可以遍历目录及子目录下的文件,`set` 命令用于设置变量,`if` 命令用于判断变量是否为 true。如果一个文件所在的目录同时含有后缀为 azw3、epub、mobi 的文件,就将该文件移动到指定目录。