Jump to content

how to make UDF ?


microbious
 Share

Recommended Posts

HI guys

i want to make my self a XCOPY udf but i dont know where to start. i looked into other udf's but still dont understadn how it works

can u guys point me to the right direction how to make xcopy from command prompt udf so in my future scripts i can just type

xcopy ("dir or file","destination", "flags")

flags are those used by xcopy.exe

i think i need to reference those in udf somehow and then include it in script that will be using it but not sure where to start

Edited by Gettingsmarter
Link to comment
Share on other sites

Look into helpfile at:

Func

RunWait

#include

yes thanks i know how to execute xcopy and give all parameters i need like this.

RunWait(@ComSpec & ' /c xcopy "Source\My Documents" "%USERPROFILE%\My Documents"/Y',"",@SW_HIDE)

What i need to do is to create UDF that will allow me to make this script more autoit like.

Something like this:

#include <my xcopy udf.au3>
xcopy($source, $destination,[flag],[working dir],@SW_HIDE

[flag] are switches xcopy.exe understands

Reason why i need this is because runwait (@comspec blah blah) is way to long.

Plus giving xcopy.exe $variables instead of windows environmental variables and/or direct paths is not gonna work, thats why i wanted to create this UDF to make copy files and folders eazy due to autoit lack of this ability.

yes autoit can figure out file date/time/attributes/ overwrite or not and so on but its to complex, instead it could be replaced with single line of code with couple of $vars and flags

the xcopy udf needs something in it where it would interpret all those parameters into once xcopy.exe understands and i dont know where to start from.

Thanks for reply

Edited by Gettingsmarter
Link to comment
Share on other sites

Hi man,

Not sure if this is what you're after or not, but this is a function to pass parameters to Xcopy:

;This example copies all files in C:\test\ to a folder "test" on the desktop

xcopy("C:\test", @DesktopDir & '\test\', "/Y" , @ScriptDir, @SW_HIDE)

Func xcopy($Source, $Dest, $params, $WorkingDir = "", $hide = @SW_SHOW)
    RunWait(@ComSpec & ' /c xcopy "' & $Source & '" "' & $Dest & '" ' & $params , $WorkingDir, $hide)
EndFunc

NOTE: There is no error checking in this script, it was just a quick example.

As for no functionality in autoit... what do you mean?

Have you tried FileCopy() or DirCopy()?

Edited by danwilli
Link to comment
Share on other sites

As for no functionality in autoit... what do you mean?

Have you tried FileCopy() or DirCopy()?

yes i did and the whole reason why i need to make custom UDF is because autoit cannot copy only those files and folders that are newer then in destination folder. Well it can but the codding is to big. you would have to create ton of $vars figure out file date/compare lots of If's and Then's is just not the way to go.

Xcopy can do it with switch /D by date, so it will only copy files that are newer then once already copied (as simple as that)

yes if i use RunWait (@comspec blah blah blah) i will not have to create all those parameters that will figure out if files is newer or older and then copy it if its newer but still this is going to be big coding.

Instead i wish there was a UDF that has something in it making it allot easier to use xcopy.

For example if right now to copy file using Xcopy.exe i need to type this code:

RunWait(@ComSpec & ' /c xcopy /E "example\source\folde" "%userprofile%\dest\folder" /Y')@

If i had my custom UDF (one am trying to create) i would end up with:

xcopy ($source, $dest, $workdir, [flag],[extra options])

As u can see, if i had custom (i call it xcopy UDF) i would end up coding half of the above example, if i did not use @comspec then i would end up coding about 5% of the whole code that would have to be used to copy only newer files.

This is SAVE TON OF TIME right here man, i just cant figure out where to start.

All UDF examples i found on forum are very complicated and not referencing to any executable file so i have no clue where to get the idea how to make one.

Your example is interesting but i am not sure 100% that i get it or if it does the job i need :D

I hope u understand what i need.

Thanks for trying to help

Link to comment
Share on other sites

hey i tried

[code#include <xcopy.au3>

$Source = "C:\file"

$Dest = "C:\Users\Administrator\Desktop"

$params = "/Y"

$WorkingDir = @ScriptDir

xcopy($Source, $Dest, $params, "","")]

where <xcopy.au3> is

Func xcopy($Source, $Dest, $params, $WorkingDir = "", $hide = @SW_SHOW)
    RunWait(@ComSpec & ' /c xcopy "' & $Source & '" "' & $Dest & '" ' & $params , $WorkingDir, $hide)
EndFunc

it worked

i dont know how it works but i guess ill figure it out later

Man my script is way smaller because of this

Thanks allot

Link to comment
Share on other sites

danvilli

can u help me understand quotes ?

i am trying to change your example but end up having ton of errors

i added new variable that will represent switch for CMD "/c"

Func xcopy($WindAct $Source, $Dest, $Switch, $Display)
    RunWait(@ComSpec & '$WindAct &' "' & xcopy &"' "' & $Source &'" "' & $Dest &'"' & $Switch, $Display)
EndFunc

am sure there is something wrong with quotes

can u tell me what ? i only learned that i can use quotes for things like '"something" $ "something else"'

but this is so many of them am lost lol

thanks man i owe u big one

Link to comment
Share on other sites

  • Moderators

danvilli

can u help me understand quotes ?

i am trying to change your example but end up having ton of errors

i added new variable that will represent switch for CMD "/c"

Func xcopy($WindAct $Source, $Dest, $Switch, $Display)
    RunWait(@ComSpec & '$WindAct &' "' & xcopy &"' "' & $Source &'" "' & $Dest &'"' & $Switch, $Display)
EndFunc

am sure there is something wrong with quotes

can u tell me what ? i only learned that i can use quotes for things like '"something" $ "something else"'

but this is so many of them am lost lol

thanks man i owe u big one

You're sure?

RunWait(@ComSpec & '$WindAct &' "' & xcopy &"' "' & $Source &'" "' & $Dest &'"' & $Switch, $Display)

If you view this in SciTe you'll be able to see where you're messing up, and adjust the quotes as need be.

Edit:

Remember that single quotes are still quotes in autoit.

So trying to do:

'"I' & "'ve been quoted" & '"'
is the same as doing
"""I've been quoted"""
and vice versa. 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

ok can u guys tell me what does separate these $vars,

this ? '" "'

this ? & '" "' &

or $var hs to be inside this ? "' & & '"

I still dont understand how these quotes work

main

#include <xcopy.au3>
$Flag = "/K"
$Source = "C:\3dsmax 2010 launcher.config"
$Dest = "C:\Users\Administrator\Desktop"
$Switch = "/Y"
xcopy($Flag, $Source, $Dest, $Switch)

xcopy

Func xcopy($Flag, $Source, $Dest, $Switch)
RunWait(@ComSpec & $Flag &' xcopy "' & $Source & '" "' & $Dest & '" ' & $Switch)
sleep (3000)
EndFunc
.

But i inderstand that if i remove $Flag and add /K or /C before xcopy "' it will work.

i need /k or /C to be variable so i can change it because sometimes i need to see the console window of CMD to see what its doing so i have to be able to change it without changing the UDF

Edited by Gettingsmarter
Link to comment
Share on other sites

& is for string concatenation.

e.g. "a" & "b" = "ab"

In AutoIt, string literals can be defined using either a pair of single or double quotes as delimiters.

e.g. 'text' or "text"

To get a double quote to appear inside a printed string, you just use two double quotes or encase the string literal with single quotes.

e.g. 'he said, "text"' or "he said, ""text"""

Link to comment
Share on other sites

& is for string concatenation.

e.g. "a" & "b" = "ab"

In AutoIt, string literals can be defined using either a pair of single or double quotes as delimiters.

e.g. 'text' or "text"

To get a double quote to appear inside a printed string, you just use two double quotes or encase the string literal with single quotes.

e.g. 'he said, "text"' or "he said, ""text"""

yes thank you i know how to deal with text but this is no text, this is a combination of $Vars and text whic i cant figure out how to combine so they work.

Tell me what am i doing wrong here ?

RunWait(@ComSpec & $Flag &' xcopy "' & $Source & '" "' & $Dest & '" ' & $Switch)

$Flag used to be /K so it was RunWait(@ComSpec &' /K xcopy "'.................

Because /K need's to be variable its not working.

I need it to be variable so i wont have to change UDF for each script and most of my scripts involve TON of xcopy commands (i like to backup restore stuff :D )

Obviously my combination of $vars and text is not working.

can u help ?

Edited by Gettingsmarter
Link to comment
Share on other sites

Ah. Does your $Flag contain a preceding whitespace?

You can check if your parameter to the RunWait function is correct by sticking it into a MsgBox before it gets passed to RunWait.

MsgBox(0x2000, "Title", $text)

It may just be the case that you're missing a whitespace or quote somewhere.

Edited by omikron48
Link to comment
Share on other sites

Ah. Does your $Flag contain a preceding whitespace?

You can check if your parameter to the RunWait function is correct by sticking it into a MsgBox before

u did reed , did u ?

am sorry but am sure that told (not u) that if i remove #Flag it works but i need $Flag because i need to be able to change /K to /C sometimes without changing UDF

i tried all night before i collapsed and fell asleep but today i feel lucky, maybe someone will try the script and tell me exactly what i did wrong

i mean script is simple, how hard would it be to try it your self and see whats wrong ?

thanks

Edited by almostnotstupid
Link to comment
Share on other sites

Did you even bother to answer my question?

#include <xcopy.au3>
$Flag = "/K"

Your $Flag has no preceding whitespace.

This:

$Flag = "/K"
$Source = "C:\3dsmax 2010 launcher.config"
$Dest = "C:\Users\Administrator\Desktop"
$Switch = "/Y"
xcopy($Flag, $Source, $Dest, $Switch)

Func xcopy($Flag, $Source, $Dest, $Switch)
MsgBox(0x2000, "Title", @ComSpec & $Flag &' xcopy "' & $Source & '" "' & $Dest & '" ' & $Switch)
EndFunc

Nets me this input string:

'C:\WINDOWS\system32\cmd.exe/K xcopy "C:\3dsmax 2010 launcher.config" "C:\Users\Administrator\Desktop" /Y'

There's no space between 'cmd.exe' and '/K'. The solution to your problem is inserting that missing space.

Edited by omikron48
Link to comment
Share on other sites

Did you even bother to answer my question?

There's no space between 'cmd.exe' and '/K'. The solution to your problem is inserting that missing space.

ok now i see whats wrong.

how do i get $Flag = " /K" to be $Flag = "/K" with no space ?

i tried changing UDF like this

RunWait(@ComSpec & $Flag & " " &' xcopy "' & $Source & '" "' & $Dest & '" ' & $Switch)

but its not working.

am guessing that & " " would have added that missing space instead of making it in the scrip as " /K" but its not

Link to comment
Share on other sites

ok got it

RunWait(@ComSpec & " " & $Flag &' xcopy "' & $Source & '" "' & $Dest & '" ' & $Switch)

works

thanks

last question

how to make it so each time i use command

xcop(

as soon as i type it it would pop up variables description like any other autoit command does ?

Edited by almostnotstupid
Link to comment
Share on other sites

last question

how to make it so each time i use command

xcop(

as soon as i type it it would pop up variables description like any other autoit command does ?

Create file "c:\Program Files\AutoIt3\SciTE\api\au3.user.calltips.api"

in the same way as it is in "c:\Program Files\AutoIt3\SciTE\api\au3.api"

Link to comment
Share on other sites

Create file "c:\Program Files\AutoIt3\SciTE\api\au3.user.calltips.api"

in the same way as it is in "c:\Program Files\AutoIt3\SciTE\api\au3.api"

au3.user.calltips.api is allready there.

i had to add

xcopy ( "CMD console action", "Source", "Destination", "xcopy switch" ) used to copy files using xcopy CMD command

Thanks guys

thank you all i have learned so much all thanks to you

Edited by almostnotstupid
Link to comment
Share on other sites

  • 3 weeks later...

sorry for asking again but i got confused with my own creation.

Func xcopy($Flag, $Switch, $Source, $Dest)
RunWait(@ComSpec & " " & $Flag &' xcopy "' & $Switch & '" "' & $Source & '" ' & $Dest)
EndFunc

if

#Include <xcopy.au3>
xcopy ("/k", "/E/D/Y", "C:\test", @DesktopDir)

code used, i get error xcopy says invalid number of arguments.

I cant figure out why :)

can u guys help me one more time ?

It looks like everything is right but it's just not working like it did minute ago

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