中国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-09-24 22:53
47,813 topics / 349,918 posts / today 1 new / 48,274 members
DOS批处理 & 脚本技术(批处理室) » How to batch change the file extensions of txt in multiple subdirectories.
Printable Version  2,664 / 22
Floor1 zzhh612 Posted 2007-03-30 13:04
中级用户 Posts 103 Credits 278
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 ]
Floor2 flyinspace Posted 2007-03-30 13:14
银牌会员 Posts 517 Credits 1,206
Oh. It's very simple. .
Floor3 flyinspace Posted 2007-03-30 13:16
银牌会员 Posts 517 Credits 1,206
@echo off
dir /b /a *.txt>1.txt
for /f "tokens=1,2 delims=. " %%i in (1.txt) do ren %%i.txt %%i.c
Floor4 MagiCube Posted 2007-03-30 13:31
新手上路 Posts 2 Credits 4
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.
Floor5 flyinspace Posted 2007-03-30 13:32
银牌会员 Posts 517 Credits 1,206
Hehe, got muddled in the head.
Floor6 zzhh612 Posted 2007-03-30 13:43
中级用户 Posts 103 Credits 278
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 ]
Floor7 MagiCube Posted 2007-03-30 14:00
新手上路 Posts 2 Credits 4
Floor8 flyinspace Posted 2007-03-30 14:08
银牌会员 Posts 517 Credits 1,206
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.
Floor9 flyinspace Posted 2007-03-30 14:10
银牌会员 Posts 517 Credits 1,206
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.
Floor10 flyinspace Posted 2007-03-30 14:13
银牌会员 Posts 517 Credits 1,206
Oh. The initial requirements didn't include subdirectories. So there was no addition of the /s parameter.
Floor11 flyinspace Posted 2007-03-30 14:15
银牌会员 Posts 517 Credits 1,206
@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.
Floor12 zzhh612 Posted 2007-03-30 14:48
中级用户 Posts 103 Credits 278
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 ]
Floor13 flyinspace Posted 2007-03-30 15:06
银牌会员 Posts 517 Credits 1,206
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?
Floor14 bjsh Posted 2007-03-30 21:18
银牌会员 Posts 621 Credits 2,000
Why does flyinspace's work and rest schedule have reversed day and night?
Floor15 zzhh612 Posted 2007-03-31 01:39
中级用户 Posts 103 Credits 278
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.
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023