AutoHotkey Community

It is currently May 27th, 2012, 11:03 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: March 28th, 2007, 9:17 pm 
I'm trying to make a script that does the following: it takes a file I've selected, copies it, and then sends it to whatever directory the file happens to be in -- but with an altered file name to show its backup status.

Here's what I mean. Say I've got C:\Folder1\Example.doc, and I run the script. The script would then produce, say, C:\Folder1\Example-1.doc. Or, if I'm in C:\Folder2\Example.txt, the script would make C:\Folder2\Example-1.txt.

Is there a way to do this? Is there a way to do this with a batch of files?

My best shot—this shows how helpless I am, lol—is something like this for an individual file:

Code:
Send ^c
Sleep 300

SetWorkingDir, %clipboard%

FileCopy, %clipboard%, %A_WorkingDir%\*-1.*


Uh, that doesn't work. Should FileSelectFolder be involved? Help!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 29th, 2007, 9:34 am 
Offline

Joined: December 23rd, 2006, 6:02 pm
Posts: 424
Location: Russia
Suppose C:\Folder1\Example-1.doc already exists, what should be done?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 29th, 2007, 10:01 am 
Quote:
Is there a way to do this?
Yes.
Quote:
Is there a way to do this with a batch of files?
Yes.

RegExMatch() ; to extract the number from the folders/path name
Loop, <FilePattern> ; to parse the folder
FileMove ; to backup the files

Quote:
Suppose C:\Folder1\Example-1.doc already exists, what should be done?
FileMove, SourcePattern, DestPattern [, Flag]


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 29th, 2007, 3:04 pm 
Starting point: Function: IncrementName (create next file)


Report this post
Top
  
Reply with quote  
PostPosted: March 30th, 2007, 3:24 pm 
Offline

Joined: March 30th, 2007, 2:48 pm
Posts: 5
YMP wrote:
Suppose C:\Folder1\Example-1.doc already exists, what should be done?


Well, ideally it'd save the next version with a "-2" at the end of its name. And so on ("-3," "-4," etc.). But that's way ahead of where I am now. I'm a newbie. (Obviously.) I'm trying to figure out the basics first!

Let me ask a slightly different question. It's probably a basic one, and I'm sorry—I've tried for hours to figure this out for myself. Let's say that you've selected a file on your computer; that is, you haven't opened it but rather, say, single-clicked it with your mouse. Is there a way, using AutoHotkey, to get that file's directory location? I know you can easily get that file's full name by doing this:

Code:
Send ^c
{whatever command you want} %clipboard%

That'll include both the directory location and the file name. Can you please show me a way that I can get only the directory location? Because then, I figure, I can just run a variant of FileCopy.

Perhaps, for example, I can temporarily make whatever folder I'm in into the working directory. Right now, when I run a script fragment like the one I showed above:

Code:
SetWorkingDir, %clipboard%

FileCopy, %clipboard%, %A_WorkingDir%\*-1.*

