China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-02 10:22
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Please tell me how to store files according to the content of text files? View 1,387 Replies 8
Original Poster Posted 2006-11-03 01:46 ·  中国 陕西 西安 电信
新手上路
Credits 10
Posts 4
Joined 2006-11-03 00:32
19-year member
UID 69347
Gender Male
Status Offline
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!
Floor 2 Posted 2006-11-03 05:58 ·  中国 北京 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
21-year member
UID 40733
Gender Male
Status Offline
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.
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 3 Posted 2006-11-03 07:10 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Same as the person above, I didn't understand exactly what kind of goal you are trying to achieve.
Floor 4 Posted 2006-11-03 22:05 ·  中国 陕西 西安 电信
新手上路
Credits 10
Posts 4
Joined 2006-11-03 00:32
19-year member
UID 69347
Gender Male
Status Offline
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


001, Ah Yi, Class 1
002, Ah Er, Class 1
003, Ah San, Class 2
004, Ah Si, Class 3


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~
Floor 5 Posted 2006-11-03 23:15 ·  中国 北京 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
21-year member
UID 40733
Gender Male
Status Offline
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
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +2 2006-11-04 03:39
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 6 Posted 2006-11-04 02:43 ·  中国 陕西 西安 电信
新手上路
Credits 10
Posts 4
Joined 2006-11-03 00:32
19-year member
UID 69347
Gender Male
Status Offline
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哦
Floor 7 Posted 2006-11-04 04:15 ·  中国 北京 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
21-year member
UID 40733
Gender Male
Status Offline
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
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 8 Posted 2006-11-04 04:39 ·  中国 陕西 西安 电信
新手上路
Credits 10
Posts 4
Joined 2006-11-03 00:32
19-year member
UID 69347
Gender Male
Status Offline
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 ]
Floor 9 Posted 2006-11-04 06:24
中级用户
★★
DOS之日
Credits 337
Posts 161
Joined 2006-11-04 05:27
19-year member
UID 69523
Gender Male
Status Offline
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.
Forum Jump: