You are here

Call user defined (batch) scripts during startup and shutdown

11 posts / 0 new
Last post
schentuu
Offline
Last seen: 11 years 9 months ago
Joined: 2010-12-15 07:36
Call user defined (batch) scripts during startup and shutdown

Hello!

Would it be a big deal to call user-defined scripts during startup and closing portableapps.com?

I'm thinking about looking if a specified batch file (for example startup.bat or shutdown.bat) exists in the Data folder.
If it doesn't exist, skip this point.
If it exists, execute the batch file before going on as usual.

Thils will give the user a possibility to define whatever he wants to do during startup or shutdown of portableapps.com without bothering you with "i need this new feature!!".

I think batch will be the best solution because every windows can handle them without requirements. And if the user wishes to use another scriptlanguage (like python or even java) he will be able to write a call of his programm with the corect parameters (like the path to a portable python interpreter) into a batchfile.

This would also cover the request Global Environment Variable Possible? in this forum because Enviroment Variables could be set and unset with batch scripts, too.

solanus
solanus's picture
Offline
Last seen: 8 years 1 month ago
Joined: 2006-01-21 19:12
During startup is easy

You can set up batch files in PortableApps format by simply using the correct file format and appinfo.ini file.
Once it appears in the menu, you can set it to Start Automatically when the menu starts.

I made this half-pony, half-monkey monster to please you.

schentuu
Offline
Last seen: 11 years 9 months ago
Joined: 2010-12-15 07:36
yes, but whats about during shutdown?

You're right, but i can't define a script that is called during the shutdown of portableapps.com.

So when i -for example- connect network share to a free drive letter (with "net use", usefull if you use portableapps a lot within one big network) using my startup-script, then i also want to disconnect the drive when i close my portable apps.
When i define Global system Variables with my script a also want to remove them from the system before leaving.
And so on...

So i got the idea to define two batch-scripts in the portableapps.com/Data/ folder that were called if they exist.

A batchfile for startup wouldn't be nessesary if you want to use the way you described, but it would be much easier to handle when the startup.bat is located in the same directory then the shutdown.bat.
And because of they would be an interface for miscellaneous plugins, they should be located in the /Data/ folder both.

VIItweaker
Offline
Last seen: 8 years 1 month ago
Joined: 2011-02-20 22:32
Here's a solution :

It should be a simple thing, right Beee

Anyways, since I had so much trouble & this is primary to portability, here is the solution so everyone else will find it a (relatively) simple thing:

Note: For my examples I will assume the USB will be assigned the drive letter H:
These examples work regardless of the letter the USB is assigned though.
I will also be using the generic "environment variable" value of %USB%.

Here are Several Options (Pick your preference):

#####################################
#####################################
#####################################
#####################################
Option A: (Temporary suggested Environmental Variable Path):
To set a suggested path during the current user login (automatically discarded when user logs off)

Note: This doesn't work for all environmental variable usages. Options get better after this one.

Use this in a .cmd or .bat:

;;:::::::::::::::::::::::::::
echo @echo off> volume.bat
echo SET USB=%cd:~0,2%>> volume.bat
dir | find "Volume"> go.bat
call go
if exist volume.bat del volume.bat
if exist go.bat del go.bat
;;:::::::::::::::::::::::::::

This sets USB to the Drive this .cmd or .bat file is run from.

So if you run this from your USB drive which is H:/

%USB% will point to H:

It creates two files (1st volume.bat then go.bat) then deletes both files (volume.bat and go.bat). When the cmd window closes the value is still set
as if you opened cmd.exe & typed SET USB=H
so
USB still points to H:

This can be easily "watched in action" by setting echo on and pauses in between each action like so:

;;:::::::::::::::::::::::::::
echo @echo on> volume.bat
pause
echo SET USB=%cd:~0,2%>> volume.bat
pause
dir | find "Volume"> go.bat
pause
call go
pause
if exist volume.bat del volume.bat
pause
if exist go.bat del go.bat
pause
;;:::::::::::::::::::::::::::

When each of the two files are created you can "edit" them to read them.

You will see that it will detect the drive the file is run from and then it will set the value (in this case USB) to the drive it is run from (in this case H:).
#####################################
#####################################
#####################################
#####################################

Option 2:

Copy this code to be used in a .cmd or .bat file:

;;::::::::::::::::::::::::::
@Echo off
ECHO ; Add USB as variable:
ECHO ; SETX USB %cd:~0,2%
ECHO ; Remove USB as variable:
ECHO ; REG DELETE HKCU\ENVIRONMENT /V USB /F
;;::::::::::::::::::::::::::

The first and third lines:

