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 

Copying a file and renaming it with a prefix in a new folder

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



Joined: 31 Oct 2009
Posts: 2

PostPosted: Sat Oct 31, 2009 5:48 am    Post subject: Copying a file and renaming it with a prefix in a new folder Reply with quote

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



Joined: 08 Apr 2009
Posts: 5336
Location: San Diego, California

PostPosted: Sat Oct 31, 2009 8:04 am    Post subject: Reply with quote

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



Joined: 31 Oct 2009
Posts: 2

PostPosted: Sun Nov 01, 2009 12:24 pm    Post subject: Reply with quote

wow thank you.. you did all the work for me. it works like a champ.

only problem is I don't understand the code. Sad.. % % are always variables right? Why does
Code:
StringRight, Var, Var, 2
add another zero every copy if you add another , Var?
Back to top
View user's profile Send private message
pajenn



Joined: 07 Feb 2009
Posts: 384

PostPosted: Sun Nov 01, 2009 2:09 pm    Post subject: Reply with quote

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. Sad.. % % 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.
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