File swapper

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Bluerose555
Posts: 111
Joined: 20 Mar 2017, 03:02

File swapper

Post by Bluerose555 » 17 Oct 2021, 18:00

Greetings. I am looking for a way to use AHK to swap out files for testing, but would like it to make backups of the original files. Essentially what I want to do is:

1 - Have a small interface that has two data fields: SOURCE and DESTINATION.

In 'SOURCE' you would choose the file you want to copy, and in 'DESTINATION' you would select the file to be overwritten from the 'SOURCE' file.

2 - since the destination file may need to be restored, it think what would work would be that the script would copy the NAME of the DESTINATION file, then rename the file to have a .bak extension (to prevent it from being read from the other program), and then to write the contents of the SOURCE file to a new file with the copied name (basically making a new file and leaving the original in place.. with a .bak extension).

There is more functionality I want to add to this later, but for now I will just need help with the main part of:

1- copying DESTINATION file name
2 - adding .bak extension to the DESTINATION file.
3 - copying SOURCE file to new file and renamed with copied DESTINATION file name.

Any help is appreciated!

User avatar
mikeyww
Posts: 26435
Joined: 09 Sep 2014, 18:38

Re: File swapper

Post by mikeyww » 17 Oct 2021, 18:37

Code: Select all

Gui, Font, s10
Gui, Add, Button,                   , Source
Gui, Add, Button,                   , Target
Gui, Add, Edit  , ym w600 vf1 gCheck
Gui, Add, Edit  ,    wp   vf2 gCheck
Gui, Add, Button,                   , Copy
Gosub, Check
Gosub, F3
F3::Gui, Show,, Copy file (press F3 to show this dialog window again)

Check:
Gui, Submit, NoHide
GuiControl, % FileExist(f1) && FileExist(f2) ? "Enable" : "Disable", Copy
Return

ButtonSource:
FileSelectFile, f1, 1,, Select source file
If ErrorLevel
 Return
GuiControl,, f1, %f1%
GuiControl, Focus, Target
Return

ButtonTarget:
FileSelectFile, f2, S17,, Select target file
If ErrorLevel
 Return
GuiControl,, f2, %f2%
Sleep, 100
GuiControl, Focus, Copy
Return

ButtonCopy:
Gui, Submit
FileRecycle, % bak := f2 ".bak"
FileMove, %f2%, %bak%
If ErrorLevel {
 MsgBox, 48, Error, An error occurred when backing up the file.`n`n%f2%
 Return
} Else FileCopy, %f1%, %f2%
If ErrorLevel
     MsgBox, 48, Failure, An error occurred when copying the file.`n`n%f1%
Else MsgBox, 64, Success, Done!
Return

GuiEscape:
Gui, Hide
Return

Bluerose555
Posts: 111
Joined: 20 Mar 2017, 03:02

Re: File swapper

Post by Bluerose555 » 17 Oct 2021, 21:47

This is amazing! thank you. I will continue to study this !

Bluerose555
Posts: 111
Joined: 20 Mar 2017, 03:02

Re: File swapper

Post by Bluerose555 » 17 Oct 2021, 21:53

Ok this works amazing. Could you help me figure out a way to 'undo' the operation once its completed? Maybe, like a restore button something?

User avatar
mikeyww
Posts: 26435
Joined: 09 Sep 2014, 18:38

Re: File swapper

Post by mikeyww » 17 Oct 2021, 22:04

Code: Select all

Gui, Font, s10
Gui, Add, Button,                       , Source
Gui, Add, Button,                       , Target
Gui, Add, Edit  ,     ym w600 vf1 gCheck
Gui, Add, Edit  ,        wp   vf2 gCheck
Gui, Add, Button,                       , Copy
Gui, Add, Button, x+m                   , Undo
Gosub, F3
F3::
Gosub, Check
Gui, Show,, Copy file (press F3 to show this dialog window again)
Return

Check:
Gui, Submit, NoHide
GuiControl, % FileExist(f1) && FileExist(f2) ? "Enable" : "Disable", Copy
GuiControl, % FileExist(f2 ".bak") ? "Enable" : "Disable", Undo
Return

ButtonSource:
FileSelectFile, f1, 1,, Select source file
If ErrorLevel
 Return
GuiControl,, f1, %f1%
GuiControl, Focus, Target
Return

ButtonTarget:
FileSelectFile, f2, S17,, Select target file
If ErrorLevel
 Return
GuiControl,, f2, %f2%
Sleep, 100
GuiControl, Focus, Copy
Return

ButtonCopy:
Gui, Submit
FileRecycle, % bak := f2 ".bak"
FileMove, %f2%, %bak%
If ErrorLevel {
 MsgBox, 48, Error, An error occurred when backing up the file.`n`n%f2%
 Return
} Else FileCopy, %f1%, %f2%
If ErrorLevel
     MsgBox, 48, Failure, An error occurred when copying the file.`n`n%f1%
Else MsgBox, 64, Success, Done!
Return

ButtonUndo:
Gui, Submit
FileRecycle, %f2%
FileMove, %f2%.bak, %f2%
If ErrorLevel
     MsgBox, 48, Failure, An error occurred when restoring the file.`n`n%f2%
Else MsgBox, 64, Success, Done!
Return

GuiEscape:
Gui, Hide
Return

Bluerose555
Posts: 111
Joined: 20 Mar 2017, 03:02

Re: File swapper

Post by Bluerose555 » 18 Oct 2021, 01:48

Mikey, is there anything you cant do?!! WOW. Amazing. I have on last thing to ask about this.

After your amazing script has replaced a file, I wanted it to launch another script and I copied certain parts of your GUI and button code and made it work! I loved that you took time to help me with a working example. I can see this being very useful for other things I want to make!

Thanks!

Bluerose555
Posts: 111
Joined: 20 Mar 2017, 03:02

Re: File swapper

Post by Bluerose555 » 18 Oct 2021, 03:30

One thing I would like to implement for a variation of this script is the ability to hide filetypes when choosing files for either 'source' or 'destination'. Could you help me build two checkboxes that would essentially only show which file types were selected?

1 - show .FCV
2 - show .SEQ

These are both the only file types that I would be dealing with, so in directories that are full of other file types it would be nice to simply hide them. With the checkbox option the script wouldn't lose its functionality if users wanted to use it for other files.

Also there is another issue whereby if users are selecting files from different directories, the script does not remember where the last directory used was. Well, put it like this, If I chose a different directory for source, the next time I chose destiation, it will default to where source was, which is undesirable. It would be nice if each field independently remembered which last directory it loaded from. Presently it's the same for both

Thanks!

Thanks again!

User avatar
mikeyww
Posts: 26435
Joined: 09 Sep 2014, 18:38

Re: File swapper

Post by mikeyww » 18 Oct 2021, 05:06

Glad it works!

Code: Select all

Gui, Font, s10
Gui, Add, Button  ,                           , Source
Gui, Add, Button  ,                           , Target
Gui, Add, Edit    ,      ym   w600 vf1  gCheck
Gui, Add, Edit    ,           wp   vf2  gCheck
Gui, Add, Button  ,                           , Copy
Gui, Add, Button  , x+m                       , Undo
Gui, Add, CheckBox, x+30 yp+4      vfcv       , FCV
Gui, Add, CheckBox, x+m            vseq       , SEQ
Gosub, F3
F3::
Gosub, Check
Gui, Show,, Copy file (press F3 to show this dialog window again)
Return

Check:
Gui, Submit, NoHide
GuiControl, % FileExist(f1) && FileExist(f2) ? "Enable" : "Disable", Copy
GuiControl, % FileExist(f2 ".bak") ? "Enable" : "Disable", Undo
Return

Filter:
Gui, Submit, NoHide
mask =
For each, ext in ["fcv", "seq"]
 mask .= %ext% ? "; *." ext : ""
mask := SubStr(mask, 3)
Return

ButtonSource:
Gosub, Filter
FileSelectFile, f1, 1, %sourceDir%, Select source file, %mask%
If ErrorLevel
 Return
SplitPath, f1,, sourceDir
GuiControl,, f1, %f1%
GuiControl, Focus, Target
Return

ButtonTarget:
Gosub, Filter
FileSelectFile, f2, S17, %targetDir%, Select target file, %mask%
If ErrorLevel
 Return
SplitPath, f2,, targetDir
GuiControl,, f2, %f2%
Sleep, 100
GuiControl, Focus, Copy
Return

ButtonCopy:
Gui, Submit
FileRecycle, % bak := f2 ".bak"
FileMove, %f2%, %bak%
If ErrorLevel {
 MsgBox, 48, Error, An error occurred when backing up the file.`n`n%f2%
 Return
} Else FileCopy, %f1%, %f2%
If ErrorLevel
     MsgBox, 48, Failure, An error occurred when copying the file.`n`n%f1%
Else MsgBox, 64, Success, Done!
Return

ButtonUndo:
Gui, Submit
FileRecycle, %f2%
FileMove, %f2%.bak, %f2%
If ErrorLevel
     MsgBox, 48, Failure, An error occurred when restoring the file.`n`n%f2%
Else MsgBox, 64, Success, Done!
Return

GuiEscape:
Gui, Hide
Return

Bluerose555
Posts: 111
Joined: 20 Mar 2017, 03:02

Re: File swapper

Post by Bluerose555 » 18 Oct 2021, 14:49

This keeps getting better and better . I have tired to add another checkbox for a different purpose of running a second script. I was able to make a button that launches the script, but I would rather use another checkbox.