;;::::::::::::::::::::::::::
ECHO ; Add USB as variable:

ECHO ; Remove USB as variable:
;;::::::::::::::::::::::::::

Are only comments explaining the codes below them.

To activate the code below the comment simply remove the "ECHO;" from in front of the code line (not the comment line) and save the file as a .cmd or .bat file.

So you will have two files one to add the code and the second to remove the code.

The second and fourth lines are the actual code lines.

Second line code without the "ECHO ;" :

SETX USB %cd:~0,2%

This sets the "environmental variable" %USB% to the value of the drive letter the code is ran from.
So if it is run from a .cmd or .bat file that is on a USB Flash Drive that is in drive H:\ then %USB% points to H: ).

This permanently sets the value in the registry until the value is removed from the registry.

Fourth line code without the "ECHO ;" :

REG DELETE HKCU\ENVIRONMENT /V USB /F

This deletes the value of the environmental variable %USB% from the registry.

This makes it as though the first command line never happened. You can create your second .cmd or .bat file to remove the value by deleting the "ECHO ;" from in front of the fourth line code only.

So you will have one .cmd or .bat file with the "ECHO ;" removed from in front of the
Second line code without the "ECHO ;" :

;;::::::::::::::::::::::::::
SETX USB %cd:~0,2%
;;::::::::::::::::::::::::::

You might name this Set_Env_Var.cmd or .bat.

and another .cmd or .bat file with the "ECHO ;" removed from in front of the

Fourth line code without the "ECHO ;" :

;;::::::::::::::::::::::::::
REG DELETE HKCU\ENVIRONMENT /V USB /F
;;::::::::::::::::::::::::::

You might name this Del_Env_Var.cmd or .bat.

Since this last (fourth) code line is a REG DELETE code, I'll explain it & let you research it if you want to verify its function.

REG DELETE HKCU\ENVIRONMENT /V USB

deletes the registry value of HKEY_CURRENT_USER > Environment > USB

the /F is a command line "switch"

the /f tells the command to force deletion without prompting the user.

To get further explanation of REG DELETE and its switches type REG DELETE /? into a command window.

If you navigate to the registry key this code deletes before you run the first .cmd or .bat you will see that the value USB does not exist. It only exists if you first create it by setting it as an environmental variable.

If for some weird reason the value of USB already exists (due to you or a program creating it), then you can simply change the USB to any other value you desire to use as the %environmental_variable% of said portable device.

#####################################
#####################################
#####################################
#####################################

Option C:

This is not really another option but a continuation of Option 2 for those who want to take it to the next level!

AutoHotkey is a scripting tool you can download for free (Google it).

With AutoHotkey you can set anything to load or unload upon activation or deactivation of a script (which can be made into a universal .exe).

AutoHotkey comes with a tool to convert the .ahk script to an .exe executable file that can be run on any Windows O.S. without AutoHotkey needing to be installed on that system.

Starting the Set_Env_Var.cmd or .bat can easily be achieved by starting the .exe.

There are several ways to program AutoHotkey to run the Del_Env_Var.cmd or .bat file upon exit or shutdown.

Option 1 (very simple):
You can set a hotkey to do this. That is perhaps the simplest method. The hotkey can both run the Del_Env_Var.cmd or .bat and then Exit itself using ExitApp.

(See AutoHotkey forums for simple instructions, there is limitless AutoHotkey help on Google).

Option B (a little more complex):

You can actually set a AutoHotkey .ahk script or .exe to perform a specific action or set of actions when the .ahk script or .exe is terminated for any reason.

A good example of this (& you will see what I mean by complex; especially if you are not used to using AutoHotkey; is the sample script called "Minimize Window to Tray Menu" (Google it or view it here:

#####################################
#####################################
#####################################
#####################################

So This way you can have all of your shortcuts, apps, and programs set to go using "environment variable".

Note: You can change the "environment variable" from USB to any alpha-numeric value of your choice. Use 1 or 2 or USB1 or Pen_Drive or your_name_drive etc. Then on shortcuts, etc. simply use %Environmental Variable of your choice% (with % signs) as the drive letter. Google Environment variables for more info.

[Duplicate in another thread removed, no need for it - mod Chris]

Chris Morgan
Chris Morgan's picture
Offline
Last seen: 7 years 5 months ago
Joined: 2007-04-15 21:08
!!?

I'm afraid it's quite clear that you don't know much about how batch scripts work - or know too well and delight in using an extremely messy way of doing it.

  1. Option A

    echo @echo off> volume.bat
    echo SET USB=%cd:~0,2%>> volume.bat
    dir | find "Volume"> go.bat
    call go
    if exist volume.bat del volume.bat
    if exist go.bat del go.bat

    ...!? This takes a lot of thought to work out what it's doing. (And I am utterly bamboozled as to why you'd do it this way - putting "set usb=X:" into volume.bat, lines which will trigger volume.bat with meaningless arguments in go.bat and then calling go.bat and then deleting them...) Just do this:

    @set usb=%cd:~0,2%
    

    Incidentally, %cd:~0,2% is suboptimal; if the working directory is a different drive it won't work. You should use %~d0 instead (%0 being the script path, and the ~d making it take just the drive letter part - X:).

  2. All you're saying here is to create a value in HKEY_CURRENT_USER\Environment...

Please try to just say what you want to say as briefly as reasonably possible, and if you have code, put it in a single contiguous block.

I am a Christian and a developer and moderator here.

“A soft answer turns away wrath, but a harsh word stirs up anger.” – Proverbs 15:1

VIItweaker
Offline
Last seen: 8 years 1 month ago
Joined: 2011-02-20 22:32
@ Chris

The methods work.

And, no I did not take time to clean up my post at the time I wrote it. If someone wants the solutions enough they will not have too much to decipher (compared to anyplace else on this topic).

If it would make you feel any better and you would like to clean up my posts, feel free (just make sure the end codes are working as well as the originals).

Item #1.
The way I posted it helps others be able to see what they are doing.

The scripts that are created can be viewed by placing pauses in between each line as I posted.

Your method works but nobody can see what it does or if it is working.

So, Yes, Alternatively:
set usb=%cd:~0,2%
will work.

Item #2.

I don't know what to say about %~d0 ? I'm lost there.

I tried it, it doesn't work.

Anyone can open a text file and type

set usb=%cd:~0,2%
start %usb%
pause

then change the .txt to .cmd or .bat

And see that it works because it opens the drive it is ran from... Any drive that the .cmd or .bat is run from.

But as far as the %~d0 or even %~d0% it does not do anything (on my computer).

Sorry if my post is difficult to follow. My intent is not to frustrate anyone.

My intent was to offer a variety of working options to these questions.

I believe my post was still far more concise than other solutions you might find on the web for a beginner, novice, or even experienced.

You seemed to understand how it worked and that it worked, I believe others will as well. At the very least, someone who wants this solution bad enough will be able to try the different methods and use the one that meets their needs.

For that, do I deserve such chastisement?

Item #3
As far as "%cd:~0,2% being suboptimal; if the working directory is a different drive it won't work."

As I stated above, it works if you run the .cmd or .bat from the drive you wish to set as the environmental variable.

So running a .cmd or .bat containing the following text:

set usb=%cd:~0,2%
start %usb%
pause

Will set that drive (no matter what drive it is) to the value %usb%.

Very simple to test:

Run such a .cmd or .bat file from your C: drive.

C:\ drive will open (due to the start %usb% in the script).

Place the file on a different drive (i.e. G:\ drive) close that drive and run it from there (i.e. via a shortcut).

That drive (i.e. G:\ drive) will open.

I'd say it is working quite well (on my computer).

Tested on Windows 7 Home Premium.

Obviously linux and Mac's work differently.

The purpose of these posts is that PortableApps users would like to have different environmental variable for their portable drives.

Thus plugging their Portable drives into different machines still allow all shortcuts & scripts on that USB to point to their files and locations on that USB no matter what drive letter the computer assigns the USB Drive.

These solutions work on Windows operating systems from USB drives they are deployed from.

The one that writes to the registry works universally. All shortcuts will work when give the environmental variable assigned.

The user can deploy these solutions as they see fit.

If they have 5 USB pendrives, they can set different environmental variables for each one, i.e. USB1, USB2, USB3, USB4, USB5. So all USB's can work on any (windows) machine at the same time.

[Use <pre>..</pre> for blocks of code, not <code>..<code> - mod Chris]

Chris Morgan
Chris Morgan's picture
Offline
Last seen: 7 years 5 months ago
Joined: 2007-04-15 21:08
You still don't understand

The way I posted it helps others be able to see what they are doing.

The scripts that are created can be viewed by placing pauses in between each line as I posted.

Your method works but nobody can see what it does or if it is working.

Sorry, but you're still clearly not understanding. SET USB=%cd:~0,2% is the only line which does anything, the rest is noise which makes it very difficult to understand. It's the only step that does anything - you don't want the rest.

As for %~d0, it does work, but only inside a batch file. It will not work directly from Command Prompt. I think you don't understand the concept of working directory properly; consider something like this from the console; echo_d0.bat has the contents @echo %~d0 and echo_cd.bat has the contents @echo %cd:~0,2%:

X:\>echo_d0.bat
X:

X:\>echo_cd.bat
X:

X:\>cd /d C:\Documents and Settings\chrism

C:\Documents and Settings\chrism>X:\echo_d0.bat
X:

C:\Documents and Settings\chrism>X:\echo_cd.bat
C:

The point with the last example is that %cd% is C:\Documents and Settings\chrism, whereas %0 would be X:\cd.bat and so %~d0 X:. Thus it's more stable.

I am a Christian and a developer and moderator here.

“A soft answer turns away wrath, but a harsh word stirs up anger.” – Proverbs 15:1

VIItweaker
Offline
Last seen: 8 years 1 month ago
Joined: 2011-02-20 22:32
I agree.

Yes, I agree the line SET USB=%cd:~0,2% is the only line in that section of code that sets the drive to the environmental variable %USB% (or whatever you decide to use in place of USB). As such it may be used exclusively. Thanks for the explanation.
As far as the %cd% being C:\Documents and Settings\%UserName% ... I am using Windows 7 which does not have Documents and Settings, so maybe on XP this is the case. I delete the XP links in my system because they get in the way of my manual backups & I do not use any legacy software that requires them.

%UserProfile% is the legitimate environment variable to be the user's profile.
%cd% is the environmental variable to the drive letter of any drive it is run from.

On my computer if I type start %cd% or dir %cd% it opens or lists the drive I ran it from. %cd% also points to the drive letter of a USB Pendrive, Portable device, CD or DVD drive, etc. (All of which do not have a ".\Documents and Settings\%UserName%" directory.

As far as the rest of the code (0,2):

0 sets %cd% to the drive letter only, so %cd:~0% = C (when run from C:\ drive (without the " : " ).

,2 sets the " : ". So, %cd:~0,2% = C: (when run from C:\ drive).

It does not matter where the .cmd or .bat file is run from on the drive, it will always point to the drive letter without any other directory.

I still do not get your %~d0 & I'm sure I'm not the only one. I'm sure you have been at this longer than I have, but you are not posting this in a simple format that can be plugged in or used unless someone already has the knowledge, so it is not useful except to someone who already understands it. As such it seems like you are trying to belittle those who do not yet understand it.

Most people do not come on forums like this looking for answers to questions they already know the answers to. Most people are at some stage of learning and seek answers to that which they do not yet know. As such, they simply want a code that is easy to use and consistently gets the job done. Some people even want to see or know what the code is doing.

The codes I posted are simple enough anybody can use them & they work consistently without any troubles. I post them so anybody, if they want, can pursue a reasonable degree of understanding about them.

Ph4n70m
Ph4n70m's picture
Offline
Last seen: 6 months 1 week ago
Joined: 2007-01-12 19:22
.

Create a .cmd or .bat file and add this:

echo %~dp0
echo %~d0
pause

You'll see that %~dp0 points to the directory where you script is in, and %~d0 points to the drive letter that it has.

Edit: It ONLY works INSIDE a script file, not in command window.

depp.jones
Offline
Last seen: 9 hours 32 min ago
DeveloperTranslator
Joined: 2010-06-05 17:19
I don't know if you

I don't know if you understood the point of Chris' post. The difference between %cd:~0,2% and %~d0 is that the latter is drive independent.
To get the specific drive letter of the USB-drive you have to call your script from a folder of that drive. If you call it from another drive you simply don't get the letter of your USB-drive but the letter of the drive you call it from. It mostly doesn't matter if you start if from a gui, but matters if you call it from a command window or a batch located on another drive.
In the specific case it should work as it typically would be called from the drive itself but it is not failsafe if that changes whereas %~d0 always produces the letter of the drive where the script is located.

In addition to Ph4n70m's post try a script containing

echo %~d0
echo %cd:~0,2%
pause

called (from a commandline) remotely from different drives (as Chris has described) to see the difference.

VIItweaker
Offline
Last seen: 8 years 1 month ago
Joined: 2011-02-20 22:32
Ok, thanks.

So, taking this code:

echo %~dp0
pause
echo %~d0
pause
echo %cd:~0,2%
pause

Past and save into EnvVar.cmd.

Placed in D:\ drive.

Called from command line via:

start D:\EnvVar.cmd

It renders

D:\

D:

C:

It only does this when called from a command line. When called from Run, started by double clicking, etc. it does not do this.

So the %~d is not re-interpreted by the command line that calls it, whereas the %cd: 0,2% is re-interpreted.

That can be useful information.

Thanks for making this clearer.

Log in or register to post comments