Batch Programming Help - Renaming Files
Moderator: DosItHelp
Batch Programming Help - Renaming Files
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..?
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..?
Re: Batch Programming Help - Renaming Files
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
)
Re: Batch Programming Help - Renaming Files
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?
Re: Batch Programming Help - Renaming Files
RE: foxidrive
Yes that is exactly what I wish to accomplish. It's all very simple, just I don't know the structure.
Yes that is exactly what I wish to accomplish. It's all very simple, just I don't know the structure.
Re: Batch Programming Help - Renaming Files
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?
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?
Re: Batch Programming Help - Renaming Files
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:
Calling the batch label allows you to check the extension of the file and run a condition
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
Re: Batch Programming Help - Renaming Files
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..
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..
Re: Batch Programming Help - Renaming Files
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
Re: Batch Programming Help - Renaming Files
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.
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.
Re: Batch Programming Help - Renaming Files
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"
)
)
Re: Batch Programming Help - Renaming Files
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.
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.
Re: Batch Programming Help - Renaming Files
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.
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"
)
)
Re: Batch Programming Help - Renaming Files
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*
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*