Page 1 of 1

My Documents changer

Posted: Tue Nov 29, 2011 2:39 am
by sphaaz
Well it might be stupid but it was very useful for temporary changing my doc local app data etc folders to current folder:)
It was used to make games portable and point the folders in wich games save ini files and save files etc, to the game folder :)
So all it does it drops 2 .reg files one that changes those paths and one that restores original paths :)
It is my first code so be gentle its far from good :)
Thx to developers for purebasic its such elegant and human friendly language, yet so powerful and gives possibilities to go
low level :)

Code: Select all

; StringtoHex procedure was found on forums so credits go to creator :)
; It was a bit modified as you can see :)
;--------------------------------------------------------------------------

;procedure to make string into hex with 00 in between and "," char between each hex value

Procedure.s StringToHex(str$)
  For x = 1 To Len(str$)
    hexchar$ = Hex(Asc(Mid(str$, x, 1)))
    If Len(hexchar$) = 1
      hexchar$ = "0" + hexchar$
    EndIf
    StringToHex$ + hexchar$ + "," + "00" + ","
  Next x
  ProcedureReturn StringToHex$
EndProcedure 

;procedure to make valid .reg files to move those folders to current dir

Procedure.s WriteToReg_change(str$, filename$, reg_key1$, reg_key2$, reg_key3$, reg_key4$)
  CreateFile(1,filename$)
  WriteStringN(1,"Windows Registry Editor Version 5.00 ",0)
  WriteStringN(1,reg_key1$ + str$,0)
  WriteStringN(1,reg_key2$ + str$,0)
  WriteStringN(1,reg_key3$ + str$,0)
  WriteString(1,reg_key4$ + str$,0)
  CloseFile(1)
EndProcedure

Procedure.s WriteToReg_restore(str1$, str2$, str3$, str4$, filename$, reg_key1$, reg_key2$, reg_key3$, reg_key4$)
  CreateFile(1,filename$)
  WriteStringN(1,"Windows Registry Editor Version 5.00 ",0)
  WriteStringN(1,reg_key1$ + str1$,0)
  WriteStringN(1,reg_key2$ + str2$,0)
  WriteStringN(1,reg_key3$ + str3$,0)
  WriteString(1,reg_key4$ + str4$,0)
  CloseFile(1)
EndProcedure


;getting current dir string...

current_dir$ = GetCurrentDirectory()

;adding padding zeroes to make valid registry format

hex_current_dir$ = StringToHex(current_dir$) + "00,00 "
;hex_restore$ = ""

;concentating strings for valid reg file (pain in the ASSS) ;)

reg_key1$ = "[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders]" + Chr(13) + Chr (10) + Chr(34) + "Personal" + Chr(34) + "=hex(2):"
reg_key2$ = "[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders]" + Chr(13) + Chr(10) + Chr(34) + "AppData" + Chr(34) + "=hex(2):" 
reg_key3$ = "[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders]" + Chr(13) + Chr(10) + Chr(34) + "Local" + " AppData" + Chr(34) + "=hex(2):"
reg_key4$ = "[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders]" + Chr(13) + Chr(10) + Chr(34) + "Common" + " AppData" + Chr(34) + "=hex(2):"
str1$ = "25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,4c,00,45,00,25,00,5c,00,4d,00,79,00,20,00,44,00,6f,00,63,00,75,00,6d,00,65,00,6e,00,74,00,73,00,00,00"
str2$ = "25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,4c,00,45,00,25,00,5c,00,41,00,70,00,70,00,6c,00,69,00,63,00,61,00,74,00,69,00,6f,00,6e,00,20,00,44,00,61,00,74,00,61,00,00,00"
str3$ = "25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,4c,00,45,00,25,00,5c,00,4c,00,6f,00,63,00,61,00,6c,00,20,00,53,00,65,00,74,00,74,00,69,00,6e,00,67,00,73,00,5c,00,41,00,70,00,70,00,6c,00,69,00,63,00,61,00,74,00,69,00,6f,00,6e,00,20,00,44,00,61,00,74,00,61,00,00,00"
str4$ = "25,00,41,00,4c,00,4c,00,55,00,53,00,45,00,52,00,53,00,50,00,52,00,4f,00,46,00,49,00,4c,00,45,00,25,00,5c,00,41,00,70,00,70,00,6c,00,69,00,63,00,61,00,74,00,69,00,6f,00,6e,00,20,00,44,00,61,00,74,00,61,00,00,00"

;writing reg files

WriteToReg_change(hex_current_dir$,"mydoc_here.reg", reg_key1$, reg_key2$, reg_key3$, reg_key4$)
WriteToReg_restore(str1$, str2$, str3$, str4$ ,"mydoc_restore.reg",reg_key1$,reg_key2$,reg_key3$,reg_key4$)

;output msgbox with string and hex values for debug

;MessageRequester(current_dir$, hex_current_dir$, #PB_MessageRequester_Ok)

Re: My Documents changer

Posted: Tue Nov 29, 2011 2:59 am
by IdeasVacuum
it might be stupid
I think it is -there is no need to do it, and you may well mess-up the functionality of other apps!

If you have a portable app, you will want the settings, saved files etc to travel with it, i.e. stored on the removable drive, not the PC.

Re: My Documents changer

Posted: Tue Nov 29, 2011 3:21 am
by sphaaz
Thx for reply :) well thing is its only used as part of bat file it changes and starts game and after game is over it restores...

Code: Select all

regedit /s mydoc_here.reg
game.exe
regedit /s mydoc_restore.reg
i tried various other methods but all newer games are supidly bound to mydoc/my games :(
anyway i posted it here so somebody might use it, and ofc if i ever loose the code haha wouldnt be first time i did that :)
Im sorry for my crude coding and lack of elegance :)

Re: My Documents changer

Posted: Tue Nov 29, 2011 3:47 am
by IdeasVacuum
...I see, it's not for a game that you have written. However, if the game crashes (as they do), the registry may not be restored, but if it's just you on your PC, you know what is going on and can sort it out no problem.

You could write an app to copy a folder from the portable drive into my docs/my games, run the game, cut the folder from my docs/my games back onto the portable drive. That avoids affecting anything else on the PC.

Re: My Documents changer

Posted: Tue Nov 29, 2011 4:06 am
by sphaaz
Well i did that with first verison of that, that was doable using script :)
However in my particular case im experimenting with windows loaded from ramdisk
so c: is around 500mb so i got 120mb free space :)
but its possible to copy just using script and cleaning after itself :)

Re: My Documents changer

Posted: Tue Nov 29, 2011 4:10 am
by MachineCode
sphaaz wrote:all it does it drops 2 .reg files one that changes those paths and one that restores original paths
Totally wrong approach and yes, it is totally stupid. You simply do NOT mess with system folders like that while your app is running! Other apps (and the system itself) relies on that folder being present; you can't just go changing it at will to suit your app.

So, let me tell you what to do instead: launch your exe, which then moves the game's data files from the game's folder to the "LocalAppAata" folder, then launches the game (so it loads its data files from "LocalAppData"); and when the game quits, your app moves the data files back from "LocalAppData" into the game's data folder. That way, the game is run in a "portable" way and you're not messing with the system and changing the Registry.

[Edit] Just realized I was repeating what IdeasVacuuam said. Sorry IV! Didn't see your post because I hadn't scrolled down before replying. :oops:

Re: My Documents changer

Posted: Tue Nov 29, 2011 4:17 am
by sphaaz
I see your point and i agree, as said above copy and cleanup method can be done using script and 5 lines of code...
However point was to trick system into beliving its folders were somewhere else and to make valid .reg files with hex entry...
that was the workout :) in other case lets say theorethically that some games put addons in mydoc folder and it can grow upto 3gb+ i suppose you would wait some time before it gets copied.... :) but that could be done checking last modified date on file and copy if it is newer than destination... anyway maybe someone can use the code and if i lose it i hope nobody will delete it from here :)

Re: My Documents changer

Posted: Tue Nov 29, 2011 4:20 am
by MachineCode
sphaaz wrote:[files] can grow upto 3gb+ i suppose you would wait some time before it gets copied
I said "move" the files, not "copy" them. A move is pretty much instant (if it's on the same drive), even for 3 GB of files.

Re: My Documents changer

Posted: Tue Nov 29, 2011 11:59 am
by Zach
MachineCode wrote:
sphaaz wrote:[files] can grow upto 3gb+ i suppose you would wait some time before it gets copied
I said "move" the files, not "copy" them. A move is pretty much instant (if it's on the same drive), even for 3 GB of files.
Kind of defeats the idea of a game being portable and stored on another drive though... Which was the whole point

Re: My Documents changer

Posted: Tue Nov 29, 2011 12:11 pm
by MachineCode
Zach wrote:Kind of defeats the idea of a game being portable and stored on another drive though... Which was the whole point
Portable means "not tied to the OS", and therefore doesn't always mean being on another drive. My AppData is on my D drive, as are my apps. So, I can do a clean install of Windows and not lose any app settings or software installations, because everything (except the OS) is portable.

Re: My Documents changer

Posted: Tue Nov 29, 2011 12:24 pm
by Kuron
MachineCode wrote: Portable means "not tied to the OS",
This is a unique definition :mrgreen:

Re: My Documents changer

Posted: Tue Nov 29, 2011 12:40 pm
by MachineCode
Kuron wrote:This is a unique definition :mrgreen:
Wikipedia agrees with me: "A portable application (portable app), sometimes also called standalone, is a computer software program designed to run independently from an operating system." (emphasis added). So, the bold text there is the same thing as "not tied to the OS".

Re: My Documents changer

Posted: Tue Nov 29, 2011 1:03 pm
by Kuron
MachineCode wrote:
Kuron wrote:This is a unique definition :mrgreen:
Wikipedia agrees with me: "A portable application (portable app), sometimes also called standalone, is a computer software program designed to run independently from an operating system." (emphasis added). So, the bold text there is the same thing as "not tied to the OS".
You forgot to quote the rest of the paragraph: A portable application (portable app), sometimes also called standalone, is a computer software program designed to run independently from an operating system. This type of application is stored on a removable storage device such as a CD, USB flash drive, flash card, or floppy disk – storing its program files, configuration information and data on the storage medium alone.

You are actually tying your software to the OS by using My AppData which is a major no-no for portable software and will generally disqualify your software from most portable outlets, as it is not portable because the storage location must be set up for every PC it is used on. Some users may not even have write access to My AppData.

Wikipedia provides the generally accepted qualifications for portable software:

To be considered portable, for purpose of this list, a software program must:
1. Not require any kind of formal installation onto a computer's permanent storage device to be executed, and can be stored on a removable storage device such as USB flash drive, enabling it to be used on multiple computers.
2. Settings are stored with, and can be carried around with, the software (i.e., they are written to the USB drive). If the registry is used to store settings, the application's configuration isn't portable, and must be set up on every PC it is used on
3. Leaves a zero (or near-zero) "footprint" on any PC it is run on after being used. i.e., All temporary files/registry settings should be removed once the program has exited, and files created by the user can be saved directly to the same removable media as the application is stored on.


Portable software is supposed to be independent of a particular PC and should be able to be moved freely from PC to PC just by copying the directory (and any subdirectories) belonging to the program. It is supposed to be self-contained and not write to any directory (including system directories) other than its own.

Re: My Documents changer

Posted: Tue Nov 29, 2011 2:10 pm
by MachineCode
As mentioned in another of my posts, I'm having major seniors moments tonight. I'm confusing "portable" with "virtualization". :oops: Please shoot me now.