如何使用PowerShell将指定前缀XML文件自动归类到对应目标文件夹
XML文件自动归类脚本方案
PowerShell实现(推荐,路径兼容性更好)
完全不需要复杂循环逻辑,直接为每个前缀编写独立移动规则即可,代码如下:
# 替换为你接收XML文件的源文件夹完整路径 $sourceDir = "C:\你的XML接收文件夹路径" # 每个前缀对应目标路径,直接修改/添加即可 # -Force 参数会自动覆盖同名文件,不需要可以删除 New-Item -ItemType Directory -Path "C:\Directory Path...\Peter" -Force Move-Item -Path "$sourceDir\1001*.xml" -Destination "C:\Directory Path...\Peter" -Force New-Item -ItemType Directory -Path "C:\Directory Path...\John" -Force Move-Item -Path "$sourceDir\1002*.xml" -Destination "C:\Directory Path...\John" -Force New-Item -ItemType Directory -Path "C:\你1003对应的文件夹路径" -Force Move-Item -Path "$sourceDir\1003*.xml" -Destination "C:\你1003对应的文件夹路径" -Force New-Item -ItemType Directory -Path "C:\你1004对应的文件夹路径" -Force Move-Item -Path "$sourceDir\1004*.xml" -Destination "C:\你1004对应的文件夹路径" -Force New-Item -ItemType Directory -Path "C:\你1005对应的文件夹路径" -Force Move-Item -Path "$sourceDir\1005*.xml" -Destination "C:\你1005对应的文件夹路径" -Force
将代码保存为后缀为.ps1的文件,双击即可执行一次,完成当前所有匹配文件的归类。
BAT批处理实现
如果习惯用批处理,同样可以用极简规则实现:
@echo off :: 替换为你接收XML文件的源文件夹完整路径 set "sourceDir=C:\你的XML接收文件夹路径" :: 每个前缀对应目标路径,直接修改/添加即可 if not exist "C:\Directory Path...\Peter\" md "C:\Directory Path...\Peter\" move /y "%sourceDir%\1001*.xml" "C:\Directory Path...\Peter\" if not exist "C:\Directory Path...\John\" md "C:\Directory Path...\John\" move /y "%sourceDir%\1002*.xml" "C:\Directory Path...\John\" if not exist "C:\你1003对应的文件夹路径\" md "C:\你1003对应的文件夹路径\" move /y "%sourceDir%\1003*.xml" "C:\你1003对应的文件夹路径\" if not exist "C:\你1004对应的文件夹路径\" md "C:\你1004对应的文件夹路径\" move /y "%sourceDir%\1004*.xml" "C:\你1004对应的文件夹路径\" if not exist "C:\你1005对应的文件夹路径\" md "C:\你1005对应的文件夹路径\" move /y "%sourceDir%\1005*.xml" "C:\你1005对应的文件夹路径\"
将代码保存为后缀为.bat的文件,双击即可执行一次。
持续自动运行设置
要实现无人值守自动归类,只需添加Windows计划任务即可:
- 按Win键搜索「任务计划程序」并打开
- 点击右侧「创建基本任务」,按提示设置任务名称,触发器选择「每天」
- 进入触发器高级设置,勾选「重复任务间隔」,选择你需要的检测间隔(比如1分钟、5分钟),持续时间选择「24小时」
- 操作页选择「启动程序」,浏览选择你之前保存的
.ps1或.bat脚本文件即可 - 最后勾选「打开属性对话框」,在属性页的安全选项中勾选「不管用户是否登录都要运行」,即可后台自动执行
内容的提问来源于stack exchange,提问作者Squary94
相关产品推荐
相关产品推荐

