Batch File redirect File Name

I  have a few tab delimited text files in a the same folder.  
Is there a way in a batch file to echo the name of a file to be redirected as a new field into that same file?
 I need to do this with a wildcard for all .txt files in a specific folder, for example with a file called:  File1.txt
File1.txt will get added to that file as a new field.
and so on for the rest of the files in the folder. Any ideas?   Thanks.
LVL 3
fjkaykr11Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
Is there only one line in each text file, or multiples?

Do you just want the name of the file added, or the name and the extension, or the full path.

I would recommend VBS for this rather than BAT, is that okay?

~bp
0
Bill PrewCommented:
Okay, here's a small VBscript that will do the job.  Save it as a VBS file and adjust the folder name in the script to reference the folder you want to update the files in.  Then run it from a command prompt as:

cscript EE28242171.vbs

' Constants for I/O
Const ForReading = 1
Const ForWriting = 2

' Define folder to search
strBaseDir = "C:\EE\EE28242171\Files\"

' Access the folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strBaseDir)

' Process each file in the folder
For Each objFile in objFolder.Files

    ' See if it matches the pattern of filenames we want
    If UCase(Right(objFile.Name, 4)) = ".TXT" Then

        ' Read the entire file
        strFile = objFile.Path
        Set objReader = objFSO.OpenTextFile(strFile, ForReading)
        strData = objReader.ReadAll
        objReader.Close

        ' Make desired replacements in the file
        strData = Replace(strData, vbCrLf, vbTab & objFile.Name & vbCrLf)

        ' Write the updated data back to the file
        Set objWriter = objFSO.OpenTextFile(strFile, ForWriting, True)
        objWriter.Write strData
        objWriter.Close

    End If

Next

Open in new window

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fjkaykr11Author Commented:
@ billprew thanks for the info.
I don't need the extension or file path, just the name of the file echoed to each individual file in the folder.  There is more than 1 line Ideally, if I could echo the file name to all lines in each file. but if not than this will have to do for now.  I am not that familiar with VBS, but if it is easier to use this than I will give this a try.  I am going to keep my posting up to see if I get any other responses regarding doing this in Batch format.
0
fjkaykr11Author Commented:
I test the VBS code it works very well. Thanks so much!
0
Bill PrewCommented:
Welcome, glad that helped.

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.