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-07-17 21:21
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Batch convert BAT format files to TXT format (please optimize) View 3,579 Replies 20
Original Poster Posted 2008-05-16 19:41 ·  中国 江苏 南京 电信
初级用户
Credits 24
Posts 11
Joined 2008-02-01 13:30
18-year member
UID 109956
Gender Male
Status Offline
Batch converting BAT format files to TXT format (batch renaming extensions)

Function: Batch convert BAT and CMD format batch files in the current directory to TXT format.
Purpose: Suitable for converting batch files and placing them on a mobile phone to view as TXT e-books. Since it is cumbersome to manually change the extensions when there are many files, I thought of using P to handle it. Here is the code:

@echo off
title bat2txt
echo Renaming all BAT files in the current directory to TXT
for /f "delims=" %%i in ('dir /b /l /a-d ^| findstr ".cmd$" ^& dir /b /a-d ^| findstr /i ".bat$"') do (
if not "%%~nxi"=="%~nx0" (
ren "%%~nxi" "%%~ni.txt" 2>nul || echo Ignored the operation on "%%i", there is "%%~ni.txt" in this directory
)
)
pause

The above code is not concise. Please provide a better solution. Try to make the code concise and user-friendly. It is best to add a function to restore to the original state after conversion. Of course, a backup function is even better. I hope experts can add some personalized functions such as adding a progress bar, displaying progress, and displaying processing details to make it more user-friendly. Thank you! Because my DOS is very shallow, I feel difficult even to solve small problems. Thank you.

[ Last edited by Batain on 2008-5-17 at 02:25 PM ]
拙拙的跟各位学习学习,增长学问。
Floor 2 Posted 2008-05-16 19:52 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
ren *.cmd *.txt
ren *.bat *.txt
Floor 3 Posted 2008-05-16 19:55 ·  中国 重庆 南岸区 电信
初级用户
Credits 45
Posts 19
Joined 2007-03-08 02:14
19-year member
UID 81032
Gender Male
Status Offline
ren *.bat *.txt
Floor 4 Posted 2008-05-16 19:55 ·  中国 重庆 南岸区 电信
初级用户
Credits 45
Posts 19
Joined 2007-03-08 02:14
19-year member
UID 81032
Gender Male
Status Offline
Floor 5 Posted 2008-05-17 14:30 ·  中国 江苏 苏州 张家港市 电信
初级用户
Credits 24
Posts 11
Joined 2008-02-01 13:30
18-year member
UID 109956
Gender Male
Status Offline
I'm sorry, I didn't make my question clear. I mean I need to solve a more optimized code for bat2txt. Because I posted this thread not just for the purpose of realizing the conversion, but to seek some more optimized and personalized solutions. The methods of the friends on the 2nd and 3rd floors are not suitable for writing into batch files, because they will even change their own suffixes, and the files that cannot be changed should also explain the reasons, otherwise it will cause misunderstandings. I hope you can give batch file codes with transparent functions and user-friendly operations for everyone to learn. We are willing to wait for the solutions from the experts at any time, but don't provide incomplete methods in a hurry. Thank you.

[ Last edited by Batain on 2008-5-17 at 02:38 PM ]
拙拙的跟各位学习学习,增长学问。
Floor 6 Posted 2008-05-17 14:43 ·  中国 江苏 苏州 张家港市 电信
初级用户
Credits 24
Posts 11
Joined 2008-02-01 13:30
18-year member
UID 109956
Gender Male
Status Offline
I can't understand what the friend on the fourth floor means. The CN-DOS forum is not a water-spamming forum. We believe everyone will read every reply carefully, not just the posts on the first page. I hope you don't have a guard against everyone. Believe us. If your answer is wonderful, we will collect it and we will remember you. After all, this is the first post I posted in this forum. I will be very happy if there is no reply, but I hate seeing irresponsible replies.

