topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Friday December 13, 2024, 5:05 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Archive each file to separate file in its own folder  (Read 13409 times)

menych

  • Participant
  • Joined in 2015
  • *
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Archive each file to separate file in its own folder
« on: December 06, 2015, 01:49 AM »
WinRAR has a great function that allows archiving multiple files each to its own archive.

Unfortunately, it saves all of these archives to a single folder, which kind of defeats the purpose of this functionality for me.

What I want is do be able to:

1. Select multiple files in various folders on my computer (I will be using Everything search for that);
2. Either drag-and-drop them or right-click-menu them to a program that would zip each of these files separately with a predefined password, save each file to the same folder as the original, and delete the original.

For example, if I selected:

e:\some folder\@and one more folder@@@\file1.docx
d:\some folder\@and one more folder@@@\file23 34.exe
e:\some folder\@and one more folder@@@\file23 34.docx

I would like the program (let's call it tmfzip.exe) to read the contents of the tmfpass.txt file in the same folder as tmfzip.exe, and save password-protected archives as:

e:\some folder\@and one more folder@@@\file1.zip
d:\some folder\@and one more folder@@@\file23 34.zip
e:\some folder\@and one more folder@@@\file23 34.zip

and then delete the originals.

Can you please help?

Lintalist

  • Participant
  • Joined in 2015
  • *
  • Posts: 122
    • View Profile
    • Lintalist
    • Donate to Member
Re: Archive each file to separate file in its own folder
« Reply #1 on: December 06, 2015, 04:10 AM »
If you don't mind using AutoHotkey - http://autohotkey.com - you can use this script. It assumes the command line rar.exe is somewhere in your PATH. If you don't want to use or install AutoHotkey.exe you can compile the script into a Standalone exe (just remember that your password will not be protected by compiling the script). You can alter the script to read the password from an Ini file for example or ask the user to enter the password using a simple InputBox command.
#NoEnv
SetBatchLines, -1

if not A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"  ; Requires v1.0.92.01+
   ExitApp
}

; ----------

Password:="MyPassword"

#IfWinActive ahk_exe everything.exe
#z:: ; Winkey-z(ip)
ClipboardSave:=ClipboardAll
Clipboard:=""
; Ctrl-Shift-C = Copy full name to Clipboard
Send ^+c
ZipTheseFiles:=Clipboard
Clipboard:=ClipboardSave
ClipboardSave:=""
Loop, parse, ZipTheseFiles, `n, `r
{
SplitPath, A_LoopField, OutFileName, OutDir, , OutNameNoExt
; a is just archiving, replace 'a' with 'm' if you want to delete (move) the files after creating the rar
RunWait, %comspec% /c rar.exe a -ep -p%Password% "%OutDir%\%OutFileName%.rar" "%A_LoopField%",,Hide
}
MsgBox Done!
Return

menych

  • Participant
  • Joined in 2015
  • *
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: Archive each file to separate file in its own folder
« Reply #2 on: December 06, 2015, 05:08 AM »
Thank you so much for your help.

I've just installed AHK.

I've never used it before, so I'm a bit lost...

I copied your script into autohotkey.ahk and ran the program, but nothing seems to happen other than I'm getting the following output:

---- C:\Users\Unomyname\Documents\AutoHotkey.ahk
002: SetBatchLines,-1
004: if not A_IsAdmin 
012: Password := "MyPassword"
015: Return (3.38)

How do I actually use it?

Thanks again

Lintalist

  • Participant
  • Joined in 2015
  • *
  • Posts: 122
    • View Profile
    • Lintalist
    • Donate to Member
Re: Archive each file to separate file in its own folder
« Reply #3 on: December 06, 2015, 06:24 AM »
The script actually checks if Everything is the active window. If it is you can press the Windows key + Z shortcut that will copy the paths of the selected files to the clipboard and then rar each selected file into its own RAR file.

At the moment it just archives them so you can test it out, if you're satisfied with the results of the script you can repalce the 'a' with 'm' so it will move the files to the archive.

See the RAR command line help for more info and options http://ss64.com/bash/rar.html

menych

  • Participant
  • Joined in 2015
  • *
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: Archive each file to separate file in its own folder
« Reply #4 on: December 06, 2015, 06:36 AM »
OK,

So I select files in Everything, press Win-Z, I get the Done message box, but no archives are created.

I tried playing around with rar.exe in the ahk file by changing call with rar.exe to:

RunWait, %comspec% /c C:\Program Files\WinRAR\Rar.exe a -ep -p%Password% "%OutDir%\%OutFileName%.rar" "%A_LoopField%",,Hide

and

RunWait, %comspec% /c "C:\Program Files\WinRAR\Rar.exe" a -ep -p%Password% "%OutDir%\%OutFileName%.rar" "%A_LoopField%",,Hide

But the archives are still not being created.

I love the algorithm you came up with. It's brilliant. Now, we have to make sure that the commands are reaching the rar.exe, which I suspect they don't.

Is there something I can do to debug?

Lintalist

  • Participant
  • Joined in 2015
  • *
  • Posts: 122
    • View Profile
    • Lintalist
    • Donate to Member
Re: Archive each file to separate file in its own folder
« Reply #5 on: December 06, 2015, 06:49 AM »
Yes, you can debug it - just select one file for testing purposes. Replace '/c' with '/k' and remove ',,Hide' from the end, so like this
RunWait, %comspec% /k "C:\Program Files\WinRAR\Rar.exe" a -ep -p%Password% "%OutDir%\%OutFileName%.rar" "%A_LoopField%"

Don't forget to reload the script, right click on the tray icon and Reload.

What the above does:
- /k means don't close the Command window after the RunWait is done
- ,,Hide would hide the command.

Now you can see what the command line is and what type of error message if any is displayed.

menych

  • Participant
  • Joined in 2015
  • *
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: Archive each file to separate file in its own folder
« Reply #6 on: December 06, 2015, 06:53 AM »
getting an error in cmd:

'C:\Program' is not recognized as an internal or external command, operable program or batch file.

Lintalist

  • Participant
  • Joined in 2015
  • *
  • Posts: 122
    • View Profile
    • Lintalist
    • Donate to Member
Re: Archive each file to separate file in its own folder
« Reply #7 on: December 06, 2015, 06:58 AM »
So it can't find rar.exe, what happens if you copy rar.exe to the folder where your autohotkey.ahk script it, and just use
RunWait, %comspec% /k rar.exe  a -ep -p%Password% "%OutDir%\%OutFileName%.rar" "%A_LoopField%"

menych

  • Participant
  • Joined in 2015
  • *
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: Archive each file to separate file in its own folder
« Reply #8 on: December 06, 2015, 07:04 AM »
'rar.exe' is not recognized as an internal or external command, operable program or batch file.

E:\Dropbox\@@@ CLIENTS @@@\ABC Inc.>

rar.exe is now in the same folder as the AutoHotkey.ahk, which is the My Documents folder

UPDATE: interestingly, rar.exe does not work when I call it from the My Documents folder, but it does work when I copy it to the root of c:

UPDATE2: I moved the ahk file to the root of c: and it almost WORKED!!!

It archived the first file as intended. But then it stopped there.
« Last Edit: December 06, 2015, 07:11 AM by menych »

Lintalist

  • Participant
  • Joined in 2015
  • *
  • Posts: 122
    • View Profile
    • Lintalist
    • Donate to Member
Re: Archive each file to separate file in its own folder
« Reply #9 on: December 06, 2015, 08:01 AM »
Lets try it slightly differently by creating a batch file with all files and then run that file, that way you can test / tweak it until you have it working.  The same principle as above, but it create a batch file with one line commands and it calls rar from c:\program files\winrar\rar.exe which I've just tested and it works for me here. You can check the batch file rarselect.cmd to see if there are any mistakes in there.

#NoEnv
SetBatchLines, -1
SetWorkingDir, %A_ScriptDir%

if not A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"  ; Requires v1.0.92.01+
   ExitApp
}

; ----------

Password:="MyPassword"

#IfWinActive ahk_exe everything.exe
#z:: ; Winkey-z
ClipboardSave:=ClipboardAll
Clipboard:=""
; Ctrl-Shift-C = Copy full name to Clipboard
Send ^+c
Sleep 100
ZipTheseFiles:=Clipboard
Clipboard:=ClipboardSave
ClipboardSave:=""
Loop, parse, ZipTheseFiles, `n, `r
{
SplitPath, A_LoopField, OutFileName, OutDir, , OutNameNoExt
; a is just archiving, replace 'a' with 'm' if you want to delete (move) the files after creating the rar
ZipOutput .= """C:\Program Files\WinRAR\Rar.exe""" " a -ep -p" Password A_Space """" OutDir "\" OutFileName ".rar" """" A_Space """" A_LoopField """" "`n"
}
FileDelete, rarselect.cmd
FileAppend, %ZipOutput%, rarselect.cmd
RunWait %comspec% /k rarselect.cmd
ZipTheseFiles:="", ZipOutput:=""
Return

menych

  • Participant
  • Joined in 2015
  • *
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: Archive each file to separate file in its own folder
« Reply #10 on: December 06, 2015, 11:31 AM »
It worked!!!!

You are a genius! Thank you!

Four more questions:

1) So, to delete files after, I'd replace 'a' with 'm'?

2) How would I change it to hide command line after execution?

3) It's my very first experience with DC, how do donations work and what's the proper amount?

4) Can AHK be used to fill a multipage online form on one server by taking values from an external file or from another web page?

THANKS AGAIN!

Lintalist

  • Participant
  • Joined in 2015
  • *
  • Posts: 122
    • View Profile
    • Lintalist
    • Donate to Member
Re: Archive each file to separate file in its own folder
« Reply #11 on: December 06, 2015, 12:39 PM »
Good to hear it works. 8)

1: Yes.
2: Like so: replace /k with /c and add ,,Hide
RunWait %comspec% /c rarselect.cmd,,Hide
3: I don't know how it actually works (I don't want anything)
4: Yes most certainly:

The recommended browser would be IE so you can reliably automate the process. Other browsers: can be done but would have to rely on sending text to each field in the form similar to copy/paste manually.

With IE & COM you can actually SET the text of each field which will be much more reliable.
Some tutorials (looks a bit daunting I'll admit):
https://autohotkey.c...script-com-tutorial/ and
https://autohotkey.c...c.php?f=7&t=7822 - some info/posts on the AutoHotkey forums will tell you need AutoHotkey_L but that is now the main branch of AutoHotkey which you already have so don't worry about that part.

For help on IE & COM related scripts is best to post them at https://autohotkey.com/boards/ as there are a few experts there that will be able to assist (if you put the effort in).

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Re: Archive each file to separate file in its own folder
« Reply #12 on: December 06, 2015, 06:43 PM »
4) Can AHK be used to fill a multipage online form on one server by taking values from an external file or from another web page?

You might find that easier to do using Powershell, there's a lot of info available on IE automation using it.