batch write file name in file
Moderator: DosItHelp
batch write file name in file
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?
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
Code: Select all
FOR %%G in (*.txt) do echo %%~nG>>%%G
Re: batch write file name in file
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?
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
foxidrive tnx
works great but how to add that line at the top of file not end?
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
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
This is untested, it expects text files, not binary files.
Please test it on some copies first, before doing all your 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
tested and working!
Re: batch write file name in file
ralfs_k wrote:foxidrive tnxCode: 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
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.