问题描述
我有一个充满以这种方式命名的文件夹的目录:
I have a directory full of folders that are named in this manner:
ABC-L2-0001 __ 2ABC12345-0101 _xxxx
ABC-L2-0001__2ABC12345-0101_xxxx
我需要将许多以此方式命名的文件移动到与文件的前9个字符匹配的文件夹中:
I need to move a lot of files that are named in this manner to the folder that matches the first 9 characters of the files:
2ABC12345-0101 .xyxyxyx.yxyxyxyxy.model
2ABC12345-0101.xyxyxyx.yxyxyxyxy.model
这是我在阅读一些类似要求的旧帖子的基础上正在尝试的方法,但它对我不起作用.
Here's what I'm trying based on reading some older posts of similar requests and it isn't working for me.
:start
@echo off
setlocal enableDelayedExpansion
for /f "tokens=*" %%f in ('dir *.model /b') do (
set filename=%%f
set folder8=!filename:~13,9!
set "targetfolder="
for /f %%l in ('dir "!folder8!"*.* /a:d /b') do (
set targetfolder=%%l
)
if defined targetfolder move "!filename!" "!targetfolder!"
)
:end
任何帮助将不胜感激.
推荐答案
您交换了fileName和folderName的位置.您不会从文件名中获取前8个字符,而是从13,9个字符中获取字符,也不会在文件夹名的中间寻找这些字符,而要在开头.检查此固定代码:
You exchanged the positions of fileName and folderName. You don't take the first 8 characters from file name, but characters 13,9, and you don't look for these characters at middle of the folder name, but at the beginning. Check this fixed code:
:start
@echo off
setlocal enableDelayedExpansion
for /f "tokens=*" %%f in ('dir *.model /b') do (
set filename=%%f
set folder8=!filename:~0,9!
set "targetfolder="
for /f %%l in ('dir "?????????????!folder8!*" /a:d /b') do (
set targetfolder=%%l
)
if defined targetfolder move "!filename!" "!targetfolder!"
)
:end
您还应该知道for
和for /D
普通命令比for /F
与dir /B
命令结合使用更有效.
You also should know that for
and for /D
plain commands are more efficient than for /F
combined with dir /B
command.
:start
@echo off
setlocal enableDelayedExpansion
for %%f in (*.model) do (
set filename=%%f
set folder9=!filename:~0,9!
set "targetfolder="
for /D %%l in ("?????????????!folder9!*") do (
set targetfolder=%%l
)
if defined targetfolder move "!filename!" "!targetfolder!"
)
:end
这篇关于批处理文件将部分文件名移至部分文件夹名字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!