Jump to content

What am I missing here?


Recommended Posts

I am trying to create a script which can be run in a directory full of text files with names that are in a numeric order, ie., 2003.txt, 2004.txt, etc. I want to read each file's first line, change the spaces to _'s and copy the file (or rename) it with this.

So if file 2003.txt had this 'How to train your dog' as the first line, the script would create/copy that file to a file named 'How_to_train_your_dog'. It would even be nice to add the .txt extension back on. I have a somewhat working script, which is giving me some odd behaviour. I am extremely new to this AutoIT stuff, but here is what I have so far:

;Get first filename and put into variable
$search = FileFindFirstFile("*.*") 

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

; Process files
While 1
        ;Set my new filename and string variables to nothing.
    $file2 = ""; 
    $name = "";

;Get filename into $file
    $file = FileFindNextFile($search)
    If @error Then 
        ConsoleWrite("Error with FileFindNextFile")
        Exit
    EndIf

;Read the first line this will be the new filename
    $line = FileReadLine($file)
    If @error = -1 Then 
        ConsoleWrite("Error with FileReadLine")
        Exit
    EndIf

        ;create a new filename, strip the whitespace
    $name = StringStripWS(StringReplace($line, " ", "_"),8)
    If @error = -1 Then 
        ConsoleWrite("Error with StringReplace")
        Exit
    EndIf

        ;i was hoping this would give the desired result.
    $file2 = FileOpen($name, 2)
    If @error = -1 Then 
        ConsoleWrite("Error with FileOpen")
        Exit
    EndIf


    FileCopy($file,$file2)
    If @error = -1 Then 
        ConsoleWrite("Error with FileCopy")
        Exit
    EndIf
Wend

FileClose($search)
FileClose($file)
FileClose($file2)

The biggest problem with this script so far is that the first few files are named correctly, then after about 8-10 files, the name begins getting _'s prepended to the name, each file after getting another _ until the last one has about 30 _'s prepended.

The new files are also 0 bytes in size. So the data is not being copied, and none of the original files are over 100k in size.

Could someone shed a little light on this for me? I feel like quite the idiot at the moment! :D

Thanks!

IvoryBishop

Link to comment
Share on other sites

You never get out of the loop because 1 is always true so you never close the files ...

--------------------------------------------------------------------------------------------------------------------------------Scripts : _Encrypt UDF_UniquePCCode UDF MS like calculatorInstall programm *UPDATED* --------------------------------------------------------------------------------------------------------------------------------[quote name='Helge' post='213117' date='Jul 26 2006, 10:22 AM']Have you ever tried surfing the internet with a milk-carton ?This is similar to what you're trying to do.[/quote]

Link to comment
Share on other sites

I am trying to create a script which can be run in a directory full of text files with names that are in a numeric order, ie., 2003.txt, 2004.txt, etc. I want to read each file's first line, change the spaces to _'s and copy the file (or rename) it with this.

So if file 2003.txt had this 'How to train your dog' as the first line, the script would create/copy that file to a file named 'How_to_train_your_dog'. It would even be nice to add the .txt extension back on. I have a somewhat working script, which is giving me some odd behaviour. I am extremely new to this AutoIT stuff, but here is what I have so far:

CODE

;Get first filename and put into variable

$search = FileFindFirstFile("*.*")

; Check if the search was successful

If $search = -1 Then

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

Exit

EndIf

; Process files

While 1

;Set my new filename and string variables to nothing.

$file2 = "";

$name = "";

;Get filename into $file

$file = FileFindNextFile($search)

If @error Then

ConsoleWrite("Error with FileFindNextFile")

Exit

EndIf

;Read the first line this will be the new filename

$line = FileReadLine($file)

If @error = -1 Then

ConsoleWrite("Error with FileReadLine")

Exit

EndIf

;create a new filename, strip the whitespace

$name = StringStripWS(StringReplace($line, " ", "_"),8)

If @error = -1 Then

ConsoleWrite("Error with StringReplace")

Exit

EndIf

;i was hoping this would give the desired result.

$file2 = FileOpen($name, 2)

If @error = -1 Then

ConsoleWrite("Error with FileOpen")

Exit

EndIf

FileCopy($file,$file2)

If @error = -1 Then

ConsoleWrite("Error with FileCopy")

Exit

EndIf

Wend

FileClose($search)

FileClose($file)

FileClose($file2)

The biggest problem with this script so far is that the first few files are named correctly, then after about 8-10 files, the name begins getting _'s prepended to the name, each file after getting another _ until the last one has about 30 _'s prepended.

The new files are also 0 bytes in size. So the data is not being copied, and none of the original files are over 100k in size.

Could someone shed a little light on this for me? I feel like quite the idiot at the moment! :D

Thanks!

IvoryBishop

Look at the example in the help file for FileFindNextFile() and you see how to handle the end of the list correctly. Since you want to read .txt files from a specific directory, you might start with:

$search = FileFindFirstFile("C:\MyFolder\*.txt")

And adding .txt to the output file is just:

$name = StringStripWS(StringReplace($line, " ", "_"),8) & ".txt"

:D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks for the quick responses. I have not yet resolved this issue. As I am still learning the extreme basics of this AutoIT script, I have concluded that the KISS principal would fit here well. In that spirit, I have modified the first script a lot. :P

Send("Echo Begin program...")
Send(@CR)
$search = FileFindFirstFile("c:\wri\*.txt") 
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf
While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    $line = FileReadLine($file)
    $name = StringStripWS(StringReplace($line, " ", "_"),8) & ".txt"
    $file2 = FileOpen($name, 2)
    FileCopy($file,$file2,1)
    FileClose($file2)   
Wend
FileClose($search)
Send(@CR)
Send("Echo End program...")

As you can see, I also did some more detailed reading of the ConsoleWrite function. :D I was sure it would write to the cmd.exe "console". So the Send function will do for now. It is late, and I fear my brain is fried. I appreciate the input. Maybe a few other pair of eyes can shed some light on this.

Warmest regards,

IvoryBishop

PS. Is there anything this scripting language cannot do? I think I am in love with it. :D

Link to comment
Share on other sites

  • Moderators

Just some things that I saw... maybe take a look and see if they help:

Send("Echo Begin program...")
Send(@CR)
$search = FileFindFirstFile(@HomeDrive & "\wri\*.txt")
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf
While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    $line = FileReadLine($file)
    $name = StringStripWS(StringReplace($line, " ", "_"),8) & ".txt";Strip white spaces then you use stringreplace to replace them with an underscore?
  ;$file2 = FileOpen($name, 2); $file2 is the "handle" not the name of the file
    FileCopy(@HomeDrive & "\wri\" & $file,@HomeDrive & "\wri\" & $name,1);$file only gives you the "name" of the file, not its location
  ;FileClose($file2)    
Wend
FileClose($search)
Send(@CR)
Send("Echo End program...")

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

$name = StringStripWS(StringReplace($line, " ", "_"),8) & ".txt";Strip white spaces then you use stringreplace to replace them with an underscore?

I think that replaces the spaces with underscore first, then removes any other white space from the result. It looked right to me... :D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Wow!

I was really missing the mark! Thanks for all the responses. Unfortunately, I am at work, but I may get a few moments to log in to my home workstation and try this out on my test data. You guys are great! Thanks again.

A very happy,

:D IvoryBishop

PS You guys have been very friendly and helpful. This forum reminds me of the arstechnica.com openforum. Very friendly, and quick responses to queries.

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...