中国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-08-11 10:46
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » [fixed] Rename all TXT files in a folder using the first line of text as the filename
Printable Version  981 / 8
Floor1 fengch1 Posted 2009-02-09 20:27
新手上路 Posts 5 Credits 6
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 ]
Floor2 netbenton Posted 2009-02-09 21:52
银牌会员 Posts 752 Credits 1,916 From 广西
Floor3 fengch1 Posted 2009-02-10 18:57
新手上路 Posts 5 Credits 6
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 ]
Floor4 netbenton Posted 2009-02-10 22:19
银牌会员 Posts 752 Credits 1,916 From 广西
Floor5 fengch1 Posted 2009-02-11 18:09
新手上路 Posts 5 Credits 6
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 ]
Floor6 tireless Posted 2009-02-11 18:32
银牌会员 Posts 1,122 Credits 2,025
Code 1:


Code 2:


[ Last edited by tireless on 2009-2-11 at 18:39 ]
Floor7 netbenton Posted 2009-02-11 20:33
银牌会员 Posts 752 Credits 1,916 From 广西
@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
Floor8 fengch1 Posted 2009-02-14 15:13
新手上路 Posts 5 Credits 6
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~
Floor9 HAT Posted 2009-02-14 17:04
版主 Posts 5,017 Credits 9,023
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.
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023