Jump to content

File name capture


Recommended Posts

Following is the part of my code that I'm having problems with:

Local $path = FileFindFirstFile("C:\data\*.txt")

; Check if the search was successful

If $path = -1 Then

MsgBox(0, "Error", "No files/directories matched the search pattern")

Exit

EndIf

While 1

Local $filename = FileFindNextFile($path)

If @error Then ExitLoop

;~ MsgBox(4096, "File:", $filename)

FileOpen("C:\data\filename.txt", 1)

FileWrite("C:\data\filename.txt", $filename)

Local $filename2 = StringReplace($filename, ".txtfilename.txt", " ", 0)

Local $filename3 = StringReplace($filename, ".txt", " ", 0)

FileWrite("C:\data\filename.txt", $filename3)

;---------------------------------------------------

local $chars, $file, $filename

Local $file = FileOpen("C:\data\20120720xxxxxxxxxxxx.txt", 0)

; Check if file opened for reading OK

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

My intention is to capture the name of a new text file. It’s automatically generated (example: 20120720xxxxxxxxxxxx.txt) from C:\data\*.txt. Each new text will be unique but follow the same basic generated format. Add this into the line: Local $file = FileOpen("C:\data\20120720xxxxxxxxxxxx.txt", 0). So that the rest of my code can act on each incoming text file.

I have tried to add this variable into FileOpen("C:\data\20120720xxxxxxxxxxxx.txt", 0) but I’m missing something!?!?!? So far no combination works for me.

A) There’s probably a better way to accomplish this. (I’ve seen you guys produce some outstanding code.)

B) I have become frustrated with this and need to go have a cold beer.

Any help would be appreciated. Mean while, cheers!

Link to comment
Share on other sites

I don't understand what the StringReplace lines are supposed to do. Are you wanting to do something along these lines? If you could, get me a cold one, too.

Local $path = FileFindFirstFile("C:data*.txt")

; Check if the search was successful
If $path = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    Local $filename = FileFindNextFile($path)
    If not @error Then
    FileWrite("C:datafilename.txt", $filename & @CRLF)
    EndIf
WEnd
Link to comment
Share on other sites

Hi Ab

Apologies, I had reached "frustration mode" and needed to walk away. In some cases like this, "Beer therapy" works wonders. I would have bought you one too.

Anyway, on to the fray.

Please ignore the Stringreplace section. I just copy/pasted this into the message. That was something I was testing to see if I could capture the filename

another way.

Anyway, what I'm attempting to do is this:

#1 capture the name of the auto-named text file as it is created in the folder. Which I have, at least to some degree. It is in the variable $filename. Which I

verified using Msgbox. However, I need to tell the read-in section what to copy. Otherwise it copies nothing! It all works when I specify a specific name, but that's not the point right.

(I'm missing something here and I can't see the forest, just too many bloody trees in the way! Arrrrrg!)

#2 then create a second working copy text message. To ensure that I don't screw up the original. (done, when #1 is complete)

#3 re-format the working copy. (done)

#4 send this text as a plain text message, using INet. (done)

Link to comment
Share on other sites

I'm still not 100% sure what the problem is. Here's my best guess: you have the file name in the variable $filename. However, when you try to use FileOpen($filename), it's not working. Is that what you mean? If so, I think you may be using functions, which do not have access to Local variables. You can either pass the data to the function or change from Local to Global.

Let me know if I'm anywhere near the ballpark.

Link to comment
Share on other sites

Ab

Global did help. Also my "While 1" was killing off the $filename value.

Sorry, I didn't mean to confuse you. What and how I think sometimes doesn't translate to paper very well.

Let me give this another go. It's fairly straight forward I think.

When a Text file is written into the "C:data" folder, I'm trying to capture the auto-named text files filename.

With this I can tell my file copy While statment to make a copy. Which in turn re-formats the dupe copy.

I can then feed the captured filename variable into the Subject line of my outbound message.

If you see a better method, I'm open to suggestions.

Link to comment
Share on other sites

Ab

Good news. I changed this portion of the code a bit:

#include <INet.au3>

#include <File.au3>

#include <FileConstants.au3>

;---------------------------------------------------

Global $chars, $file, $filename

;~ Local $file = FileOpen("c:data201207xxxxxxxxxxxx.txt", 0)

Global $filename = FileFindFirstFile("c:data*.txt")

Global $path = "c:data"

; Check if file opened for reading OK

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

Global $filename2 = FileFindNextFile($filename)

Global $file = FileOpen($path & $filename2)

; Read in characters

While 1

Local $chars = FileRead($file, -1)

If @error = -1 Then ExitLoop

FileWrite("c:datatest2.txt", $chars)

Local $sOutput = FileRead($chars, -1)

Local $sOutput = StringRegExpReplace($chars, "rsH", @CRLF)

FileWrite("c:datatest3.txt", $sOutput)

ExitLoop

;~ Exit

WEnd

This works. I'm just retaining the filename + .txt in the variable. I'd like to strip the .txt off.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...