China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-02 19:26
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to implement the function of calling a bat file through the right-click menu to move files View 2,893 Replies 43
Floor 16 Posted 2008-02-06 12:44 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Originally posted by pinghu at 2008-2-5 21:48:
It seems that everyone hasn't attached importance to my post yet. I think it's because everyone thinks the difficulty lies in the renaming. Moving files seems easy, but when you test it personally, you will find that when you select several files,...


@echo off
call :%* 2>nul || goto setup
goto :eof

:Sub1
rem Move to parent directory
set "str=%~nx1"

:loop
if not exist "..\%str%" goto Next
set "str=%~n1_New%~x1"
goto loop

:Next
move "%~nx1" "..\%str%"
goto :eof

:Sub2
rem Unpack
rem Complete by yourself
goto :eof

:setup
reg add "hkcr\*\shell\Move to Parent Directory\command" /f /t reg_sz /v "" /d "\"%~0\" Sub1 \"%%1\""
reg add "hkcr\Folder\shell\Unpack\command" /f /t reg_sz /v "" /d "\"%~0\" Sub2 \"%%1\""
goto :eof

The above is just a demo script. You can install it automatically by double-clicking.
Note that the setup part only adds the "Move to Parent Directory" function on files, and does not process folders. This can be added by yourself.
In addition, the above script does not handle the synchronization problem when multiple files are selected at the same time. It may cause the problem of overwriting with the same name. For this situation, you can appropriately use a temporary file to set an identifier, so that each instance can decide whether to copy according to the situation. For example, you can write the currently processed file name into a certain file, remember to use the overwrite mode, then delay for a period of time, then read the flag file, and the file name corresponding to the obtained one will perform the moving work. After moving, change the flag file, and those that do not match will continue to wait. Continue to query the flag file....
It is really not of much significance to complete it with pure bat. If possible, I suggest you try vbs, which will be much simpler.
In addition, here are a few links that may be useful:
SendTo + Batch Practical Small Tool -- Enhanced Version Send To
Find all bat files in the computer and copy them to the current directory?
How to add right-click menu in batch processing?
SendTo + (VBS Version)
Floor 17 Posted 2008-02-06 12:54 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
The above * I'm using AllFileSystemObject
Floor 18 Posted 2008-02-06 15:06 ·  中国 吉林 吉林市 联通
中级用户
★★
Credits 307
Posts 130
Joined 2008-02-01 21:29
18-year member
UID 109981
Gender Male
Status Offline
The problem has been initially solved, and I can breathe a sigh of relief!
Thanks to the support of 3742668, I learned the usage of sub1, reg add and setting str variables from it
Thank you very much!!
Floor 19 Posted 2008-02-06 18:10 ·  中国 吉林 吉林市 联通
中级用户
★★
Credits 307
Posts 130
Joined 2008-02-01 21:29
18-year member
UID 109981
Gender Male
Status Offline
There is a problem when running the script for 3742668. Suppose the file name I selected is 1.txt, and when moving up, a 1.txt file appears in the parent directory. I rebuild the 1.txt file, then move up again, and a 1_New.txt file appears in the parent directory. I create the 1.txt file again, then move up again, and it fails!
Floor 20 Posted 2008-02-06 18:24 ·  中国 吉林 吉林市 联通
中级用户
★★
Credits 307
Posts 130
Joined 2008-02-01 21:29
18-year member
UID 109981
Gender Male
Status Offline
To be precise, it entered an infinite loop
Floor 21 Posted 2008-02-06 18:42 ·  中国 吉林 吉林市 联通
中级用户
★★
Credits 307
Posts 130
Joined 2008-02-01 21:29
18-year member
UID 109981
Gender Male
Status Offline
I improved the code and solved the infinite loop
@echo off
call :%* 2>nul || goto setup
goto :eof

:Sub1
rem Move to parent directory
set "str=%~nx1"
set /a counter=1
:loop
if not exist "..\%str%" goto Next
set "str=%~n1_New%counter%%~x1"
set /a counter+=1
goto loop

:Next
set /a counter=
move "%~nx1" "..\%str%"
goto :eof

:Sub2
rem Unpack
rem Complete it yourself
goto :eof

:setup
reg add "hkcr\*\shell\Move to Parent Directory\command" /f /t reg_sz /v "" /d "\"%~0\" Sub1 \"%%1\""
reg add "hkcr\Folder\shell\Unpack\command" /f /t reg_sz /v "" /d "\"%~0\" Sub2 \"%%1\""
goto :eof
But what about when it comes to folders?
Floor 22 Posted 2008-02-06 21:15 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Adding a right - click also only allows single files to be used
Floor 23 Posted 2008-02-06 21:54 ·  中国 吉林 吉林市 联通
中级用户
★★
Credits 307
Posts 130
Joined 2008-02-01 21:29
18-year member
UID 109981
Gender Male
Status Offline
@echo off
call :%* 2>nul || goto setup
goto :eof

:Sub1
rem Move to parent directory
set "str=%~nx1"
set /a counter=1
:loop
if not exist "..\%str%" goto Next
set "str=%~n1_New%counter%%~x1"
set /a counter+=1
goto loop

