-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename.bat
108 lines (85 loc) · 1.95 KB
/
rename.bat
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
::
:: replace pattern in files with replacement
::
:: docs
:: https://ss64.com/nt/syntax-replace.html
::
@echo off
Setlocal enabledelayedexpansion
set prog_name=%~n0%~x0
set user_dir="%~dp0"
set /a verbose=0
Set files=
Set pattern=
Set replace=
SET /a test=0
SET /a verbose=0
if [%~1] == [] call :usage && goto exitMain
GOTO :ParseParams
:ParseParams
if [%1]==[/?] call :help && goto exitMain
if /i [%1]==[/h] call :help && goto exitMain
if /i [%1]==[/help] call :help && goto exitMain
IF /i "%~1"=="/f" (
SET "files=%~2"
SHIFT
goto reParseParams
)
IF /i "%~1"=="/p" (
SET "pattern=%~2"
SHIFT
goto reParseParams
)
IF /i "%~1"=="/r" (
SET "replace=%~2"
SHIFT
goto reParseParams
)
IF /i "%~1"=="/t" (
SET /a test=1
goto reParseParams
)
IF /i "%~1"=="/v" (
SET /a verbose=1
goto reParseParams
) ELSE (
echo Unknown option : "%~1"
)
:reParseParams
SHIFT
if [%1]==[] goto main
GOTO :ParseParams
:main
if %verbose% EQU 1 (
echo files: %files%
echo pattern: %pattern%
echo replace: %replace%
)
For %%a in (%files%) Do (
Set "file=%%~a"
set "newName=!file:%pattern%=%replace%!"
if %test% EQU 1 (
echo file=!file!
echo =^> !newName!
) else (
move "%%a" "!newName!"
)
)
:exitMain
endlocal
exit /b %errorlevel%
:usage
echo Usage: %prog_name% /f ^<files^> /p ^<pattern^> [/r ^<replace^>] [/t] [/v] [/h]
exit /B 0
:help
call :usage
echo.
echo Options:
echo /f File pattern to iterate.
echo /p Rattern to replace.
echo /r Replacement. Defaults to empty string.
echo.
echo /t Test mode. Just showing the potential renaming happening, not actually renaming.
echo /v Verbose mode
echo /h Print help
exit /B 0