简体   繁体   中英

Using a shortcut or batch file to move file to a relative location

I'm trying to create a shortcutor batch file to sit in the windows'send to' menu which sends the file to a location relative to the current file path of the file.

So for example I'm looking to create an archive shortcut to stick in the ocntext menu that would move files as follows

C:\cheese\stilton.txt > C:\cheese\archive\stilton.txt

or

C:\biscuits\hobnobs.txt > C:\biscuits\archive\hobnobs.txt

But I don't understand how to capture the current file path and pass it to the batch\\shortcut.

Can anyone help?

Funnily enough, windows relative path does not differentiate between file & directory, when using .. .

Simply, move "%1" "%1"\\..\\archive also works. ;-) Though not a clean way. :-)
Also make sure that you do mkdir "%1"\\..\\archive before move.

Also, instead of send-to, you may think of adding it to registry; specific to a file type.
For text files, .reg file will look as below:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\txtfile\shell\movefile]
@="Move to archive"
[HKEY_CLASSES_ROOT\txtfile\shell\movefile\command]
@="cmd /c mkdir \"%1\"\\..\\archive & move \"%1\" \"%1\"\\..\\archive "

In a batch file you can get the path of the first parameter with %~dp1 , or just the path with %~p1 . This means that you would want to pass the full filename with path to the batch file.

To use this in a batch file, for example to move a file, you could do the following:

@echo off
setlocal enabledelayedexpansion
set fn=%~1
move "!!fn!!" "%~p1archive"

The %~1 removes quotes from the filename, and I just do that out of habit.

The %~p1 expands to the path of the file, with a slash appended.

Note that I haven't included here checking that the directory exists, which should be simple enough... add this line before the move to create it:

md "%~p1archive"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM