Hi rolly,

Place this code in a module. Then run Main.

What this does is allows you to select a *.txt file, it opens said file, looks for "0test", then replaces it with the name of the file without the ".txt" then it writes the file back to the same location with the corrections. I did not open notepad as it wasn't neccessary, if you want to open it anyway let me know and I will open it and have it open the file.


[vba]Private Declare Function GetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" (pOpenfilename As OpenFileName) As Long
Private Type OpenFileName
lStructSize As Long
hWndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
'Purpose : Allows the user to select a file name from a local or network directory.
'Inputs : sInitDir The initial directory of the file dialog.
' sFileFilters A file filter string, with the following format:
' eg. "Excel Files;*.xls|Text Files;*.txt|Word Files;*.doc"
' [sTitle] The dialog title
' [lParentHwnd] The handle to the parent dialog that is calling this function.
'Outputs : Returns the selected path and file name or a zero length string if the user pressed cancel

Function BrowseForFile(sInitDir As String, Optional ByVal sFileFilters As String, _
Optional sTitle As String = "Open File", Optional lParentHwnd As Long) As String
Dim tFileBrowse As OpenFileName
Const clMaxLen As Long = 254

tFileBrowse.lStructSize = Len(tFileBrowse)

'Replace friendly deliminators with nulls
sFileFilters = Replace(sFileFilters, "|", vbNullChar)
sFileFilters = Replace(sFileFilters, ";", vbNullChar)
If Right$(sFileFilters, 1) <> vbNullChar Then
'Add final delimiter
sFileFilters = sFileFilters & vbNullChar
End If

'Select a filter
tFileBrowse.lpstrFilter = "Text Files (*.TXT)" & vbNullChar & "*.TXT" & vbNullChar
'create a buffer for the file
tFileBrowse.lpstrFile = String(clMaxLen, " ")
'set the maximum length of a returned file
tFileBrowse.nMaxFile = clMaxLen + 1
'Create a buffer for the file title
tFileBrowse.lpstrFileTitle = Space$(clMaxLen)
'Set the maximum length of a returned file title
tFileBrowse.nMaxFileTitle = clMaxLen + 1
'Set the initial directory
tFileBrowse.lpstrInitialDir = sInitDir
'Set the parent handle
tFileBrowse.hWndOwner = lParentHwnd
'Set the title
tFileBrowse.lpstrTitle = sTitle

'No flags
tFileBrowse.flags = 0
'Show the dialog
If GetOpenFileName(tFileBrowse) Then
BrowseForFile = Trim$(tFileBrowse.lpstrFile)
If Right$(BrowseForFile, 1) = vbNullChar Then
'Remove trailing null
BrowseForFile = Left$(BrowseForFile, Len(BrowseForFile) - 1)
End If
End If
End Function
Sub Main()
Dim mFleNm As String, Fle As Long, mInfo As String, mLongFle As Long
Dim mFileName As String
mFleNm = BrowseForFile("C:\")
Fle = FreeFile
mFileName = Right(mFleNm, Len(mFleNm) - InStrRev(mFleNm, "\"))
mFileName = Left(mFileName, Len(mFileName) - 4)
Open mFleNm For Input As #Fle
mLongFle = LOF(Fle)
mInfo = Input(mLongFle, #Fle)
Close (Fle)
mInfo = Replace(mInfo, "0test", mFileName)
Fle = FreeFile
Open mFleNm For Binary As #Fle
Put #Fle, , mInfo
Close #Fle
End Sub
[/vba]

If you need explainations or further help do not hesitate to ask.

EDIT: I forgot to fix for readability. (no scrolling to the right)