it doesn't work. If, however, instead of "%clipboard%" I could substitute, you know, something signifying "whatever folder is currently active," then I think it'd work. (This would kind of be like WinGetActiveTitle except that, instead from giving me the title of the active window, I'd get the location.)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 30th, 2007, 3:28 pm 
Offline

Joined: March 30th, 2007, 2:48 pm
Posts: 5
BoBo wrote:
RegExMatch() ; to extract the number from the folders/path name
Loop, <FilePattern> ; to parse the folder

I'm sorry; I don't understand how to use those commands to get me where I'm going. Could you please explain or show me an example?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 30th, 2007, 3:43 pm 
Smirkles wrote:
BoBo wrote:
RegExMatch()
I'm sorry

In case it went unnoticed, the blue stuff on my message is a llink to a message which might solve or help to solve your problem.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 30th, 2007, 4:01 pm 
Offline

Joined: March 30th, 2007, 2:48 pm
Posts: 5
No, no. I saw your post, Helpy. Thanks! If I’m not mistaken, however, that script fragment generates iterations of name files—the “-1,” “-2,” etc. That’s very useful! But again, I’m still focusing most on the first part of my problem, which I think is about directory identification.

If I knew how to identify selected files as part of a Loop sequence, then perhaps I could use “A_LoopFileDir.” I don’t know.


(If I’m wrong, I apologize. I’m trying my best.)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 30th, 2007, 7:19 pm 
Offline

Joined: March 30th, 2007, 2:48 pm
Posts: 5
OK, I know you’re all on the edges of your seats. Finally I found (and shamelessly appropriated) this script by PhiLho. With some modifications, I get this:

Code:
 
#IfWinActive ahk_class Progman
#7::
   explorerClass = Progman
Goto MassRename
#IfWinActive ahk_class ExploreWClass
#7::
   explorerClass = ExploreWClass
Goto MassRename
#IfWinActive ahk_class CabinetWClass
#7::
   explorerClass = CabinetWClass
Goto MassRename
#IfWinActive

MassRename:
   appName = Mass Rename
   ; Get current path in Explorer (works even when Address bar is hidden)
   ControlGetText currentPath, Edit1, ahk_class %explorerClass%
   ; Get the list of selected files in Windows Explorer
   ControlGet selectedFiles, List, Selected Col1, SysListView321, ahk_class %explorerClass%
   If selectedFiles =   ; Blank
   {
      MsgBox 16, %appName%, Nothing to rename! Please, select the files to rename before running %appName%.
      Return
   }
   renamedFiles = In %currentPath%`n`n
   yolderFiles = In %currentPath%`n
   Loop Parse, selectedFiles, `n  ; Rows are delimited by linefeeds (`n).
   {
      Loop %currentPath%\%A_LoopField%*
      {
         ; Get the full name
         fullName := A_LoopFileName
      }
      SplitPath fullName, , , ext, nameNoExt
      if (ext == "")
      {
         newName = %nameNoExt%%suffix%
      }
      Else
      {
         newName = %nameNoExt%%suffix%.%ext%
      }
      Filecopy %currentPath%\%fullName%, %currentPath%\x%fullName%
      yolderfiles = %yolderFiles%%fullName% -] %newName% (%ErrorLevel%)`n
   }
Return


That code looks like it’ll take any file and copy it to the same folder, but with an “x” in front of the file name. Hey, to me, that’s progress!

Except: it appears to work everywhere but a) the desktop and (2) My Documents. Weird. I don’t know why it doesn’t work in those places. (I’ve altered the beginning of PhiLho’s script to include an “ahk_class Progman” category; still the script doesn’t work on the Desktop.)

I’m also trying to integrate this script with the one that Helpy helpfully pointed out here (also by PhiLho).

Any ideas?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 30th, 2007, 10:49 pm 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
Desktop and My Documents are specials, virtual folders, like the Control Panel. Now, since you can know you are there, you can just get the corresponding paths in special AutoHotkey variables: A_Desktop and A_MyDocuments.

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2007, 8:06 am 
Offline

Joined: December 23rd, 2006, 6:02 pm
Posts: 424
Location: Russia
Look at this example, perhaps it will be sufficient for your purpose.
Code:
F11::
  Clipboard=
  Send, ^c
  ClipWait, 2
  If ErrorLevel
  {
    Msgbox, The attempt to copy files onto the clipboard failed.
    Return
  }
  FileList=%Clipboard%
  Loop, Parse, FileList, `n, `r   ; Process the files one by one.
  {
    SourceName=%A_LoopField%
    SplitPath, SourceName,, Folder, Ext, Name  ; Parse the file's path.
    Loop                                       ; Create a new name.
    {
      DestName=%Folder%\%Name%-%A_Index%.%Ext%
      IfNotExist, %DestName%   ; Check to see if the name is already used.
        Break
    }
    FileCopy, %SourceName%, %DestName%
  }   
Return


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2007, 9:37 pm 
Offline

Joined: March 30th, 2007, 2:48 pm
Posts: 5
Thank you so much, YMP! With just a few alterations, that works perfectly.

You're a genius!


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], BrandonHotkey, Edd, Exabot [Bot], Google Feedfetcher, HotkeyStick, Yahoo [Bot] and 13 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