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-06-30 19:06
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to batch change the file extensions of txt in multiple subdirectories. View 2,278 Replies 22
Original Poster Posted 2007-03-30 13:04 ·  中国 四川 遂宁 电信
中级用户
★★
Credits 278
Posts 103
Joined 2006-10-21 21:08
19-year member
UID 67562
Gender Male
Status Offline
There are many C codes saved in text form in several subdirectories under a directory. How can I use a batch to change these code documents with the TXT extension into C source files with the.C extension?

[ Last edited by zzhh612 on 2007-3-31 at 01:48 AM ]
Floor 2 Posted 2007-03-30 13:14 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Oh. It's very simple. .
Floor 3 Posted 2007-03-30 13:16 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
@echo off
dir /b /a *.txt>1.txt
for /f "tokens=1,2 delims=. " %%i in (1.txt) do ren %%i.txt %%i.c
Floor 4 Posted 2007-03-30 13:31 ·  中国 广东 深圳 电信
新手上路
Credits 4
Posts 2
Joined 2007-03-30 13:04
19-year member
UID 83403
Gender Male
Status Offline
Under XP, you can use the command `ren *.txt *.c` to rename all `.txt` files in the current directory to `.c` files. Just arrived, seeing many experts here, and many complex grammars are not understood. Like this one, there should be no need to use complex methods.
Floor 5 Posted 2007-03-30 13:32 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Hehe, got muddled in the head.
Floor 6 Posted 2007-03-30 13:43 ·  中国 四川 遂宁 电信
中级用户
★★
Credits 278
Posts 103
Joined 2006-10-21 21:08
19-year member
UID 67562
Gender Male
Status Offline
I wonder if flyinspace has tried it. The directory listing should be correct with dir /b /s. This can write the file names into 1.txt, but when executing the file renaming, it will prompt that the path to the file is not found. Brother's code probably needs to be improved. Maybe the idea is correct.

The reason I want to use batch processing is that the text exists in multiple subdirectories of different levels. It is very troublesome to use the REN command alone. Of course, there are batch renaming software under WINDOWS. I want to learn batch processing in practice. I can't always rely on one method to solve the problem.

[ Last edited by zzhh612 on 2007-3-30 at 01:51 PM ]
Floor 7 Posted 2007-03-30 14:00 ·  中国 广东 深圳 电信
新手上路
Credits 4
Posts 2
Joined 2007-03-30 13:04
19-year member
UID 83403
Gender Male
Status Offline
Floor 8 Posted 2007-03-30 14:08 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Hehaha, I already said that.

I didn't pay attention...

I forgot to add a few parameters.
Also, that name can be written randomly.
It doesn't have to be .txt
You can just write something like .fly.
Floor 9 Posted 2007-03-30 14:10 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Have you tried it or not. The directory listing application should be dir /b /s. This can write the file name into 1.txt, but when performing the file renaming, it will prompt that the path of the file is not found. Brother's code may need to be improved. Maybe the idea is correct.

??
No way?
This dir /b /s can be written casually.
Writing dir /s /b is the same.
Floor 10 Posted 2007-03-30 14:13 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Oh. The initial requirements didn't include subdirectories. So there was no addition of the /s parameter.
Floor 11 Posted 2007-03-30 14:15 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
@echo off
dir /b /a /s *.txt>1.bpx
for /f "tokens=1,2 delims=. " %%i in (1.bpx) do ren %%i.txt %%i.c
Adding the /a parameter is to prevent the appearance of hidden files.
Floor 12 Posted 2007-03-30 14:48 ·  中国 四川 遂宁 电信
中级用户
★★
Credits 278
Posts 103
Joined 2006-10-21 21:08
19-year member
UID 67562
Gender Male
Status Offline
The code of flyinspace still prompts that the system cannot find the file when executing.
There is no way to rename, only the one in the root directory is renamed.
I got it by myself, and it worked after testing.

@echo off
FOR /R c:\test %%a IN (*.txt) DO (ren %%a %%~na.c)
pause

Just that the file name cannot have commas. Of course, this is not standard originally, so there is no idea to include it. If it must be included, only experts can solve it..

[ Last edited by zzhh612 on 2007-3-30 at 02:57 PM ]
Floor 13 Posted 2007-03-30 15:06 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Oh. I tried it myself?
Can all the files in all directories be changed?

To be the most standardized, a lot of judgment statements need to be added.

But do you want to be troublesome?
Floor 14 Posted 2007-03-30 21:18 ·  中国 浙江 杭州 华数宽带
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
Why does flyinspace's work and rest schedule have reversed day and night?
Floor 15 Posted 2007-03-31 01:39 ·  中国 四川 遂宁 电信
中级用户
★★
Credits 278
Posts 103
Joined 2006-10-21 21:08
19-year member
UID 67562
Gender Male
Status Offline
The code of flyinspace still prompts that the system cannot find the file when executed.
None of the TXT files in all subdirectories have been changed.

I don't know how you ran it successfully.
Forum Jump: