Jump to content

Simple CVS Script Help


Recommended Posts

I believe AutoIT would be a good fit for this but I don't know how. Any help would be GREATLY appreciated.

I have a CSV file with three values in it

FILENAME DECISION PATH

----------- ------------ ------

Basically what I'd like to do is that in the DECISION field, if it says ARCHIVE, then MOVE the file to a new location. If it says KEEP, then move to the next line.

I gotta think this is pretty simple, but I'm haven't been able to find anything that can help me out.

PLEASE? :">

Thanks in advance!

Link to comment
Share on other sites

Take a look at the sample code in the help file under FileReadLine

Start with that basic script and add StringSplit using a comma as the delimiter.

(This should work if your filenames and paths do not have commas in them.)

Then look at FileMove and the If function.

$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $info = StringSplit ($line, ",", 1)
    If $info[2] = "ARCHIVE" Then FileMove(.....
Wend

FileClose($file)
...just a start, I'll let you do the rest.

Post back if you cannot figure out the syntax of the FileMove.

Caveat: The code above is off of the top of my head and I cannot test it right now... It could be full of bugs.

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Thanks for the help. It's doing exactly what I asked. I really appreciate it.

The third field in the CSV file is PATH. As far as I can tell, Autoit will not move the files into a directory if that directory does not already exist. Is there a way to do this?

For example:

I have a file:

C:\temp\directory2\file1.txt

I want to move the files down one directory, so I would have:

C:\ARCHIVE\temp\directory2\file1.txt

See what I mean? I need to keep the same directory structure, except moved down one level.

Any ideas/suggestions?

Thanks again for your help!

Link to comment
Share on other sites

...As far as I can tell, Autoit will not move the files into a directory if that directory does not already exist...

FileMove should work for you.

I've never used it in a script, but the help file states this in part:

FileMove ("source", "dest", flag)

[optional] this flag determines whether to overwrite files if they already exist:

Can be a combination of the following:

0 = (default) do not overwrite existing files

1 = overwrite existing files

8 = Create destination directory structure if it doesn't exist (See Remarks).

Remarks

If the source and destination paths are on different volumes a copy and delete operation is performed rather than a move.

For instance the combined flag '9' (1 + 8) overwrites the target file and prechecks for the destination directory structure and if it doesn't exist creates it automatically.

If you cannot get it to work for you, please copy and paste your code into your next post along with a portion of the CSV file that you are using. :-)

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Okay I'm getting closer now. I was running a slightly older version of AutoIT and it didn't have the "8" flag on FileMove.

Now it does create the directory structure, but instead of moving the into the directory, it CREATES a directory with the filename

Here's the code I'm using:

$file = FileOpen("C:\Temp\test.csv", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $info = StringSplit ($line, ",", 1)
    If $info[2] = "ARCHIVE" Then FileMove("C:\" & $info[3] & "\" & $info[1], "C:\TEMP\Archive\" & $info[3] & "\", 8)
Wend

FileClose($file)

And for reference, I've zipped up all of the files I'm using. Just extract to your Temp directory and you'll be looking at exactly what I am.

THANK YOU!!!!!

Edited by rchamberlin
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...