Rename text file based on content

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Serpent
Posts: 12
Joined: 08 Sep 2013 17:18

Re: Rename text file based on content

#16 Post by Serpent » 09 Sep 2013 03:40

i have a lot of txt file's 1.txt 2.txt ect.
but i have to rename them
and the name come's out of the file
example

1.txt

line 1 : // test file , month
line 2 bla bla bla
line 3
so 1.txt must be renamed in to " test file.txt "

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Rename text file based on content

#17 Post by foxidrive » 09 Sep 2013 04:50

Serpent wrote:i have a lot of txt file's 1.txt 2.txt ect.
but i have to rename them
and the name come's out of the file
example

1.txt

line 1 : // test file , month
line 2 bla bla bla
line 3
so 1.txt must be renamed in to " test file.txt "


If line one contains this

Code: Select all

:  // test file , month 


then we need to also know if every file 1.txt 2.txt etc also have ": // test file , month " as the first line.
Does the format of line one change at all? Is it always a full colon-space-//-space-two words-comma-space-month ?

Show some actual examples of the first line from a few of the files please.

Serpent
Posts: 12
Joined: 08 Sep 2013 17:18

Re: Rename text file based on content

#18 Post by Serpent » 09 Sep 2013 05:09

every file have a different first line but always start with //

// blue moon , black
// green , house
// stuppid car , beer
// road kill , me
// example , yep
// comp , tree

so the files must be renamed in

blue moon.txt
green.txt
stuppid car.txt
ect.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Rename text file based on content

#19 Post by foxidrive » 09 Sep 2013 05:39

Code: Select all

@echo off
>1.txt echo // blue moon , black
>2.txt echo // green , house
>3.txt echo // stuppid car , beer
>4.txt echo // road kill , me
>5.txt echo // example , yep
>6.txt echo // comp , tree

dir *.txt /b
pause
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir *.txt /b ') do (
set /p "var="<"%%a"
for /f "delims=/," %%b in ("!var!") do set "var=%%b"
ren "%%a" "!var:~1,-1!%%~xa"
)
dir *.txt /b
pause


Serpent
Posts: 12
Joined: 08 Sep 2013 17:18

Re: Rename text file based on content

#20 Post by Serpent » 09 Sep 2013 06:56

// blue moon , black
// green , house
// stuppid car , beer
// road kill , me
// example , yep
// comp , tree

is a example

i have around 5000 files with diferend line's
your script make a file named " blue moon.txt " with " // blue moon , black in it "
let me explain it again perhaps i did it wrong

i am need the script like the script from Aacini

but his script looks at line 3 and i need it from line 1 what ever there is standing

so the script has to look inside a txt file and look at the first line that always start with //
and after the name it always ends with ,
for example

line 1: // blue moon , black
line 2: 4 for the road
line 3: 2 people

there is no file the same after the //

the script look after the // and see " blue moon "
so he close te txt file and rename it to blue moon.txt or whatever there is to read

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Rename text file based on content

#21 Post by foxidrive » 09 Sep 2013 07:03

That's what my script does. Test it on some copies of your files.

Remove the portion that creates test files - that just proves that it works with the sample data that you provided.
It takes the first line of each file - and so I didn't need to add more lines to the sample text files.

Serpent
Posts: 12
Joined: 08 Sep 2013 17:18

Re: Rename text file based on content

#22 Post by Serpent » 09 Sep 2013 07:43

yep it is so , but he didnt work first
and somwhere if the file is bigger then he dont work , when i reduce the info then he works

when i use it on the next file he dont work

// Counter-Top , a Legacy

2 rwerwerwer
4 rwerwerwer
5 frwerwerwer
6 rwerwrwrwer
2 ferfefrfwerg
6 ferferferferf
8 dwedwewedwe

dont know wy
when i make the file smaller

// Counter-Top , a Legacy

the it works almost
he makes a file named

ounter-Top.txt

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Rename text file based on content

#23 Post by foxidrive » 09 Sep 2013 07:56

This works with the same code. How large are your files?

Code: Select all

@echo off

>1.txt echo // Counter-Top , a Legacy
>>1.txt echo.
>>1.txt echo 2 rwerwerwer
>>1.txt echo 4 rwerwerwer
>>1.txt echo 5 frwerwerwer
>>1.txt echo 6 rwerwrwrwer
>>1.txt echo 2 ferfefrfwerg
>>1.txt echo 6 ferferferferf
>>1.txt echo 8 dwedwewedwe

dir *.txt /b
pause
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir ?.txt /b ') do (
set /p "var="<"%%a"
for /f "delims=/," %%b in ("!var!") do set "var=%%b"
ren "%%a" "!var:~1,-1!%%~xa"
)
dir *.txt /b
pause


Serpent
Posts: 12
Joined: 08 Sep 2013 17:18

Re: Rename text file based on content

#24 Post by Serpent » 09 Sep 2013 13:56

the files are around 500/550 bytes

Serpent
Posts: 12
Joined: 08 Sep 2013 17:18

Re: Rename text file based on content

#25 Post by Serpent » 09 Sep 2013 15:41

Below is a file that i like to renamed it is a txt file and when i look at it it looks like this
i use your script and he renamed it in

Scalding Tarn.txt
in stat of
RUG.txt

most of the files look like this with "notepad"

