batch-file - 根据部分文件名将文件移动到目录
问题描述
我有这样的文件名:
“Jeff W ACE #20495 外滩工作报告已提交 9 16 2020 -oOo-.msg”
“Miguel M ACE #21462 Ficus 工作报告已提交 9 15 2020 -oOo-.msg”
“桑迪 U ACE #6624 99 双胞胎交叉作业报告已提交 9 17 2020 -oOo-.msg”
我需要将它们移动到基于名称中 # 之后的 4 或 5 位数字的目录。我找到了一个批处理文件,但它需要在文件名中具有相同位置的目录名和相同的长度。
@echo off &setlocal
for /f "delims=" %%i in ('dir /b /a-d *.text') do (
set "filename1=%%~i"
setlocal enabledelayedexpansion
set "folder1=!filename1:~0,1!"
mkdir "!folder1!" 2>nul
move "!filename1!" "!folder1!" >nul
endlocal
)
如何修改此批处理文件以搜索 4 或 5 位数字,而不是数字必须位于名称中的固定位置?谢谢!
解决方案
我可能会使用以下代码来实现您的目标:
rem /* Loop through (unhidden) files whose names contain the sequence ` #`;
rem split the file names at the `#` character then (there should be only one);
rem the part before `#` is available in `%%E`, the part after `#` in `%%F`: */
for /F "tokens=1* eol=# delims=#" %%E in ('dir /B /A:-D-H-S "* #*.msg"') do (
rem // Split off the first space-separated word from the second name part:
for /F "eol= " %%G in ("%%~nF") do (
rem /* Use that word as the name of a sub-directory and create it
rem (` 2> nul` suppresses error messages when it already exists): */
md "%%G" 2> nul
rem /* Actually move the currently iterated file into the sub-directory
rem (there will appear a prompt when the target file already exists;
rem to avoid that, use the `/Y` option of `move` to force overwriting,
rem or put `if not exist "%%G\%%E#%%F" ` in front of `move` to avoid
rem overwriting; append ` > nul` to suppress text `1 file(s) moved.`): */
move "%%E#%%F" "%%G\"
)
)
推荐阅读
- r - 当我尝试对它们进行分组时,为什么会得到一个额外的类别?
- android - NativeScript Firebase Google 身份验证
- jsf - OrderList 中的复选框字段
- vb.net - 我无法在 vb.net 的 datagridview 中的按钮上发短信
- apache-spark - Spark SQL,不尊重 Dataframe 格式
- java - 为什么谷歌地图在android中不起作用(通过WebView)
- android-intent - 关于 goAsync 的困惑-BroadcastReceiver 进程在 10 秒内不会被杀死
- python - 如何询问 python 对象的结构?
- wpf - 基于变换(旋转等)考虑孩子边界框的控制
- javascript - 渲染 ListItem 元素显示空白