10

The idea is to right click any file and then select "backup" in a drop down that just copies the file and adds a date time to the end of its name, then possibly moves that copy to another drive.

I would like a method of running a batch script by right clicking on a file and either passing that in as an argument (without typing it in), or somehow knowing that file is the one that should be used in the script.

Mainly for windows XP, I need it to be native as many site computers I work on do not belong to us and I don't want to copy over more files than necessary or rely on anything more than the standard commands available (two thirds of the machines don't have powershell ect)

I know how you can change file options to open a cmd at a specific folder location(below), and I cold probably change this to work for 1 specific file type, but I don't want to add a key for each type of file on the machine.

[HKEY_CLASSES_ROOT\Folder\shell\1.bat\command] @="c:\windows\system32\cmd.exe \"%1\""

From another site one work around that nearly solves this for me

jvierra - "Windows has always had that ability without changing the registry.

Place a bat or VBS file on your desktop. Drag and drop a file on the bat icon. The bat will receive the file name of the dropped file as %1 and vbscript will receive it as WScript.Arguments(0).

Try it. It works quite well. The bat or script can do anything with the file from that point."

daniel
  • 471
  • 1
  • 4
  • 13
  • you can also put a shortcut in the `SendTo` folder, then you can right click a file, select "Send to", and choose backup. That would require no modification of the registry. – wimh Jul 28 '11 at 06:35

2 Answers2

19

you can add your bat to the "Send To" menu. See http://support.microsoft.com/kb/310270

In brief, just copy your .bat file into the user SendTo folder.

COPY MYBACKUP.BAT "%USERPROFILE%\SendTo"

the user invokes your bat selecting the option of the "Send To" menu item.

for more sophisticated parametrization, like changing the text displayed in the menu, or the icon... you may create a link to your .BAT and place the link in the SendTo folder instead of the .bat itself

COPY "My very special backup.lnk" "%USERPROFILE%\SendTo"

You may first run a quick test. Create a BAT file with this content, and copy it over Sendto folder.

@echo off
echo Current Directory is %cd%
echo Current batch run is %0 %~dpnx0
echo Subject is %1 %~dpnx1
pause  

Edit: following some of the commments, I have corrected the SendTo folder specification in the COPY command, by adding the required quotes; and I have appended an test example, and corrected the %~dpnx syntax

PA.
  • 28,486
  • 9
  • 71
  • 95
  • I don't see how you would pass in the file name from there. – daniel Jul 29 '11 at 05:27
  • 1
    the filename is passed by Explorer as the first parameter to the invoked .bat. Have you tried it? what problem have you had? – PA. Jul 29 '11 at 11:05
  • It's a tough thing to explain, and hard to google for as well which is why I am asking. The batch script needs to know which file has been clicked on. I understand about having a .bat in the sendto lets you right click and run it. but how does the file get in as the first parameter after that. – daniel Jul 30 '11 at 04:48
  • I guess a better way of explaining is I have a batch script that backs up c:\onething.txt, I now want to change it from being hard coded, to be able to backup anything, and I want to run the script without using a cmd or typing anything (clicking only, the example in the OP uses a drag and drop onto a file which is a little strange, if there was a way to do this with a send to it would feel more normal). – daniel Jul 30 '11 at 04:52
  • copy the bat into sendto folder, then right click on any file, then select Send To, and select the bat name. try it. – PA. Jul 30 '11 at 05:42
  • One tiny change I had to make was adding quote marks for the user profile path. COPY MYBACKUP.BAT "%USERPROFILE%\SendTo" – daniel Jul 30 '11 at 06:54
  • Note if you create a shortcut to your batch file via Windows explorer and put it in the SendTo folder by default it will have the "Start in" folder set to the folder which contains your batch file. Because of this, output from your batch file will be saved to the batch file folder rather than the folder where you right clicked. To fix this, just clear the "Start in" folder in your shortcut's properties. – User Nov 14 '12 at 21:34
  • 3
    On Windows 7, the Send To: directory is:C:\Users\YOUR_USER_ID\AppData\Roaming\Microsoft\Windows\SendTo, or %AppData%\Microsoft\Windows\SendTo – Joachim Werner Jan 10 '14 at 08:38
  • You can also get to your sendto directory by typing `shell:sendto` in the explorer address bar. – Joan Charmant May 26 '17 at 19:17
2

@PA example (copied right below for easy viewing) is off by a hair.

@echo off
echo Current Directory is %cd%
echo Current batch run is %0 %dpnx0
echo Subject is %1 %dpnx1
pause  

I don't have enough reputation to respond to @PA. You forgot to include the ~ in the variable. This Q & A helped me a lot so I hope this helps someone else out. Thanks @daniel and @PA

Corrected daniel test example below

@echo off
echo Current Directory is %cd%
echo Current batch run is %0 %~dpnx0
echo Subject is %1 %~dpnx1
pause

If you want to just echo the name of the file without the path then you would use %~n1

example:

echo Subject is %~n1
julesverne
  • 392
  • 1
  • 8
  • 18