AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Read CSV and use variables in SendKey

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
ryans



Joined: 03 Nov 2009
Posts: 5

PostPosted: Tue Nov 03, 2009 7:58 pm    Post subject: Read CSV and use variables in SendKey Reply with quote

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
Back to top
View user's profile Send private message
Loren C Fortner



Joined: 03 Nov 2009
Posts: 3

PostPosted: Tue Nov 03, 2009 8:16 pm    Post subject: Please post The code you have so far so we can help you. Reply with quote

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)
Back to top
View user's profile Send private message
rtcvb32



Joined: 17 Feb 2008
Posts: 289

PostPosted: Tue Nov 03, 2009 8:20 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Yahoo Messenger
rtcvb32



Joined: 17 Feb 2008
Posts: 289

PostPosted: Wed Nov 04, 2009 3:03 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Yahoo Messenger
ryans



Joined: 03 Nov 2009
Posts: 5

PostPosted: Wed Nov 04, 2009 3:07 am    Post subject: Reply with quote

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) ?
Back to top
View user's profile Send private message
rtcvb32



Joined: 17 Feb 2008
Posts: 289

PostPosted: Wed Nov 04, 2009 3:20 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Yahoo Messenger
jaco0646



Joined: 07 Oct 2006
Posts: 3113
Location: MN, USA

PostPosted: Wed Nov 04, 2009 3:45 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Visit poster's website
ryans



Joined: 03 Nov 2009
Posts: 5

PostPosted: Wed Nov 04, 2009 5:49 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
OceanMachine



Joined: 15 Oct 2007
Posts: 780
Location: England

PostPosted: Wed Nov 04, 2009 6:06 pm    Post subject: Reply with quote

Code:
FileReadLine, line, `n%SelectedFile%, %A_index%


Why is there a `n in there? Remove that and it should be OK.
Back to top
View user's profile Send private message
ryans



Joined: 03 Nov 2009
Posts: 5

PostPosted: Fri Nov 06, 2009 12:19 am    Post subject: FINAL CODE Reply with quote

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
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group