AutoHotkey Community

It is currently May 26th, 2012, 10:56 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: October 31st, 2009, 6:48 am 
Offline

Joined: October 31st, 2009, 6:35 am
Posts: 2
Well I spent a good 2 hours trying to figure out how the hell to have autohotkey copy a file to another directory, while renaming it with a prefix each time win+c is pressed. I imagined having autohotkey add a prefix to the name of the file copied to the new folder such as:

record01.dem
record02.dem
record03.dem
record04.dem ..etc

Here is my first failed attempt, but it's the closest I got to getting this to work:

Code:
*#c::FileCopy, C:\Program Files\Steam\steamapps\chief_ace\counter-strike source\cstrike\record.dem, C:\Documents and Settings\USER\Desktop\demos\record*.dem


I tried using the integer variable, but I couldn't get it working. Then I tried arrays, and well, I had the same problem only that I was even far more confused, but I think I'm supposed to be using arrays to have autohotkey add a prefix? Do I have the script retrieve the latest number from a txt?

Here is my attempt at using the FileCopyFolders, editing the example provided in the help html:

Code:
ErrorCount := CopyFilesAndFolders("C:\Program Files\Steam\steamapps\chief_ace\counter-strike source\cstrike\record.dem", "C:\Documents and Settings\USER\Desktop\demos\")
if ErrorCount <> 0
    MsgBox %ErrorCount% files/folders could not be copied.

CopyFilesAndFolders(SourcePattern, DestinationFolder, DoOverwrite = false)
; Copies all files and folders matching SourcePattern into the folder named DestinationFolder and
; returns the number of files/folders that could not be copied.
{
    ; First copy all the files (but not the folders):
    FileCopy, %SourcePattern%, %DestinationFolder%, %DoOverwrite%
    ErrorCount := ErrorLevel
    ; Now copy all the folders:
    Loop, %SourcePattern%, 2  ; 2 means "retrieve folders only".
    {
        FileCopyDir, %A_LoopFileFullPath%, %DestinationFolder%\%A_LoopFileName%, %DoOverwrite%
        ErrorCount += ErrorLevel
        if ErrorLevel  ; Report each problem folder by name.
            MsgBox Could not copy %A_LoopFileFullPath% into %DestinationFolder%.
    }
    return ErrorCount
}


Here is my attempt at using arrays, editing the example provided in the arrays section, which never worked:

Code:
; Write to the array:
ArrayCount = 0
Loop, Read, C:\Documents and Settings\USER\Desktop\demos\Array.txt   ; This loop retrieves each line from the file, one at a time.
{
    ArrayCount += 1  ; Keep track of how many items are in the array.
    Array%ArrayCount% := A_LoopReadLine  ; Store this line in the next array element.
}

; Read from the array:
Loop %ArrayCount%
{
    ; The following line uses the := operator to retrieve an array element:
    element := Array%A_Index%  ; A_Index is a built-in variable.
    ; Alternatively, you could use the "% " prefix to make MsgBox or some other command expression-capable:
    MsgBox % "Element number " . A_Index . " is " . Array%A_Index%
}

*#c::FileAppend, Var := Array%A_Index% + 1, C:\Program Files\Steam\steamapps\chief_ace\counter-strike source\cstrike\record.dem, C:\Documents and Settings\USER\Desktop\demos\*.*


Could someone please lead me in the right direction? Thanks


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 31st, 2009, 9:04 am 
Online

Joined: April 8th, 2009, 7:49 pm
Posts: 6067
Location: San Diego, California
tested, minimal error checking, no verify that write passed

you change the hotkey :wink:

Code:
;FullFileName= C:\Users\Leef_me\Documents\Downloaded Files\junk274.ahk
FullFileName= C:\Program Files\Steam\steamapps\chief_ace\counter-strike source\cstrike\record.dem

;dest_dir=C:\Users\Leef_me\Documents\

dest_dir=C:\Documents and Settings\USER\Desktop\demos\

SplitPath, FullFileName, name, dir, ext, name_no_ext, drive

maxcopies=5

return


f1::


loop, %maxcopies%
{

  ;msgbox full:%FullFileName%`nname:%name%`ndir:%dir%`next:%ext%`nname_wo_ext:%name_no_ext%`ndrive:%drive%
  var:=a_index
  Var := "0000000000" . Var     ; The quotes contain 10 spaces.  To pad with zeros, substitute zeros for the spaces.
  StringRight, Var, Var, 2  ; This pads the number in Var with enough spaces to make its total width 10 characters.

  dest_filename=%dest_dir%%name_no_ext%%var%.%ext%

  IfnotExist, %dest_filename%
     break

  if a_index=%maxcopies%
    dest_filename=
}

if dest_filename=
{
  msgbox nothing available
}
else
{
  msgbox %dest_filename%
  FileCopy, %FullFileName%, %dest_filename%
}
return


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 1st, 2009, 1:24 pm 
Offline

Joined: October 31st, 2009, 6:35 am
Posts: 2
wow thank you.. you did all the work for me. it works like a champ.

only problem is I don't understand the code. :(.. % % are always variables right? Why does
Code:
StringRight, Var, Var, 2
add another zero every copy if you add another , Var?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 1st, 2009, 3:09 pm 
Offline

Joined: February 7th, 2009, 11:28 pm
Posts: 384
DappleApple wrote:
wow thank you.. you did all the work for me. it works like a champ.

only problem is I don't understand the code. :(.. % % are always variables right? Why does
Code:
StringRight, Var, Var, 2
add another zero every copy if you add another , Var?


%var% = contents of variable var. For example, if var = Program Files, then

Code:
FileCopy, c:\boot.ini, c:\%var%\boot.ini


would copy boot.ini to C:\program files, whereas

Code:
FileCopy, c:\boot.ini, c:\var\boot.ini


would try to copy boot.ini to c:\var folder.

With commands like

Code:
StringRight, Var, Var, 2


the %-signs are not necessary because StringRight command operates on the contents of the input variable by default. However, when to use %-signs and when not to is one of the confusing aspects of AHK.

and
Code:
StringRight, Var, Var, 2
does not add anything, it simply saves the last two characters from string var into variable var, or put another way, it replaces string var by var's last to characters.

_________________
Hardware: 1.8 GHz laptop with 4 GB ram, Windows XP/SP3
Software: Prevx, Privatefirewall, KeyScrambler.


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: hyper_, JSLover, Kirtman, Leef_me, Maestr0, Miguel, XstatyK and 61 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