AutoHotkey Community

It is currently May 26th, 2012, 11:06 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: November 3rd, 2009, 8:58 pm 
Offline

Joined: November 3rd, 2009, 8:19 pm
Posts: 5
Hello,

I need to read a CSV file, then using sendkey have it post the variables (content of the csv) so it can be "typed into a dos program"
I want to be able to use a sendkey command to work the data as I need.. example:

READS CSV FILE AND ASSIGNS VARIABLES FOR LINE
VAR1, VAR2, VAR3, VAR4
sendkey amkyyesVAR2{enter}VAR1{space}adks{space}VAR3 etc...

I can pay via paypal if needed OR MAKE A DONATION.

Thanks - Ryan


Report this post
Top
 Profile  
Reply with quote  
PostPosted: November 3rd, 2009, 9:16 pm 
Offline

Joined: November 3rd, 2009, 8:17 pm
Posts: 3
Please post The code you have so far so we can help you.

_________________
"Learning without thought is labor lost;
thought without learning is perilous."
-Kong Fu Zi(Confucius)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 3rd, 2009, 9:20 pm 
Offline

Joined: February 17th, 2008, 7:09 am
Posts: 536
Here is what i have, based on StringSplit. I used notepad and cat to confirm it works.

Code:
setkeydelay, 50, 10
sleep 3000

sep1 := "{enter}"
sep2 := "{space}"

loop {
   FileReadLine, line, c:\test.csv, %A_index%
   if ErrorLevel
      break

   StringSplit, array, line, `,, %A_space%
   if (array0 == 0)
      continue

   loop, %array0% {
      tmp := array%a_index%
      SendInput {raw}%tmp%
      if (a_index == 1)
         SendInput %sep1%
      else
         SendInput %sep2%
   }

   ;determine how you want this separated.
   SendInput {enter}
}


and some sample data.
Code:
header, abc, 123, {tab} raw test
Header Only

Following:,   the,empty,line


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 4th, 2009, 4:03 am 
Offline

Joined: February 17th, 2008, 7:09 am
Posts: 536
Quote:
if ErrorLevel
SendInput FILE READ ERROR
break


Your problem is that your if statment doesn't encapsulate the two lines. Plus, do you want this to always say 'FILE READ ERROR' each time it gets done reading a file? Change it to this instead.

Code:
    if ErrorLevel {
      if (A_index == 1)
        SendInput FILE READ ERROR
      break
    }


EDIT: seems his last entry got deleted before my reply posted.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 4th, 2009, 4:07 am 
Offline

Joined: November 3rd, 2009, 8:19 pm
Posts: 5
Hello,

Here is what I have so far:

test.csv contents:
Code:
test1,test2,test3
test11,test22,test33
test111,test222,test333


Code:
Code:

^x::
setkeydelay, 50, 10
sleep 1000

loop {
   FileReadLine, line, c:\test.csv, %A_index%
       if ErrorLevel {
      if (A_index == 1)
        SendInput FILE READ ERROR
      break
    }

  StringSplit, array, line, `,, %A_space%
   if (array0 == 0)
      continue

   loop, %array0% {
      tmp := array%a_index%
 
}
SendInput %array2% test other keys %array3%{enter}
}

return




Output:
Code:
test2 test other keys test3
test22 test other keys test33
test222 test other keys test333


THANK YOU rtcvb32 - I HAVE UPDATED THIS POST & CODE!

Any ideas how I can make it sleep between lines (SendInput commands) ?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 4th, 2009, 4:20 am 
Offline

