写bat脚本,检索目录及子目录下的ZIP或RAR文件,如果目录下同时含有后缀为为azw3,epub,mobi文件,都移动到检索根目录下
时间: 2024-01-09 21:05:45 浏览: 24
可以使用下面的代码实现这个功能:
```
@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 的文件,就将该文件移动到指定目录。
相关问题
写bat脚本,检索目录及子目录下的ZIP或RAR文件,如果一个文件夹下同时含有后缀为为azw3,epub,mobi文件,都移动到检索根目录下
可以使用下面的代码实现这个功能:
```
@echo off
setlocal enabledelayedexpansion
set root_dir=C:\test
set move_dir=%cd%
for /d /r "%root_dir%" %%d in (*) do (
set has_azw3=false
set has_epub=false
set has_mobi=false
for /r "%%d" %%f in (*.azw3) do set has_azw3=true
for /r "%%d" %%f in (*.epub) do set has_epub=true
for /r "%%d" %%f in (*.mobi) do set has_mobi=true
if !has_azw3! == true if !has_epub! == true if !has_mobi! == true (
move "%%d" "%move_dir%"
)
)
```
其中,`root_dir` 表示要检索的目录,`move_dir` 表示要移动文件夹到的目录。`for /d /r` 命令可以遍历目录及子目录下的文件夹,`set` 命令用于设置变量,`if` 命令用于判断变量是否为 true。如果一个文件夹下同时含有后缀为 azw3、epub、mobi 的文件,就将该文件夹移动到指定目录。注意,这里只移动文件夹,不会移动其中的文件。如果需要移动文件夹及其中的文件,可以使用上一个回答中的代码。
编写bat脚本,设置检索根目录为当前脚本所在目录,遍历文件夹和子文件夹。如果同时含有后缀为azw3,epub,mobi文件,就将这个文件夹移动到 F:\百度网盘资源\何博传教授传\电子书
可以使用以下的代码实现:
```bat
@echo off
setlocal enabledelayedexpansion
set "root_folder=%~dp0"
set "target_folder=F:\百度网盘资源\何博传教授传\电子书"
for /r "%root_folder%" %%a in (*) do (
set "azw3="
set "epub="
set "mobi="
if "%%~xa" == ".azw3" set "azw3=1"
if "%%~xa" == ".epub" set "epub=1"
if "%%~xa" == ".mobi" set "mobi=1"
if defined azw3 if defined epub if defined mobi (
echo Moving %%~dpa to "%target_folder%"
move "%%~dpa" "%target_folder%"
)
)
echo Done.
pause
```
将 `target_folder` 替换为你想要移动到的目标文件夹。这个脚本会将检索根目录设置为当前脚本所在的目录,然后遍历该目录下的所有文件和子文件夹,判断每个文件是否是后缀名为 `.azw3`、`.epub` 或 `.mobi` 的文件。如果一个文件夹内同时含有这三种类型的文件,那么这个文件夹会被移动到 `target_folder` 中。