• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 396
  • Last Modified:

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.
  • 3
  • 2
1 Solution
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?

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

        ' 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

    End If


Open in new window

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.
fjkaykr11Author Commented:
I test the VBS code it works very well. Thanks so much!
Bill PrewCommented:
Welcome, glad that helped.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now