Jump to content
david1337

Only look for the first 3 characters

Recommended Posts

Hey guys
I'm a little stuck and could need some help.

$Dir = "C:\temp\"
$username = @UserName


Global $Custom[3]

$Custom[0] = ("01_List")
$Custom[1] = ("02_List")
$Custom[2] = ("03_List")


 For $i = 0 to 3 - 1

$CustomFile = FileOpen($Dir & $Custom[$i] & ".txt", 0)
$ReadCustomFile = FileRead($CustomFile)
FileClose($CustomFile)

If StringRegExp($ReadCustomFile, $username) Then
   FileCopy($Dir & $Custom[$i] & ".txt", $Dir & "Match")
EndIf

Next

In C:\temp\ I have:
01_List.txt
02_List.txt
03_List.txt

 

The script will look through all 3 files to find the one containing my username. When it finds that file, it will copy the file to the folder called "Match"

All this works fine. But what if I would like it to only identify the files by the first 3 characters? "01_" &  "02_" & "03_"
This way I could rename the txt files to 01_whatever, 02_somethingElse  etc., and it would still work.

Share this post


Link to post
Share on other sites
$sString = "String"

$sFirstThreeChars = StringLeft($sString, 3)

; $sFirstThreeChars contains "Str"

:)

Edited by TheDcoder

A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites

Thank you for answering :)

I have tried to use StringLeft, but haven't got it to work.
Using StringLeft tells the script to read "01_List" as "01_" yes, but that doesn't change the fact that the filecopy will fail, since there is no file called "01_"

Or am I missing something obvious?

Share this post


Link to post
Share on other sites
#include <File.au3>

$aFiles = _FileListToArray($Dir, "???*.txt", $FLTA_FILES) ; Will return an array containing txt files with minimum of 3 chars

For $i = 1 To $aFiles[0]
    If StringLeft($aFiles[$i], 3) = "<Your Matching String Here>" Then
        ; You code here
    EndIf
Next

 


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites

TheDcoder

Thank you for your time!
Ahhhh head explosion... can't get your example implemented into my own script.

And why did it change to For $i = 1 To $Custom[0] ?

$Dir = "C:\temp\"
$username = @UserName



Global $Custom[3]

$Custom[0] = ("01_List")
$Custom[1] = ("02_List")
$Custom[2] = ("03_List")

#include <File.au3>

$Custom = _FileListToArray($Dir, "???*.txt", $FLTA_FILES) ; Will return an array containing txt files with minimum of 3 chars

For $i = 1 To $Custom[0]
    If StringLeft($Custom[$i], 3) = "<What Matching String? The username?>" Then

        FileCopy($Dir & $Custom[$i] & ".txt", $Dir & "Match")

    EndIf
Next

 

Share this post


Link to post
Share on other sites

And why did it change to For $i = 1 To $Custom[0] ?

$Custom[0] contains the number of files, so no need to Ubound :)

 

$Dir = "C:\temp\"
$username = @UserName



Global $Custom[3]

$Custom[0] = ("01_List")
$Custom[1] = ("02_List")
$Custom[2] = ("03_List")

#include <File.au3>

$Custom = _FileListToArray($Dir, "???*.txt", $FLTA_FILES) ; Will return an array containing txt files with minimum of 3 chars

For $i = 1 To $Custom[0]
    If StringLeft($Custom[$i], 3) = $username Then

        FileCopy($Dir & $Custom[$i], $Dir & "Match")

    EndIf
Next

You appended the txt extension, there is no need for it :)


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites

Thanks mate!

I got it working, thanks to you :)

Much appreciated!

$Dir = "C:\temp\"
$username = @UserName



Global $Custom[3]

$Custom[0] = ("01_List")
$Custom[1] = ("02_List")
$Custom[2] = ("03_List")

#include <File.au3>

$Custom = _FileListToArray($Dir, "???*.txt", $FLTA_FILES) ; Will return an array containing txt files with minimum of 3 chars

For $i = 1 To $Custom[0]

$CustomFile = FileOpen($Dir & $Custom[$i], 0)
$ReadCustomFile = FileRead($CustomFile)
FileClose($CustomFile)
If StringRegExp($ReadCustomFile, $username) Then
   FileCopy($Dir & $Custom[$i], $Dir & "Match")
EndIf

Next

 

Share this post


Link to post
Share on other sites

@david1337 My pleasure! :D


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites

Does it? It doesn't for me... BTW I modified the filter, try this:

$aFiles = _FileListToArray($sLocation, "*_*.txt", $FLTA_FILES) ; Will return an array containing txt files with an underscore in its name

 


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
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

×
×
  • Create New...