Context Menu Backup [REGEDIT AND BATCH]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Context Menu Backup [REGEDIT AND BATCH]

#1 Post by Boombox » 02 Dec 2012 06:27

.
My goal is to have a right-click context-menu-item called 'BACKUP'...
Every file and folder, when right clicked, will have access to this menu item.
And each item will be sent to the same location. D:\MUSTSAVE\

USING REGEDIT

Navigate to HKEY_CLASSES_ROOT\*\SHELL

Right click 'shell' and choose 'New' then 'Key'.

Rename this key to 'BACKUP'

Inside this key I create another key called 'command'.

With the 'command' key selected, edit the '(Default)' string by double clicking (right pane).

In the blank field I reference the name of my batch file and any parameters

I used,
c:\save.bat %1

To apply this to folders, navigate to HKEY_CLASSES_ROOT\DIRECTORY\SHELL and create the same keys.

Now, I know that the command needs to differ, depending on the backup item. (Item or folder).

So I added a parameter to the 'BACKUP' string in the 'Directory' location. I simply used the word 'folder'.

c:\save.bat %1 folder

My c:\Save.bat contained:

Code: Select all

if %2==folder goto robo

if not exist d:\MUSTSAVE md d:\MUSTSAVE
copy "%1" "d:\MUSTSAVE\%~nx1"
pause

:robo
if not exist d:\MUSTSAVE md d:\MUSTSAVE
robocopy %1 d:\MUSTSAVE\%~nx1
pause


Robocopy handles the folders fine, but if %2 is not equal to 'folder' the bat just exits.

Can someone help me please. Thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Context Menu Backup [REGEDIT AND BATCH]

#2 Post by foxidrive » 02 Dec 2012 06:43

Boombox wrote:Robocopy handles the folders fine, but if %2 is not equal to 'folder' the bat just exits.


Try this:

if "%~2"=="folder" goto robo


Also add a /b to the copy command.

and a goto :EOF after the copy command by the looks of it.

Your copy command will fail if %1 is quoted - I think you need to take another look at it. :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Context Menu Backup [REGEDIT AND BATCH]

#3 Post by foxidrive » 02 Dec 2012 06:55

if %2==folder goto robo

if not exist d:\MUSTSAVE md d:\MUSTSAVE
copy "%1" "d:\MUSTSAVE\%~nx1"
pause

:robo
if not exist d:\MUSTSAVE md d:\MUSTSAVE
robocopy %1 d:\MUSTSAVE\%~nx1
pause


Try this instead:

@echo off
if not exist "d:\MUSTSAVE\" md "d:\MUSTSAVE"
if "%~2"=="folder" (
robocopy "%~1" "d:\MUSTSAVE\%~nx1\"
) else (
copy /b /z "%~1" "d:\MUSTSAVE\"
)
pause

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Context Menu Backup [REGEDIT AND BATCH]

#4 Post by Boombox » 02 Dec 2012 07:49

.
@echo off
if not exist "d:\MUSTSAVE\" md "d:\MUSTSAVE"
if "%~2"=="folder" (
robocopy %1 d:\MUSTSAVE\%~nx1
) else (
copy /b /z "%~1" "d:\MUSTSAVE\"
)
pause

This worked for me with the above line void of quotes.

Thanks a bunch Foxi.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Context Menu Backup [REGEDIT AND BATCH]

#5 Post by foxidrive » 02 Dec 2012 09:08

Boombox wrote:.
This worked for me with the above line void of quotes.

Thanks a bunch Foxi.


That's ok. When all things are equal it will not work when there are spaces in the foldername though.

Post Reply