[ Last edited by Batain on 2008-5-17 at 02:45 PM ]
拙拙的跟各位学习学习,增长学问。
Floor 7 Posted 2008-05-17 14:46 ·  中国 重庆 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
It's not that someone else's method is imperfect, but that your problem isn't clearly described.
@echo off
for /f "tokens=1* delims=" %%a in ('dir /b *.bat *.cmd') do (
if "%%a" neq "%0" (
ren %%a %%~na.txt
)
)
Floor 8 Posted 2008-05-17 14:49 ·  中国 重庆 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
The friend on the 4th floor means that the method he thought of was posted by the 2nd floor friend first, and there's no other meaning. The thread starter doesn't need to mind. Wish the thread starter smooth study in this forum.
Floor 9 Posted 2008-05-17 15:47 ·  中国 重庆 沙坪坝区 电信
中级用户
★★
金牌会员
Credits 315
Posts 152
Joined 2007-01-25 23:55
19-year member
UID 77669
Gender Male
Status Offline
Are you from Chongqing, upstairs?
I'm from Shapingba, heh heh
Floor 10 Posted 2008-05-17 16:02 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Why put it in the same directory... Put it in the parent directory. You can add the path in the bat. That's simple... Or... Directly register a new type...

[ Last edited by slore on 2008-5-17 at 04:08 PM ]
Floor 11 Posted 2008-05-18 12:34 ·  中国 江苏 苏州 张家港市 电信
初级用户
Credits 24
Posts 11
Joined 2008-02-01 13:30
18-year member
UID 109956
Gender Male
Status Offline
Thanks for your sincerity! Also thanks to the friend on floor 7 for the method. It's the same dir command, and you used it concisely and flexibly. If there are many kinds of suffixes, it can be easily added. Also, may I ask everyone what the command to refresh the folder is? If there are hundreds or thousands of files whose suffixes need to be changed, it's best to change part of them first, then automatically refresh the folder, and then change the rest, so that friends with low machine configurations won't feel stuck.. Hehe, thanks you all!
拙拙的跟各位学习学习,增长学问。
Floor 12 Posted 2008-05-18 14:20 ·  中国 广西 百色 中移铁通
银牌会员
★★★
永远的菜鸟
Credits 1,335
Posts 574
Joined 2007-11-27 12:50
18-year member
UID 103929
Gender Male
From 广西
Status Offline
Use PING to pause. Pause for a few seconds every 10 times

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1* delims=" %%a in ('dir /b *.bat *.cmd' do (
if "%%a" neq "%0" (
ren %%a %%~na.txt
set /a n+=1
if "!n:~-1!"=="0" ping -n 5 127.1>nul
)
)
Floor 13 Posted 2008-05-18 15:54 ·  中国 重庆 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
I changed thousands of files at one time and didn't find it stuck. I think modifying in batches like this will反而affect the execution efficiency.

(注:原句中“反而”不需要翻译,直接保留英文形式即可,整体按照要求翻译)
Floor 14 Posted 2008-05-19 14:04 ·  中国 江苏 南京 电信
初级用户
Credits 24
Posts 11
Joined 2008-02-01 13:30
18-year member
UID 109956
Gender Male
Status Offline
Heheh! Thank you all. The method on floor 12 is very useful, but I don't understand the principle. I still don't know how to extend variables for delay. I will find materials to study when I have time; also, I found that the code styles you posted are very beautiful, with indentation and alignment very hierarchical. What editor are you using? Or do you indent manually with spaces or TABs?

Recently, I've been busy and haven't had time to focus on studying. I only come to the forum to check replies when I'm online. Friends from Sichuan and Chongqing on the upper floors, are your families all safe? I heard that there were too many shoddy houses in Sichuan during this earthquake. Now many builders cut corners, really there's no way.

[ Last edited by Batain on 2008-5-19 at 02:06 PM ]
拙拙的跟各位学习学习,增长学问。
Floor 15 Posted 2008-05-19 17:04 ·  中国 重庆 南岸区 电信
初级用户
Credits 45
Posts 19
Joined 2007-03-08 02:14
19-year member
UID 81032
Gender Male
Status Offline
Almost discouraged by the LZ...

I'll do another one


if "%1" equ "" exit
cd %1
ren *.cmd *.txt
ren *.bat *.txt


Save as a batch file, then drag the folder where the batch files to be renamed are located onto this batch file, so as to solve the problem of being renamed to txt yourself

Note that there should be no spaces in the path

I feel that using for is still inefficient... so it's better to just use ren directly

[ Last edited by gahoo on 2008-5-19 at 05:17 PM ]
Forum Jump: