Sign in to follow this  
Followers 0
Best4FREE

How to Copy a file and name it random like screenshot 1 sreenshot 2....

6 posts in this topic

Hi can someone give me a code that Copy a file and save it in a other folder under random name ?

Share this post


Link to post
Share on other sites



Like This?

$srcfile = FileOpenDialog("Copy File",@WorkingDir,"(*.*)")
If Not FileExists($srcfile) Then Exit
$destfolder = FileSelectFolder("Save to","")
If Not FileExists($destfolder) Then Exit

$rnd = Random(1,100,1)
Do
    If StringLen($rnd) < 3 Then $rnd = "0" & $rnd
Until StringLen($rnd) = 3

$extension = "." & StringRight($srcfile,StringLen($srcfile)-StringInstr($srcfile,"."))
$filename = "screenshot"
$file = $destfolder & "\" & $filename & $rnd & $extension
FileCopy($srcfile,$file)

If FileExists($file) Then
    Msgbox(0,"Done.","Filed Copied Successfully." & @CRLF & $srcfile & @CRLF & $file)
Else
    Msgbox(16,"Failed.","Failed to Copy File." & @CRLF & $srcfile & @CRLF & $file)
EndIf

Share this post


Link to post
Share on other sites

Like This?

$srcfile = FileOpenDialog("Copy File",@WorkingDir,"(*.*)")
If Not FileExists($srcfile) Then Exit
$destfolder = FileSelectFolder("Save to","")
If Not FileExists($destfolder) Then Exit

$rnd = Random(1,100,1)
Do
    If StringLen($rnd) < 3 Then $rnd = "0" & $rnd
Until StringLen($rnd) = 3

$extension = "." & StringRight($srcfile,StringLen($srcfile)-StringInstr($srcfile,"."))
$filename = "screenshot"
$file = $destfolder & "\" & $filename & $rnd & $extension
FileCopy($srcfile,$file)

If FileExists($file) Then
    Msgbox(0,"Done.","Filed Copied Successfully." & @CRLF & $srcfile & @CRLF & $file)
Else
    Msgbox(16,"Failed.","Failed to Copy File." & @CRLF & $srcfile & @CRLF & $file)
EndIf
Ummm.

If you want a random number with 3 digits then you might as well use Random(100,999,1). Using 1 to 100 only allows for 100 different files which might not be enough. I don't know if 900 is enough either.

However, you need to ensure that the "random" number isn't repeated or a file will get overwritten.

The easiest way (I think) to generate a unique number, if that is what is needed rather than a random number, is to use the date and time which is never repeated.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

That was just an example. I'd say you could even do Random(1,999,1). The only reason I put zeros in front (if length isn't full) is so files are sorted properly.

Also, yes the file will overwrite with this code, but it's not hard to add a check for that.

Edited by spudw2k

Share this post


Link to post
Share on other sites

That was just an example. I'd say you could even do Random(1,999,1). The only reason I put zeros in front (if length isn't full) is so files are sorted properly.

OK.

Also, yes the file will overwrite with this code, but it's not hard to add a check for that.

Agreed

do
 $randomNum = Random(100,999,1)
until not FileExists($path & "\" & $randomNum & ".xyz")

Which is ok until you have 900 files and then it will get stuck. With the date way there is no problem with duplicates and displaying by name will also give them in date order.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Which is ok until you have 900 files and then it will get stuck. With the date way there is no problem with duplicates and displaying by name will also give them in date order.

Fair 'nough. Didn't think about that.

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
Sign in to follow this  
Followers 0