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-11 09:46
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [fixed] Rename all TXT files in a folder using the first line of text as the filename View 980 Replies 8
Original Poster Posted 2009-02-09 20:27 ·  中国 河南 焦作 电信
新手上路
Credits 6
Posts 5
Joined 2009-02-08 19:17
17-year member
UID 138345
Gender Male
Status Offline
I need all TXT files in a folder to be named using the text on the first line as the filename~

Note: before the first line of text, there are many blank lines. If the blank lines can be removed, that's also fine.

───────────────── Moderation record ─────────────────
Action by: HAT
Operation: Added search keywords to the thread title; added code tags to the code
Note: the original title "Can someone help write a text batch file" was not good for forum searching
Penalty: as a new forum user, exempted from credits penalty
Reminder: recommended to read the following threads
{1415}The Wisdom of Asking Questions
{7326}Must-read for forum newcomers, the basic code of conduct for everyone
{22703}Please don't be an impetuous person
{27314}Negative example teaching material: a small collection of “wonderful” thread titles from this forum
{32667}Anger: those of you who can't even write a clear title, wake up
{32825}This section is strictly cleaning up poor posts
───────────────── Moderation record ─────────────────


[ Last edited by fengch1 on 2009-3-7 at 12:29 ]
Floor 2 Posted 2009-02-09 21:52 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
zhengqian529 +2 2009-02-10 16:18
精简
=> 个人网志
Floor 3 Posted 2009-02-10 18:57 ·  中国 河南 焦作 电信
新手上路
Credits 6
Posts 5
Joined 2009-02-08 19:17
17-year member
UID 138345
Gender Male
Status Offline
Thanks to reply #2~
But~ it doesn't seem to work 'The naming syntax is incorrect'

Each of those text files has a lot of blank space at the beginning

[ Last edited by fengch1 on 2009-2-10 at 18:58 ]
Floor 4 Posted 2009-02-10 22:19 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
精简
=> 个人网志
Floor 5 Posted 2009-02-11 18:09 ·  中国 河南 焦作 电信
新手上路
Credits 6
Posts 5
Joined 2009-02-08 19:17
17-year member
UID 138345
Gender Male
Status Offline
Oh~~
Still doesn't work~
But it's much better
The renamed files don't have extensions~
And they're all the same repeated one~

[ Last edited by fengch1 on 2009-2-11 at 18:13 ]
Floor 6 Posted 2009-02-11 18:32 ·  中国 江西 赣州 电信
银牌会员
★★★★
Credits 2,025
Posts 1,122
Joined 2007-09-05 20:15
18-year member
UID 96653
Gender Male
Status Offline
Code 1:
@echo off
for %%a in (*.txt) do call :ren "%%a"
exit

:ren
for /f "tokens=*" %%a in (%~s1) do (
ren %1 "%%a.txt"
goto :eof
)


Code 2:
@echo off
setlocal enabledelayedexpansion
for %%a in (*.txt) do (
set /p new<"%%a"
ren "%%a" "!new!.txt"
)


[ Last edited by tireless on 2009-2-11 at 18:39 ]
Floor 7 Posted 2009-02-11 20:33 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
@echo off
:: Can handle long filenames
for /f "tokens=*" %%a in ('dir/a-d/b') do (
set "a=%%a"
setlocal enabledelayedexpansion
call :sub
endlocal
)
goto :eof

:sub

::avoid itself
if "!a!"=="%~nx0" goto :eof
::the if test is to avoid lines that contain only spaces and the [TAB] key
for /f "tokens=*" %%c in ('type "!a!"') do if not "%%c"=="" (ren "!a!" "%%c"&goto :eof)


::this way can handle most special cases
精简
=> 个人网志
Floor 8 Posted 2009-02-14 15:13 ·  中国 河南 焦作 电信
新手上路
Credits 6
Posts 5
Joined 2009-02-08 19:17
17-year member
UID 138345
Gender Male
Status Offline
Thanks. Although the text files still don't have extensions,
I have my own way to deal with it.
Problem solved.
Thank you very much~
Floor 9 Posted 2009-02-14 17:04 ·  中国 重庆 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
If someone helped you solve the problem, it is recommended to do the following:

1. Modify the thread title and mark it with:

2. Add credits to the person who answered your question correctly to show your thanks. Although everyone comes to the forum to learn rather than for credits, giving credits as a token of thanks can always, to some extent, give a little comfort to the person who spent time and energy helping you selflessly.

3. If you wrote the code yourself based on someone else's hints, please post your own method and share it with everyone. Maybe others can point out shortcomings in your code, tell you how to improve execution efficiency, etc. Only through discussion can you improve faster.
Forum Jump: