Batch Programming Help - Renaming Files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
marceno
Posts: 7
Joined: 15 Feb 2012 20:38

Batch Programming Help - Renaming Files

#1 Post by marceno » 15 Feb 2012 20:40

So, I'm new to writing Batch files. I've got the basic algorithm, however it is the formatting and language I'm having problems with.

I'm trying to write a file that replace that will, for each and every text file found in "C:\files\" folder, switch it's file name with it's content.

E.g. "file1.txt", "file2.txt", "file3.txt", and "file4.txt" exist in "C:\files\" folder. In "file1.txt" there is a line of text containing the single word "alpha". The other text files have different words as content.
"file1.txt" containing the word "alpha" becomes "alpha.txt" containing the word "file1".

-------------------------------------------------------------------------------------------------------------------
Here's my go at it:


FOR /R %%F IN (*.txt) DO (
set /p var= <Text.txt
RENAME %%F %var%
%%F > (*.txt)
)

-------------------------------------------------------------------------------------------------------------------


It's not working and for the love of all that is holy, I can't get my head around it.


NOW...if I can get it to switch file names back AND forth using the same batch file......

I would be so gracious as to even give you an internet hug...., their not cheap y'know..?

Cat
Posts: 32
Joined: 11 Nov 2011 12:04

Re: Batch Programming Help - Renaming Files

#2 Post by Cat » 15 Feb 2012 21:48

This works fine for me:

Code: Select all

@echo off
Cd /d c:\files
For /f  %%a in ('dir /b') do (
For /f "tokens=1" %%b in (%%a) do ren %%a %%b.txt
)

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

Re: Batch Programming Help - Renaming Files

#3 Post by foxidrive » 15 Feb 2012 22:28

marceno wrote:NOW...if I can get it to switch file names back AND forth using the same batch file......


Do you have file.txt which contains "alpha" and nothing else?

And you want to call it alpha.txt and fill it with "file" and nothing else?

Repeat?

marceno
Posts: 7
Joined: 15 Feb 2012 20:38

Re: Batch Programming Help - Renaming Files

#4 Post by marceno » 15 Feb 2012 22:35

RE: foxidrive

Yes that is exactly what I wish to accomplish. It's all very simple, just I don't know the structure.

marceno
Posts: 7
Joined: 15 Feb 2012 20:38

Re: Batch Programming Help - Renaming Files

#5 Post by marceno » 15 Feb 2012 22:42

RE: Cat


This nearly works!!! I'm so excited you've no idea!!

HOWEVER!!

When I run the batch file in the same directory as the files, the batch file is than converted into a txt document and is renamed. How do I keep the batch file separate?

Cat
Posts: 32
Joined: 11 Nov 2011 12:04

Re: Batch Programming Help - Renaming Files

#6 Post by Cat » 15 Feb 2012 23:10

The code I posted only works when the batch is run elsewhere on the computer, and no other files but text docs are in c:\files. From your reply to foxidrive, you'd probably want to get the contents of the file and save it then delete the file itself. Try this instead:

Code: Select all

@echo off
For /f  %%a in ('dir /b') do (
Set file=%%a
Call :rename %file%
)
:rename
If "%~x1"==".txt" (
For /f "tokens=1" %%b in (%file%) do set export=%%b
Del %1
Echo %file% >> %export%.txt
)
Exit /b


Calling the batch label allows you to check the extension of the file and run a condition :)

marceno
Posts: 7
Joined: 15 Feb 2012 20:38

Re: Batch Programming Help - Renaming Files

#7 Post by marceno » 15 Feb 2012 23:18

RE: Cat

Creating a temp file would work and I'm about to go streaking on my street at the mere idea I may finally achieve this...

HOWEVER!!

The code you just posted doesn't seem to be even renaming the files..?

I smell a bug...oh yes.., it's quite strong..

Cat
Posts: 32
Joined: 11 Nov 2011 12:04

Re: Batch Programming Help - Renaming Files

#8 Post by Cat » 15 Feb 2012 23:29

Code: Select all

@echo off
Setlocal enabledelayedexpansion
For /f  %%a in ('dir /b') do (
Set file=%%a
If !file:txt=! Neq !file! (
For /f "tokens=1" %%b in (%file%) do set export=%%b
Del %file%
Echo %file% >> %export%.txt
))
Exit /b
I don't see any problems with it like that. I'm away from my computer submitting these from my ipad, so I couldn't personally test it out at the moment.

marceno
Posts: 7
Joined: 15 Feb 2012 20:38

Re: Batch Programming Help - Renaming Files

#9 Post by marceno » 15 Feb 2012 23:58

RE: Cat

Still a no go. I tried in and out of the directory as well. Also just noticed a small problem with the first batch file. It renames the file names, but the contents remains as "alpha," etc.


If I could just get it working where it's outside of the directory and works at least one way, than I'll be happy and go to bed, lol.

Thank you btw for your help on this, much appreciated.

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

Re: Batch Programming Help - Renaming Files

#10 Post by foxidrive » 16 Feb 2012 00:02

marceno wrote:RE: foxidrive

Yes that is exactly what I wish to accomplish. It's all very simple, just I don't know the structure.


This works here. Test it on dummy files first.

There is no point in renaming the files and replacing the contents, this creates new files to solve your task

Code: Select all

@echo off
for %%a in (*.txt) do (
for /f "delims=" %%b in ('type "%%a"') do (
echo.%%~na>"%%b.txt"
del "%%a"
)
)

marceno
Posts: 7
Joined: 15 Feb 2012 20:38

Re: Batch Programming Help - Renaming Files

#11 Post by marceno » 16 Feb 2012 00:21

RE: foxidrive


NEARLY THERE!!

Ok, this works great the first time. When I go to try it again, and see if they switch back...it works.

Look'in good so far, but if I continue this loop, with each proceeding switch a file is left behind.

Out of 4 files; After 4 loops, 8 runs of the batch file, all of the files don't switch.


It's a little qwerky, but it looks like the for loop isn't reset back to it's initial parameters with each run.


I can't thank you guys enough for all of your hard work.

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

Re: Batch Programming Help - Renaming Files

#12 Post by foxidrive » 16 Feb 2012 01:00

I know what the issue is. The first normal for loop get confused when you create new files with the same extension it is using.
This use of FOR /F should fix it.

Code: Select all

@echo off
for /f "delims=" %%a in ('dir /b *.txt') do (
for /f "delims=" %%b in ('type "%%a"') do (
echo.%%~na>"%%b.txt"
del "%%a"
)
)

marceno
Posts: 7
Joined: 15 Feb 2012 20:38

Re: Batch Programming Help - Renaming Files

#13 Post by marceno » 16 Feb 2012 01:22

THANK YOU SO MUCH!! Internet hugs for everyone!! YAAAAHHHOOOOO!!!!


Please excuse for just a moment, I must disrobe and attend to my local streets.

I bid thee adu,

Danke, Graci, Gracias, Arigato, THANK YOU!!

*tips hat off*

Post Reply