:Next
set /a counter=
move "%~nx1" "..\%str%"
goto :eof

:Sub2
rem Unpack
setlocal enabledelayedexpansion
set /a counter=1
cd /d "%1"

for /f "delims=" %%i in (*.*) do (
if not exist "..\%%~nxi" goto Next
:loop
set "str=%%~ni_New!counter!%%~xi"
set /a counter+=1
if not exist "..\!str!" goto Next

goto loop

:Next
if "!counter!" equ "1" (move "%%i" "..\%%~nxi") else move "%%i" "..\!str!"
set /a counter=1)
set counter=
goto :eof

:setup
reg add "hkcr\*\shell\Move to Parent Directory\command" /f /t reg_sz /v "" /d "\"%~0\" Sub1 \"%%1\""
reg add "hkcr\directory\shell\Move to Parent Directory\command" /f /t reg_sz /v "" /d "\"%~0\" Sub1 \"%%1\""
reg add "hkcr\directory\shell\Unpack\command" /f /t reg_sz /v "" /d "\"%~0\" Sub2 \"%%1\""
goto :eof
Tested, the function of Sub1 can support all selected files and folders and ensure the reliability of renaming. It's just that Sub2 is still wrong. Hope everyone can help.
Really don't know where the mistake is, I'm really confused when writing this Sub2. Hope everyone can find the error
Floor 24 Posted 2008-02-07 02:16 ·  中国 吉林 吉林市 联通
中级用户
★★
Credits 307
Posts 130
Joined 2008-02-01 21:29
18-year member
UID 109981
Gender Male
Status Offline
Found that there are duplicate tags. After correcting, sub2 still cannot be used. Notify me and I don't know what way to open it. Depressed
Floor 25 Posted 2008-02-07 02:33 ·  中国 天津 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Why rewrite so much code when there's already sub1? It's still suggested to use VBS or other more rigorous scripts to implement.

[ Last edited by 3742668 on 2008-2-7 at 01:26 PM ]
Floor 26 Posted 2008-02-07 12:28 ·  中国 吉林 吉林市 联通
中级用户
★★
Credits 307
Posts 130
Joined 2008-02-01 21:29
18-year member
UID 109981
Gender Male
Status Offline
I can't do VBS, I only know batch processing. The script upstairs has been tested, and both functions don't work. What's the matter? Where exactly is the mistake in my Sub2?
Floor 27 Posted 2008-02-07 13:07 ·  中国 天津 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Sorry, because there is no test environment, there are many errors in the code on floor 25, including logical errors and grammatical errors.
Hehe, it's been too long since I wrote it, my hands are rusty. Try the following:

@echo off
call :%* 2>nul || goto setup
goto :eof

:Sub1
rem Move to the parent directory
if not defined str set "str=%~nx1"
if not exist "..\%~nx1" (move "%str%" "..\%~nx1" && set "str=" && goto :eof)
call :Sub1 "%~n1_New%~x1"
goto :eof

:Sub2
rem Unpack
pushd "%~1"
for %%i in (*.*) do call :Sub1 "%%~dpnxi"
popd
rd "%~1"
goto :eof


:setup
reg add "hkcr\*\shell\Move to Parent Directory\command" /f /t reg_sz /v "" /d "\"%~0\" Sub1 \"%%1\""
reg add "hkcr\Folder\shell\Unpack\command" /f /t reg_sz /v "" /d "\"%~0\" Sub2 \"%%1\""
goto :eof

It is recommended to select the operation targets in batches at one time for testing and see if there will be synchronization problems. In addition, you can add the function of writing logs after move.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
pinghu +3 2008-02-13 13:25
Floor 28 Posted 2008-02-07 17:13 ·  中国 吉林 吉林市 联通
中级用户
★★
Credits 307
Posts 130
Joined 2008-02-01 21:29
18-year member
UID 109981
Gender Male
Status Offline
After several trial attempts, it was found that the "Move to parent directory" function supports all files and folders. The "Unpack" function can only be applied to the files in the selected folder, and cannot be applied to the folders in the selected folder.
Floor 29 Posted 2008-02-07 18:32 ·  中国 吉林 吉林市 联通
中级用户
★★
Credits 307
Posts 130
Joined 2008-02-01 21:29
18-year member
UID 109981
Gender Male
Status Offline
The above code plus
reg add "hkcr\Folder\shell\Move to Parent Directory\command" /f /t reg_sz /v "" /d "\"%~0\" Sub2 \"%%1\""
can move the directory
Floor 30 Posted 2008-02-07 19:00 ·  中国 天津 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Originally posted by pinghu at 2008-2-7 17:13:
After several trials, it was found that the "Move to parent directory" function can support all files and folders. The "Unpack" function can only be applied to the files in the selected folder and cannot be applied to the folders in the selected folder.

I only take charge of giving you hints and frameworks. The specific start and end still need to be written by yourself according to your own ideas. For example, as mentioned above, it is suggested to add a log function and try operations with extremely large quantities.
In addition, if you want the unpack function to be applied to folders, you can try to solve it with dir /b by yourself.

It is suggested that you rewrite it from the beginning to the end according to your own ideas. Only in this way can you gain something. This is also the significance of the forum.
Forum Jump: