Skip to content

Commit bea3d27

Browse files
authored
feat(fs): Add find and replace option to batch rename (#250)
1 parent 9c01922 commit bea3d27

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

src/lang/en/home.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@
123123
"regex_rename_preview_old_name": "Old Name",
124124
"regex_rename_preview_new_name": "New Name",
125125
"regular_rename": "Regular expression file renaming. Input the source file name regular expression on the first line, and input the new file name regular expression on the second line.",
126-
"sequential_renaming_desc": "The new file name will have a numerical starting value appended to it, and it will be displayed sequentially by adding 1 to the starting value. Enter the new file name on the first line and the starting value on the second line."
126+
"sequential_renaming_desc": "The new file name will have a numerical starting value appended to it, and it will be displayed sequentially by adding 1 to the starting value. Enter the new file name on the first line and the starting value on the second line.",
127+
"find_replace": "Find and Replace",
128+
"find_replace_desc": "Find and Replace"
127129
},
128130
"upload": {
129131
"add_as_task": "Add as task",

src/pages/home/toolbar/BatchRename.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const BatchRename = () => {
6363
})
6464

6565
const submit = () => {
66-
if (!srcName() || !newName()) {
66+
if (!srcName()) {
6767
// Check if both input values are not empty
6868
notify.warning(t("global.empty_input"))
6969
return
@@ -132,7 +132,7 @@ export const BatchRename = () => {
132132
}
133133
return renameObj
134134
})
135-
} else {
135+
} else if (type() === "2") {
136136
let tempNum = newName()
137137
matchNames = selectedObjs().map((obj) => {
138138
let suffix = ""
@@ -150,6 +150,16 @@ export const BatchRename = () => {
150150
.padStart(tempNum.length, "0")
151151
return renameObj
152152
})
153+
} else {
154+
matchNames = selectedObjs()
155+
.filter((obj) => obj.name.indexOf(srcName()) !== -1)
156+
.map((obj) => {
157+
const renameObj: RenameObj = {
158+
src_name: obj.name,
159+
new_name: obj.name.replace(srcName(), newName()),
160+
}
161+
return renameObj
162+
})
153163
}
154164

155165
setMatchNames(matchNames)
@@ -178,7 +188,7 @@ export const BatchRename = () => {
178188
defaultValue="1"
179189
onChange={(event) => {
180190
setType(event)
181-
if (event === "1") {
191+
if (event === "1" || event === "3") {
182192
setNewNameType("string")
183193
} else if (event === "2") {
184194
setNewNameType("number")
@@ -188,6 +198,7 @@ export const BatchRename = () => {
188198
<HStack spacing="$4">
189199
<Radio value="1">{t("home.toolbar.regex_rename")}</Radio>
190200
<Radio value="2">{t("home.toolbar.sequential_renaming")}</Radio>
201+
<Radio value="3">{t("home.toolbar.find_replace")}</Radio>
191202
</HStack>
192203
</RadioGroup>
193204
<VStack spacing="$2">
@@ -198,6 +209,9 @@ export const BatchRename = () => {
198209
<Show when={type() === "2"}>
199210
{t("home.toolbar.sequential_renaming_desc")}
200211
</Show>
212+
<Show when={type() === "3"}>
213+
{t("home.toolbar.find_replace_desc")}
214+
</Show>
201215
</p>
202216
<Input
203217
id="modal-input1" // Update id to "modal-input1" for first input
@@ -240,7 +254,7 @@ export const BatchRename = () => {
240254
</Button>
241255
<Button
242256
onClick={() => submit()}
243-
disabled={!srcName() || !newName()}
257+
disabled={type() === "2" || type() === "3" ? !srcName() || !newName() : !srcName()}
244258
>
245259
{t("global.ok")}
246260
</Button>

0 commit comments

Comments
 (0)