中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-02 11:39
48,038 topics / 350,123 posts / today 1 new / 48,251 members
DOS批处理 & 脚本技术(批处理室) » Please tell me how to store files according to the content of text files?
Printable Version  1,388 / 8
Floor1 bakeyhe Posted 2006-11-03 01:46
新手上路 Posts 4 Credits 10
Newcomer's first post, hope everyone supports.

Just entered the wonderful realm of batch processing operations, many things can't be realized only by my own level, so I come here to ask for help from everyone.

Specifically like this:

There are many files in a certain folder, and these files are all stored in different subdirectories
001_aa
001_bb
002
003
and so on.

At the same time, there is a txt file in this folder, the content is as follows
001, Name 1, Category 1
002, Name 2, Category 2
003, Name 3, Category 3

I now want to rename and re - store all files according to the content of this txt file.
For example, the file "001_aa" is renamed to "Name 1_aa" according to the requirements of txt, and a directory "Category 1" is established in the root directory, a directory "Name 1" is established under Category 1, and all files with 001_* are copied to the newly - created directory Name 1.

Don't know how to solve it, hope to get everyone's help, thank everyone!
Floor2 无奈何 Posted 2006-11-03 05:58
荣誉版主 Posts 356 Credits 1,338
I really want to help you, but I can't understand your description clearly. This might be the reason why no other friends have replied so far. Please list the directory with tree /f and post it, and also tell me in detail what kind of file names you want to change the final 001_aa, 001_bb... into.
Floor3 vkill Posted 2006-11-03 07:10
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
Same as the person above, I didn't understand exactly what kind of goal you are trying to achieve.
Floor4 bakeyhe Posted 2006-11-03 22:05
新手上路 Posts 4 Credits 10
Ai, my description ability is really poor...

I post the directory tree~

├─new
│ ├─Class 3
│ │ └─Ah Si
│ │ ├─Physics
│ │ │ Ah Si_Physics
│ │ │
│ │ ├─Mathematics
│ │ │ Ah Si_Mathematics
│ │ │
│ │ └─Chemistry
│ │ Ah Si_Chemistry
│ │
│ ├─Class 2
│ │ └─Ah San
│ │ ├─Physics
│ │ │ Ah San_Physics
│ │ │
│ │ ├─Mathematics
│ │ │ Ah San_Mathematics
│ │ │
│ │ └─Chemistry
│ │ Ah San_Chemistry
│ │
│ └─Class 1
│ ├─Ah Er
│ │ ├─Physics
│ │ │ Ah Er_Physics
│ │ │
│ │ ├─Mathematics
│ │ │ Ah Er_Mathematics
│ │ │
│ │ └─Chemistry
│ │ Ah Er_Chemistry
│ │
│ └─Ah Yi
│ ├─Physics
│ │ Ah Yi_Physics
│ │
│ ├─Mathematics
│ │ Ah Yi_Mathematics
│ │
│ └─Chemistry
│ Ah Yi_Chemistry

└─old
a.txt

├─Mathematics
│ 001
│ 002
│ 003
│ 004

├─Chemistry
│ 001
│ 002
│ 003
│ 004

└─Physics
001
002
003
004


Among them, the content of a.txt is as follows



I just hope to rename the files in old according to the requirements of a.txt, and create new folders, and then change to the saving method like new.

I don't know if this is clear now, heh heh~
Floor5 无奈何 Posted 2006-11-03 23:15
荣誉版主 Posts 356 Credits 1,338
Finally, I understand. The directory structure is a bit complicated, but after detailed testing, there are no problems.
If there are other subject directories, please add them yourself.


  1. @echo off
  2. md new
  3. for /f "tokens=1,2,3 delims=," %%a in (a.txt) do (
  4. md new\%%c\%%b
  5. for %%i in (Mathematics Chemistry Physics) do (
  6. echo F|xcopy /y %%i\%%a new\%%c\%%b\%%i\%%b_%%i
  7. )
  8. )
Helplessly posted on November 3, 2006 at 11:11
Floor6 bakeyhe Posted 2006-11-04 02:43
新手上路 Posts 4 Credits 10
Thanks to the moderator's help

But there is still a small problem. If the file extensions in the original folder are different, then how should this be done?
It seems that the moderator's code did not consider the issue of file extensions哦
Floor7 无奈何 Posted 2006-11-04 04:15
荣誉版主 Posts 356 Credits 1,338
This is written according to the example you provided on the fourth floor. So just change line 6 to:
echo F|xcopy /y %%i\%%a* new\%%c\%%b\%%i\%%b_%%i
Floor8 bakeyhe Posted 2006-11-04 04:39
新手上路 Posts 4 Credits 10
Thanks!
But I can run it normally only after changing it like this
echo F|xcopy /y %%i\%%a.* new\%%c\%%b\%%i\%%b_%%i.*
Hehe

By the way, I don't quite understand this line...
for /f "tokens=1,2,3 delims=," %%a in (a.txt) do (

I'm reading the tutorials on the forum, but I also hope someone can give me a little explanation~
Thanks again!


Supplement~ No wonder it didn't run just now...

for /f "tokens=1,2,3 delims=" %%a in (a.txt) do (

It was this comma... really in tears~
Haha
But now I found the reason

Continuing to study ing

[ Last edited by bakeyhe on 2006-11-4 at 05:04 AM ]
Floor9 hxuan999 Posted 2006-11-04 06:24
中级用户 Posts 161 Credits 337
for /f "tokens=1,2,3 delims=," %%a in (a.txt) do (
md new\%%c\%%b

Among them, delims=,is the delimiter set. Here it is the "," character.
tokens=1,2,3 can be in the form of tokens=x,y,m-n. x,y can be specified as numbers, and m-n is a range. That is, after splitting a line in a.txt with the "," character, the content of the specified position is passed to variables like %%a,%%b,%%c... later.
Of course, here it cannot exceed 26 letters. For example:
for /f "tokens=2,4,5-7,8* delims=," %%a in (tt.txt) do (
echo %%a %%b %%c %%d %%e %%f %%g)
In tt.txt, the content is:
aa,bb,cc,dd,ee,ff,gg,hh,ii,jj,kk
11,22,33,44,55,66,77,88,99,00,1010
Then the running result is:
bb dd ee ff gg hh ii,jj,kk
22 44 55 66 77 88 99,00,1010
"*" is to give all the content after 8 to %%g.
5-7 means 5, 6, 7 are the same.

I'm also just learning, please give advice.
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023