批处理文件,用于根据文件名将文件复制到相应的文件夹

时间:2013-11-23 14:55:59

标签: batch-file windows-7 copy

尝试创建一个批处理文件,该文件将根据描述将文件从一个文件夹复制到另一个文件夹。例如,如果文件名有六位数,我希望它读取前三位数并将其放在某个文件夹中,如果文件名只有五位数,那么我希望它读取前两位数并将该文件复制到一个不同的文件夹

文件704000.txt进入文件夹T704

文件70400.txt进入文件夹T70

1 个答案:

答案 0 :(得分:0)

试试这个:

@echo on & setlocal enabledelayedexpansion
goto start

the file structure should look simalar to this:

C:.
|   file.bat
|   
+---a
|       11111.txt
|       111111.txt
|       1111111.txt
|       22222.txt
|       222222.txt
|       33333.txt
|       333333.txt
|       44444.txt
|       444444.txt
|       55555.txt
|       555555.txt
|       66666.txt
|       666666.txt
|       77777.txt
|       777777.txt
|       88888.txt
|       888888.txt
|       99999.txt
|       999999.txt
|       
+---T11
+---T111
+---T22
+---T222
+---T33
+---T333
+---T44
+---T444
+---T55
+---T555
+---T66
+---T666
+---T77
+---T777
+---T88
+---T888
+---T99
\---T999

with this being file.bat

:start
set dir=a

for /f "tokens=*" %%i in ('dir %dir% /s /b') do (
   set filename=%%~ni
   set file=%%i
   if "!filename:~6!" EQU "" (if "!filename:~5!" NEQ "" ( move /Y "%%~i" "T!filename:~0,3!") else if "!filename:~4!" NEQ "" ( move /Y "%%~i" "T!filename:~0,2!"))
)