Page 1 of 1
batch write file name in file
Posted: 07 Mar 2012 11:53
by ralfs_k
i have ~20000 files with different names how do i echo file name inside a file?
for example if file is called 10008992.txt then is should contain text 10008992 and so on...
any ideas?
Re: batch write file name in file
Posted: 07 Mar 2012 12:42
by Squashman
Code: Select all
FOR %%G in (*.txt) do echo %%~nG>>%%G
Re: batch write file name in file
Posted: 07 Mar 2012 23:15
by foxidrive
Careful, that will change all your files if that is not what you want to do, and using plain FOR has a bug for this kind of usage too (it will process some files twice or three times etc)
This will append the filename to each .txt file in the folder.
Is that what you want to do?
Code: Select all
@echo off
for /f "delims=" %%G in ('dir /b "*.txt"') do echo %%~nG>>"%%G"
Re: batch write file name in file
Posted: 08 Mar 2012 05:02
by ralfs_k
foxidrive tnx
Code: Select all
@echo off
for /f "delims=" %%G in ('dir /b "*.txt"') do echo %%~nG>>"%%G"
works great but how to add that line at the top of file not end?
Re: batch write file name in file
Posted: 08 Mar 2012 06:41
by Squashman
ralfs_k wrote:works great but how to add that line at the top of file not end?
Just knew you were going to say that next after I read your initial question. That would have been nice to know before hand.
Re: batch write file name in file
Posted: 08 Mar 2012 08:57
by foxidrive
This is untested, it expects text files, not binary files.
Please test it on some copies first, before doing all your files.
Code: Select all
@echo off
for /f "delims=" %%G in ('dir /b "*.txt"') do (
echo processing "%%G"
echo %%~nG>tempfile.abc
type "%%G">>tempfile.abc
move /y tempfile.abc "%%G" >nul
)
echo.
echo Done.
Re: batch write file name in file
Posted: 09 Mar 2012 05:35
by ralfs_k
tested and working!
Re: batch write file name in file
Posted: 09 Mar 2012 06:03
by miskox
ralfs_k wrote:foxidrive tnx
Code: Select all
@echo off
for /f "delims=" %%G in ('dir /b "*.txt"') do echo %%~nG>>"%%G"
works great but how to add that line at the top of file not end?
to use DIR /B /O-N ?
Saso
Re: batch write file name in file
Posted: 09 Mar 2012 06:29
by foxidrive
miskox wrote:ralfs_k wrote:works great but how to add that line at the top of file not end?
to use DIR /B /O-N ?
Saso
No Saso. As we understand it the OP wants the first line in the file to be the filename.
sorting will only change the order of which file is processed first.