Joined: February 17th, 2008, 7:09 am
Posts: 536
First, the code i had removed the spaces. As per the documentation, if that doesn't work you can always trim it.
Code:
StringSplit, array, line, `,, %A_space%


Code:
tmp = %tmp ;will trim unless told not to


From the code you have posted, it will loop through the array but assign tmp to the last one. But you don't use tmp at all, instead you're forcing it to use array2 & array3 only.

If you know it will always have 3 elements like your sample, then you don't need the loop only reference them as array0 (number of elements) array1-array3 as the comma separated data.

And to make it sleep, yes use the sleep command. every 1000 is a second. Place it anywhere you need a pause.
Code:
sleep 500 ;wait half a second before resuming code


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 4th, 2009, 4:45 am 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
Getting the output is simple enough:
Code:
output=
Loop, Read, test.csv
 Loop, Parse, A_LoopReadLine, CSV
  output .= A_Index=2 ? A_LoopField " STRING "
           :A_Index=3 ? A_LoopField "`n":""
MsgBox,% output
...however, I would search for an easier way of entering the data into your DOS program. SendInput is a last resort. The program should have a native command to import text, or a command line option.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 4th, 2009, 6:49 pm 
Offline

Joined: November 3rd, 2009, 8:19 pm
Posts: 5
Here is the latest code.. Can someone please tell me why i get a FILE READ ERROR... is it the
"FileReadLine, line, `n%SelectedFile%, %A_index%" Line?

Code:

^x::

MsgBox, 4, , Do you want to start import? (Press YES or NO)
IfMsgBox No
    return

FileSelectFile, SelectedFile, 3, , Open a file, Text Documents (*.csv; *.txt)
if SelectedFile =
    MsgBox, No File Selection Made. Import Process Canceled.
else
    MsgBox, The user selected the following:`n%SelectedFile%


setkeydelay, 50, 10
sleep 1000

loop {
   FileReadLine, line, `n%SelectedFile%, %A_index%
       if ErrorLevel {
      if (A_index == 1)
        SendInput FILE READ ERROR
      break
    }

StringSplit, array, line, `,, %A_space%
   if (array0 == 0)
      continue

   loop, %array0% {
      tmp := array%a_index%
 
}
SendInput %array2% test other keys %array3%{enter}
sleep 1000
}

return








Thanks - Ryan


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 4th, 2009, 7:06 pm 
Offline

Joined: October 15th, 2007, 3:10 pm
Posts: 790
Location: England
Code:
FileReadLine, line, `n%SelectedFile%, %A_index%


Why is there a `n in there? Remove that and it should be OK.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: FINAL CODE
PostPosted: November 6th, 2009, 1:19 am 
Offline

Joined: November 3rd, 2009, 8:19 pm
Posts: 5
Here is my finaly code.. tested and working.. In case anyone in the future ever has a task like this. Maybe this code will help them get started.
Thanks everyone!

Code:
^x::

MsgBox, 4, , Do you want to start import? (Press YES or NO)
IfMsgBox No
    return

FileSelectFile, SelectedFile, 3, , Open a file, Text Documents (*.csv; *.txt)
if SelectedFile =
    MsgBox, No File Selection Made. Import Process Canceled.
else
   
SetKeyDelay, 200, 100
sleep 2000

loop {
   FileReadLine, line, %SelectedFile%, %A_index%

       if ErrorLevel {
      if (A_index == 1)
        SendInput FILE READ ERROR
      break
    }

StringSplit, array, line, `,, %A_space%
   if (array0 == 0)
      continue

   loop, %array0% {
      tmp := array%a_index%
 
}

manufacturer=shar
department=7
partnumber=%array1%
partnumber := SubStr( partnumber . "            ", 1, 12 )
description=%array2%
description := SubStr( description . "                            ", 1, 28 )
StringLeft, listprice, array3, 7
StringLeft, cost, array4, 7
StringLeft, qty, array5, 6



Send ay%manufacturer%{Enter}{Enter}{Enter}%partnumber%%department%{enter}{enter}
sleep 1000
Send {enter}{F1}{enter}%description%%qty%{enter}0{enter}-1{enter}{enter}{enter}{enter}{enter}%listprice%{enter}%cost%{enter}d{enter}n

SoundPlay *-1
sleep 2000
}

MsgBox All Done!

return


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: JSLover, Kirtman, Miguel, XstatyK and 59 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group