-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmove.cmd
108 lines (86 loc) · 2.59 KB
/
vmove.cmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
@setlocal enabledelayedexpansion && echo off
REM // vmove
REM //
REM // Batch file/directory moving/renaming from editor.
REM //
REM // Usage: vmove [-n] [-f] [glob] ...
REM //
REM // This command uses the EDITOR environment variable to launch the user's
REM // preferred editor.
REM // Process arguments
if "%1" equ "--go" (
shift
) else (
start "VMove %*" /MIN cmd /C vmove --go %*
goto :eof
)
set noExecute=0
set forceClobber=0
set showHelp=0
set sourceGlobs=
:argLoop
if "%1" equ "" goto :labelArgsDone
set arg=%1
set arg=%arg:/=-%
if /I "%arg%" equ "-n" (
set noExecute=1
) else if /I "%arg%" equ "-f" (
set forceClobber=1
) else if /I "%arg%" equ "-?" (
set showHelp=1
) else if /I "%arg%" equ "-h" (
set showHelp=1
) else (
set sourceGlobs=%sourceGlobs% "%~1%"
)
shift
goto :argLoop
:labelArgsDone
call get-tempfile tempDir
mkdir %tempDir% || (
echo vmove: Unable to create temporary directory "%tempDir%". 1>&2
exit /b 1
)
if %showHelp% neq 0 (
echo.vmove: Batch file move / rename
echo.Usage: vmove [-n] [-f] [-?] [glob] ...
echo.
echo.This command creates an editing session on the named file globs. Rename ^(or
echo.move^) the files in the editor, and then save and exit. At that point, vmove will
echo.perform the moves from the old name to the new name.
echo.
echo.Options:
echo. -n No execute. If this option is specified, the command will display a
echo. list of the actions it would take, but will not actually perform them.
echo.
echo. -f Force clobber files. If you move a file to an existing file, then the
echo. moved file will clobber the existing one.
echo.
echo. -? Show help information
exit /b 0
)
REM // Create move file for editing
for /f "delims=" %%f in ('dir/b %sourceGlobs%') do (
echo>>%tempDir%\moves.txt "%%f" ^< from "%%f"
)
if not defined editor set EDITOR=notepad.exe
call "%EDITOR:"=%" %tempDir%\moves.txt
if %forceClobber% equ 0 (
set clobberOption=/-Y
) else (
set clobberOption=/Y
)
REM // Loop through the lines in the move file
for /f "delims=< tokens=1,2" %%f in (%tempDir%\moves.txt) do (
set dest=%%f
set src=%%g
set src=!src: from =!
set dest=!dest:~0,-1!
if !src! neq !dest! (
set moveCommand=move %clobberOption% !src! !dest!
echo !moveCommand!
if %noExecute% equ 0 (!moveCommand!)
)
)
rmdir /s /q %tempDir%
goto :eof