This makes the button (albeit not on the same row so i need to figure that out)
but also it is not running the script ("pl08-UDAS.ahk). Basically I want the script to run after the OK box is entered after pressing COPY.

I was able to make the script run automatically by adding:

Code: Select all

ButtonCopy:
Gui, Submit
FileRecycle, % bak := f2 ".bak"
FileMove, %f2%, %bak%
If ErrorLevel {
 MsgBox, 48, Error, An error occurred when backing up the file.`n`n%f2%
 Return
} Else FileCopy, %f1%, %f2%
If ErrorLevel
     MsgBox, 48, Failure, An error occurred when copying the file.`n`n%f1%
Else Run %A_AHKPath% "pl08-UDAS.ahk" ; auto repack
send {F3} ; brings GUI back up
but as stated above, I don't want a button, but want a checkbox instead.

THanks again. I have learned so much with this thread!

User avatar
mikeyww
Posts: 26435
Joined: 09 Sep 2014, 18:38

Re: File swapper

Post by mikeyww » 18 Oct 2021, 15:02

Code: Select all

Gui, Font, s10
Gui, Add, Button  ,                           , Source
Gui, Add, Button  ,                           , Target
Gui, Add, Edit    ,      ym   w600 vf1  gCheck
Gui, Add, Edit    ,           wp   vf2  gCheck
Gui, Add, Button  ,                           , Copy
Gui, Add, Button  , x+m                       , Undo
Gui, Add, CheckBox, x+30 yp+4      vfcv       , FCV
Gui, Add, CheckBox, x+m            vseq       , SEQ
Gui, Add, CheckBox, x+m            vrepack    , Auto-repack
Gosub, F3
F3::
Gosub, Check
Gui, Show,, Copy file (press F3 to show this dialog window again)
Return

Check:
Gui, Submit, NoHide
GuiControl, % FileExist(f1) && FileExist(f2) ? "Enable" : "Disable", Copy
GuiControl, % FileExist(f2 ".bak") ? "Enable" : "Disable", Undo
Return

Filter:
Gui, Submit, NoHide
mask =
For each, ext in ["fcv", "seq"]
 mask .= %ext% ? "; *." ext : ""
mask := SubStr(mask, 3)
Return

ButtonSource:
Gosub, Filter
FileSelectFile, f1, 1, %sourceDir%, Select source file, %mask%
If ErrorLevel
 Return
SplitPath, f1,, sourceDir
GuiControl,, f1, %f1%
GuiControl, Focus, Target
Return

ButtonTarget:
Gosub, Filter
FileSelectFile, f2, S17, %targetDir%, Select target file, %mask%
If ErrorLevel
 Return
SplitPath, f2,, targetDir
GuiControl,, f2, %f2%
Sleep, 100
GuiControl, Focus, Copy
Return

ButtonCopy:
Gui, Submit
FileRecycle, % bak := f2 ".bak"
FileMove, %f2%, %bak%
If ErrorLevel {
 MsgBox, 48, Error, An error occurred when backing up the file.`n`n%f2%
 Return
} Else FileCopy, %f1%, %f2%
If ErrorLevel
     MsgBox, 48, Failure, An error occurred when copying the file.`n`n%f1%
Else MsgBox, 64, Success, Success!
If repack
 Run, pl08-UDAS.ahk ; Auto repack
Gosub, F3
Return

ButtonUndo:
Gui, Submit
FileRecycle, %f2%
FileMove, %f2%.bak, %f2%
If ErrorLevel
     MsgBox, 48, Failure, An error occurred when restoring the file.`n`n%f2%
Else MsgBox, 64, Success, Done!
Return

GuiEscape:
Gui, Hide
Return

Bluerose555
Posts: 111
Joined: 20 Mar 2017, 03:02

Re: File swapper

Post by Bluerose555 » 18 Oct 2021, 15:26

oh I see now, it had to be placed in the ButtonCopy: section! That makes perfect sense..

Now I have ONE last question and I am not sure if I should start a new thread, or ask here because it has to do with the second script. To make sense of this let me briefly explain my workflow

The files I am edting (FCV and SEQ) are files that are repacked into another file which is essentially a container ( .UDAS)
Once this .UDAS file is repacked, it needs to go into a specific directory to work. While it is easy for me to set the path of that directory, it may vary for other users. So in order to make this work for everyone, I would like to get some help in learning about paths.

Basically put, the variation of the paths from user to user will be slight, as there is a certain part of the path that will always be the same. For example my destination path for all the UDAS files is:

D:\Gaming\Steam\steamapps\common\Resident Evil 4\BIO4\EM

for other it might look like:

C:\MyGames\Steam\steamapps\common\Resident Evil 4\BIO4\EM

To contextualize what this script does:

Code: Select all

FileDelete, D:\Gaming\Steam\steamapps\common\Resident Evil 4\BIO4\EM\pl08.UDAS ; this removes the currentl file. 

SetWorkingDir %A_ScriptDir% ; this makes is so that the script will run in a working directory

RunWait , %comspec% /c "RE4_UHD_UDAS_Tool.exe" -p < nul ; this is the tool that repacks the .UDAS file

FileCopy, pl08.UDAS, D:\Gaming\Steam\steamapps\common\Resident Evil 4\BIO4\EM\ ; copies new file

TrayTip , UDAS Tool, File Repacked, Seconds, 16
Sleep 1000
Return
So I am wondering if there is a way to make this work for all users despite their different paths? Would it be as simple as making something that requests the location of the parent directory? Say like: \Steam\steamapps\common\Resident Evil 4\BIO4\ ?

User avatar
mikeyww
Posts: 26435
Joined: 09 Sep 2014, 18:38

Re: File swapper

Post by mikeyww » 18 Oct 2021, 16:42

Yes, if the parent directory is always the one that you need. The script below demonstrates how to get a parent directory.

Code: Select all

dir = D:\Gaming\Steam\steamapps\common\Resident Evil 4\BIO4\EM
SplitPath, dir,, parent
MsgBox, 64, Directories, Dir:`n%dir%`n`nParent:`n%parent%

Bluerose555
Posts: 111
Joined: 20 Mar 2017, 03:02

Re: File swapper

Post by Bluerose555 » 18 Oct 2021, 19:50

Thanks for this. I am not sure how this works though. I mean, I see that it is spitting out a message about the parent, but I am not sure how to implement a feature with this.

Can you show me how to make it so that this script will always repack a file to:

D:\Gaming\Steam\steamapps\common\Resident Evil 4\BIO4\EM

even for users that would have a different path say something like:

C:\TotallyDifferentParent\Steam\steamapps\common\Resident Evil 4\BIO4\EM

Code: Select all

FileDelete, THIS PART OF PATH NEEDS TO BE DEFINED\Steam\steamapps\common\Resident Evil 4\BIO4\EM\pl08.UDAS

RunWait , %comspec% /c "RE4_UHD_UDAS_Tool.exe" -p < nul

FileCopy, pl08.UDAS, THIS PART OF PATH NEEDS TO BE DEFINED\Steam\steamapps\common\Resident Evil 4\BIO4\EM\pl08.UDAS

TrayTip , UDAS Tool, File Repacked, Seconds, 16
Sleep 2000
Return

User avatar
mikeyww
Posts: 26435
Joined: 09 Sep 2014, 18:38

Re: File swapper

Post by mikeyww » 18 Oct 2021, 20:27

I do not know how your packing program works. How would you describe the way that you determine the target directory?

Bluerose555
Posts: 111
Joined: 20 Mar 2017, 03:02

Re: File swapper

Post by Bluerose555 » 18 Oct 2021, 20:42

The program just repacks a bunch of other files into a container (the .UDAS file.

"How would you describe the way that you determine the target directory?"

That target directory for the repacked .UDAS file is always:

\Steam\steamapps\common\Resident Evil 4\BIO4\EM\

the issue is that people install the game in different directories on their machines, so I am looking for a way, say to, ask the user where their \BIO4\ directory is , then the script would simply follow up by placing the repacked .UDAS in \BIO4\EM\

User avatar
mikeyww
Posts: 26435
Joined: 09 Sep 2014, 18:38

Re: File swapper

Post by mikeyww » 18 Oct 2021, 21:17

Code: Select all

Loop {                                               ; Get the BIO4 directory
 FileSelectFolder, dir,, ALLOWEDIT := 2, Select the BIO4 folder.
 If ErrorLevel
  Return
} Until (dir ~= "\\Steam\\steamapps\\common\\Resident Evil 4\\BIO4\\?$")
If !FileExist(dir .= "\EM") {                        ; Create target directory if needed
 FileCreateDir, %dir%
 If ErrorLevel {
  MsgBox, 48, Error, An error occurred when creating the directory. Aborting.`n`n%dir%
  Return
 }
} Else SetWorkingDir, %A_ScriptDir%
RunWait, %ComSpec% /c RE4_UHD_UDAS_Tool.exe -p < NUL ; Pack
If !FileExist(fn := "pl08.UDAS") {                   ; Abort if file was not created
 MsgBox, 48, Error, File not found. Aborting.`n`n%fn%
 Return
} Else FileRecycle, %dir%\%fn%
FileMove, %fn%, %dir%
If ErrorLevel
     MsgBox, 48, Failure, An error occurred while moving the file.`n`n%fn%
Else MsgBox, 64, Success, Done!

Bluerose555
Posts: 111
Joined: 20 Mar 2017, 03:02

Re: File swapper

Post by Bluerose555 » 19 Oct 2021, 00:36

Again, amazing, but need help to fix a couple of things. The script should not 'move' the repacked .UDAS, but instead copy it. I ask this because sometimes I need to unpack it again, and its always good to have that working copy present.

Second thing I could like to know. Is it possible to have to only enter the directory for BIO4 once? I am not sure it would be handy to have to paste that in there each time I repack. I don't know how this could be accomplished though, as the script would need to store that data somewhere. Is it possible the script writes to iteself before closing or?

Perhaps there is a another way to get it to store the info in a .txt file when that directory is inputted for the first time, and then the next time it is launched it checks for that file, and if it is present it loads the directory data from there.

Cheers, and....

T H A N K Y O U !!!!

User avatar
mikeyww
Posts: 26435
Joined: 09 Sep 2014, 18:38

Re: File swapper

Post by mikeyww » 19 Oct 2021, 04:44

Code: Select all

ini := StrReplace(A_ScriptFullPath, ".ahk", ".ini")   ; Store the folder path here
IniRead, dir, %ini%, Main, dir                        ; Get the stored folder path
If !FileExist(dir) {                                  ; If path was not found,
 Loop {                                               ;  get the BIO4 directory
  FileSelectFolder, dir,, ALLOWEDIT := 2, Select the BIO4 folder.
  If ErrorLevel
   Return
 } Until (dir ~= "\\Steam\\steamapps\\common\\Resident Evil 4\\BIO4\\?$")
}
IniWrite, %dir%, %ini%, Main, dir                    ; Save the folder path for next run
If !FileExist(dir .= "\EM") {                        ; Create target directory if needed
 FileCreateDir, %dir%
 If ErrorLevel {
  MsgBox, 48, Error, An error occurred when creating the directory. Aborting.`n`n%dir%
  Return
 }
} Else SetWorkingDir, %A_ScriptDir%
RunWait, %ComSpec% /c RE4_UHD_UDAS_Tool.exe -p < NUL ; Pack
If !FileExist(fn := "pl08.UDAS") {                   ; Abort if file was not created
 MsgBox, 48, Error, File not found. Aborting.`n`n%fn%
 Return
} Else FileRecycle, %dir%\%fn%
FileCopy, %fn%, %dir%
If ErrorLevel
     MsgBox, 48, Failure, An error occurred while copying the file.`n`n%fn%
Else MsgBox, 64, Success, Done!

Bluerose555
Posts: 111
Joined: 20 Mar 2017, 03:02

Re: File swapper

Post by Bluerose555 » 19 Oct 2021, 11:18

This is so amazing! I wonder if you can help me with one last thing on this (I promise :D )
I'd like to expand this script so that it changes the destination folder depending on the input file name. So instead of just handling pl08.UDAS the script could handle a variety of .UDAS files, repacking all with the same tool, but being copied to varying directories based on their name structure. For example, it would desired that:

all files that start with plXX.UDAS go into \Steam\steamapps\common\Resident Evil 4\BIO4\Em (pl08.UDAS for example)
all files that start with emXX.UDAS go into \Steam\steamapps\common\Resident Evil 4\BIO4\Em (em1d.UDAS) for example)

all files that start with r1xx.UDAS go into \Steam\steamapps\common\Resident Evil 4\BIO4\St1 (r109.UDAS)
all files that start with r2xx.UDAS go into \Steam\steamapps\common\Resident Evil 4\BIO4\St2 (r209.UDAS)
all files that start with r3xx.UDAS go into \Steam\steamapps\common\Resident Evil 4\BIO4\St3 (r309.UDAS)
all files that start with r4xx.UDAS go into \Steam\steamapps\common\Resident Evil 4\BIO4\St4 (r409.UDAS)
all files that start with r5xx.UDAS go into \Steam\steamapps\common\Resident Evil 4\BIO4\St4 (r409.UDAS)

Of course while keeping the amazing last script you posted. I mean that is just gold !

User avatar
mikeyww
Posts: 26435
Joined: 09 Sep 2014, 18:38

Re: File swapper

Post by mikeyww » 19 Oct 2021, 11:45

At this point, we have veered far away from the original question, having added different directories, undo, filters, and saving options, while addressing one last question two or three or four times. This one's for you! :)
Last edited by mikeyww on 19 Oct 2021, 20:07, edited 1 time in total.

Post Reply

Return to “Ask for Help (v1)”