batch write file name in file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ralfs_k
Posts: 6
Joined: 24 Feb 2012 04:17

batch write file name in file

#1 Post by ralfs_k » 07 Mar 2012 11:53

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?

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: batch write file name in file

#2 Post by Squashman » 07 Mar 2012 12:42

Code: Select all

FOR %%G in (*.txt) do echo %%~nG>>%%G

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

Re: batch write file name in file

#3 Post by foxidrive » 07 Mar 2012 23:15

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"

ralfs_k
Posts: 6
Joined: 24 Feb 2012 04:17

Re: batch write file name in file

#4 Post by ralfs_k » 08 Mar 2012 05:02

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?

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: batch write file name in file

#5 Post by Squashman » 08 Mar 2012 06:41

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.

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

Re: batch write file name in file

#6 Post by foxidrive » 08 Mar 2012 08:57

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.

ralfs_k
Posts: 6
Joined: 24 Feb 2012 04:17

Re: batch write file name in file

#7 Post by ralfs_k » 09 Mar 2012 05:35

tested and working!

miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: batch write file name in file

#8 Post by miskox » 09 Mar 2012 06:03

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

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

Re: batch write file name in file

#9 Post by foxidrive » 09 Mar 2012 06:29

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.

Post Reply