//RUG, a Standard deck by Kyle Jessie2 Precursor Golem1 Avenger of Zendikar1 Oracle of Mul Daya4 Inferno Titan4 Lotus Cobra4 Lightning Bolt4 Mana Leak4 Preordain4 Explore1 Garruk Wildspeaker4 Jace, the Mind Sculptor3 Forest5 Island2 Mountain3 Copperline Gorge2 Halimar Depths4 Raging Ravine4 Misty Rainforest4 Scalding Tarn

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Rename text file based on content

#26 Post by foxidrive » 10 Sep 2013 00:06

That line doesn't conform to the layout you have described previously.

Are these text files?
Please show a few more of the actual first lines, of a few files.

Serpent
Posts: 12
Joined: 08 Sep 2013 17:18

Re: Rename text file based on content

#27 Post by Serpent » 10 Sep 2013 02:09

yes these are txt files

//Survival Zoo , a Legacy deck by Stork3 Qasali Pridemage3 Vengevine4 Fauna Shaman1 Loyal Retainers4 Mother of Runes4 Noble Hierarch4 Knight of the Reliquary4 Tarmogoyf1 Basking Rootwalla1 Iona, Shield of Emeria1 Gaddock Teeg4 Swords to Plowshares4 Survival of the Fittest3 Forest1 Plains4 Windswept Heath1 Verdant Catacombs1 Wooded Foothills3 Wasteland1 Misty Rainforest3 Horizon Canopy3 Savannah1 Karakas1 Gaea's Cradle

//Valakut Ramp , a Standard deck by MilanoLawyer3 Avenger of Zendikar4 Primeval Titan2 Inferno Titan4 Goblin Ruinblaster4 Overgrown Battlement4 Lightning Bolt4 Summoning Trap4 Cultivate4 Explore5 Forest12 Mountain4 Evolving Wilds2 Terramorphic Expanse4 Valakut, the Molten Pinnacle// Sideboard:SB: 4 Obstinate BalothSB: 3 Tunnel IgnusSB: 3 Acidic SlimeSB: 2 Ricochet TrapSB: 3 Pyroclasm


//Goblins! , a Standard deck by manachuel4 Memnite3 Tunnel Ignus3 Ember Hauler4 Goblin Chieftain4 Goblin Guide4 Goblin Bushwhacker3 Searing Blaze4 Lightning Bolt3 Forked Bolt3 Kuldotha Rebirth2 Devastating Summons2 Panic Spellbomb15 Mountain4 Teetering Peaks2 Smoldering Spires// Sideboard:SB: 4 Dragon's ClawSB: 4 Leyline of PunishmentSB: 3 Koth of the HammerSB: 4 Mark of Mutiny

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Rename text file based on content

#28 Post by foxidrive » 10 Sep 2013 02:30

Try this on some sample files. You have ! characters in the names so this uses a different technique to allow them to be used.


Code: Select all

@echo off
for /f "delims=" %%a in ('dir *.txt /b /a-d ') do call :next "%%a"
pause
goto :EOF
:next
set /p "var="<"%~1"
for /f "delims=/," %%b in ("%var%") do set "var=%%b"
ren "%~1" "%var:~0,-1%%~x1"

Serpent
Posts: 12
Joined: 08 Sep 2013 17:18

Re: Rename text file based on content

#29 Post by Serpent » 10 Sep 2013 03:31

i try it but nothing is going on ???

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Rename text file based on content

#30 Post by foxidrive » 10 Sep 2013 03:53

It works fine. Try this in an empty folder.

Code: Select all

>1.txt echo //Survival Zoo , a Legacy deck by Stork3 Qasali Pridemage3 Vengevine4 Fauna Shaman1 Loyal Retainers4 Mother of Runes4 Noble Hierarch4 Knight of the Reliquary4 Tarmogoyf1 Basking Rootwalla1 Iona, Shield of Emeria1 Gaddock Teeg4 Swords to Plowshares4 Survival of the Fittest3 Forest1 Plains4 Windswept Heath1 Verdant Catacombs1 Wooded Foothills3 Wasteland1 Misty Rainforest3 Horizon Canopy3 Savannah1 Karakas1 Gaea's Cradle

>2.txt echo //Valakut Ramp , a Standard deck by MilanoLawyer3 Avenger of Zendikar4 Primeval Titan2 Inferno Titan4 Goblin Ruinblaster4 Overgrown Battlement4 Lightning Bolt4 Summoning Trap4 Cultivate4 Explore5 Forest12 Mountain4 Evolving Wilds2 Terramorphic Expanse4 Valakut, the Molten Pinnacle// Sideboard:SB: 4 Obstinate BalothSB: 3 Tunnel IgnusSB: 3 Acidic SlimeSB: 2 Ricochet TrapSB: 3 Pyroclasm


>3.txt echo //Goblins! , a Standard deck by manachuel4 Memnite3 Tunnel Ignus3 Ember Hauler4 Goblin Chieftain4 Goblin Guide4 Goblin Bushwhacker3 Searing Blaze4 Lightning Bolt3 Forked Bolt3 Kuldotha Rebirth2 Devastating Summons2 Panic Spellbomb15 Mountain4 Teetering Peaks2 Smoldering Spires// Sideboard:SB: 4 Dragon's ClawSB: 4 Leyline of PunishmentSB: 3 Koth of the HammerSB: 4 Mark of Mutiny


@echo off
for /f "delims=" %%a in ('dir *.txt /b /a-d ') do call :next "%%a"
pause
goto :EOF
:next
set /p "var="<"%~1"
for /f "delims=/," %%b in ("%var%") do set "var=%%b"
ren "%~1" "%var:~0,-1%%~x1"

Post Reply