@echo off set current_path=%cd% set file_type1=*.prt *.pdf set file_type2=.cxp set file_type3=.doc set folder_name1=程序模型 set folder_name2=工艺规程 set folder_name3=沟通记录 set destination_folder=工艺文件 if not exist "%folder_name1%" mkdir "%folder_name1%" if not exist "%folder_name2%" mkdir "%folder_name2%" if not exist "%folder_name3%" mkdir "%folder_name3%" for /r "%current_path%" %%i in (*) do ( if "%%~xi" == "%file_type1%" ( copy "%%i" "%folder_name1%\" ) else if "%%~xi" == "%file_type2%" ( copy "%%i" "%folder_name2%\" ) else if "%%~xi" == "%file_type3%" ( copy "%%i" "%folder_name3%\" ) ) xcopy "%folder_name1%" "%destination_folder%\%folder_name1%\" /s /e rd "%folder_name1%" /s /q xcopy "%folder_name2%" "%destination_folder%\%folder_name2%\" /s /e rd "%folder_name2%" /s /q xcopy "%folder_name3%" "%destination_folder%\%folder_name3%\" /s /e rd "%folder_name3%" /s /q 修改以上代码实现可以指定多个遍历的文件夹名称
时间: 2023-07-02 12:21:53 浏览: 130
inner_gear_prt.rar_UG_UG二次开发_UG参数化_gear .prt_参数化 prt
可以通过在代码中添加一个数组来实现指定多个遍历的文件夹名称。下面是修改后的代码:
```
@echo off
setlocal
set "current_path=%cd%"
set "file_type1=*.prt *.pdf"
set "file_type2=.cxp"
set "file_type3=.doc"
set "folder_name1=程序模型"
set "folder_name2=工艺规程"
set "folder_name3=沟通记录"
set "destination_folder=工艺文件"
set "folders=文件夹1 文件夹2 文件夹3"
REM 遍历指定的多个文件夹,查找指定类型的文件并移动到对应的文件夹中
for %%f in (%folders%) do (
if not exist "%%~f" mkdir "%%~f"
for /r "%%~f" %%i in (*) do (
if "%%~xi" == "%file_type1%" (
move "%%~fi" "%%~dpif%folder_name1%\"
) else if "%%~xi" == "%file_type2%" (
move "%%~fi" "%%~dpif%folder_name2%\"
) else if "%%~xi" == "%file_type3%" (
move "%%~fi" "%%~dpif%folder_name3%\"
)
)
)
REM 将三个文件夹复制到目标文件夹中
xcopy "%current_path%\%folder_name1%" "%destination_folder%\%folder_name1%\" /s /e
rd "%current_path%\%folder_name1%" /s /q
xcopy "%current_path%\%folder_name2%" "%destination_folder%\%folder_name2%\" /s /e
rd "%current_path%\%folder_name2%" /s /q
xcopy "%current_path%\%folder_name3%" "%destination_folder%\%folder_name3%\" /s /e
rd "%current_path%\%folder_name3%" /s /q
endlocal
```
在这个修改后的代码中,我们使用了一个名为 `folders` 的数组来存储要遍历的文件夹名称。在遍历文件夹时,我们使用了一个嵌套的 for 循环,第一个循环遍历 `folders` 数组中的每个文件夹,第二个循环遍历每个文件夹中的文件,并根据文件类型移动到对应的文件夹中。在复制和删除文件夹时,我们使用了 `%current_path%` 变量和 `%folder_name%` 变量来构建完整的路径。
阅读全文