Replace last _ when renaming files
I want to delete all 「name.jpg」 and rename 「name_.jpg」 to 「name.jpg」 in windows.
The file structure is like this.
>upperFolder
>p1
>學生_二反田●雄.jpg
>學生_二反田●雄_.jpg
>學生_半田●男.jpg
>學生_半田●男_.jpg
>學生_潘●.jpg
>學生_潘●_.jpg
>...
>...
>p7
>學生_石井節●.jpg
>學生_石井節●_.jpg
>學生_王裕●.jpg
>學生_王裕●_.jpg
>學生_●垣 勇.jpg
>學生_●垣 勇_.jpg
delete p1/學生_二反田●雄.jpg
rename p1/學生_二反田●雄_.jpg p1_學生-二反田●雄.jpg
The final result looks as below.
p1_ is to prevent repeat filename and - seperate student name.
>upperFolder
>p1_學生-二反田●雄.jpg
>p1_學生-半田●男.jpg
>p1_學生-潘●.jpg
>...
>p7_學生-石井節●.jpg
>p7_學生-王裕●.jpg
>p7_學生-●垣 勇.jpg
How can I make this in bat or php?
I have tried
mkdir tmp
for /f "eol=: delims=" %%D in ('dir /b /s /ad *^|sort /r') do (
pushd "%%D"
for %%F in (*) do move "%%F" "..%%~nxD_%%F" >nul
popd
)
move *_.jpg tmp
del *.jpg
cd tmp
move *.jpg .. 2>NUL
but can't get rid of the last '_'
batch-file
add a comment |
I want to delete all 「name.jpg」 and rename 「name_.jpg」 to 「name.jpg」 in windows.
The file structure is like this.
>upperFolder
>p1
>學生_二反田●雄.jpg
>學生_二反田●雄_.jpg
>學生_半田●男.jpg
>學生_半田●男_.jpg
>學生_潘●.jpg
>學生_潘●_.jpg
>...
>...
>p7
>學生_石井節●.jpg
>學生_石井節●_.jpg
>學生_王裕●.jpg
>學生_王裕●_.jpg
>學生_●垣 勇.jpg
>學生_●垣 勇_.jpg
delete p1/學生_二反田●雄.jpg
rename p1/學生_二反田●雄_.jpg p1_學生-二反田●雄.jpg
The final result looks as below.
p1_ is to prevent repeat filename and - seperate student name.
>upperFolder
>p1_學生-二反田●雄.jpg
>p1_學生-半田●男.jpg
>p1_學生-潘●.jpg
>...
>p7_學生-石井節●.jpg
>p7_學生-王裕●.jpg
>p7_學生-●垣 勇.jpg
How can I make this in bat or php?
I have tried
mkdir tmp
for /f "eol=: delims=" %%D in ('dir /b /s /ad *^|sort /r') do (
pushd "%%D"
for %%F in (*) do move "%%F" "..%%~nxD_%%F" >nul
popd
)
move *_.jpg tmp
del *.jpg
cd tmp
move *.jpg .. 2>NUL
but can't get rid of the last '_'
batch-file
add a comment |
I want to delete all 「name.jpg」 and rename 「name_.jpg」 to 「name.jpg」 in windows.
The file structure is like this.
>upperFolder
>p1
>學生_二反田●雄.jpg
>學生_二反田●雄_.jpg
>學生_半田●男.jpg
>學生_半田●男_.jpg
>學生_潘●.jpg
>學生_潘●_.jpg
>...
>...
>p7
>學生_石井節●.jpg
>學生_石井節●_.jpg
>學生_王裕●.jpg
>學生_王裕●_.jpg
>學生_●垣 勇.jpg
>學生_●垣 勇_.jpg
delete p1/學生_二反田●雄.jpg
rename p1/學生_二反田●雄_.jpg p1_學生-二反田●雄.jpg
The final result looks as below.
p1_ is to prevent repeat filename and - seperate student name.
>upperFolder
>p1_學生-二反田●雄.jpg
>p1_學生-半田●男.jpg
>p1_學生-潘●.jpg
>...
>p7_學生-石井節●.jpg
>p7_學生-王裕●.jpg
>p7_學生-●垣 勇.jpg
How can I make this in bat or php?
I have tried
mkdir tmp
for /f "eol=: delims=" %%D in ('dir /b /s /ad *^|sort /r') do (
pushd "%%D"
for %%F in (*) do move "%%F" "..%%~nxD_%%F" >nul
popd
)
move *_.jpg tmp
del *.jpg
cd tmp
move *.jpg .. 2>NUL
but can't get rid of the last '_'
batch-file
I want to delete all 「name.jpg」 and rename 「name_.jpg」 to 「name.jpg」 in windows.
The file structure is like this.
>upperFolder
>p1
>學生_二反田●雄.jpg
>學生_二反田●雄_.jpg
>學生_半田●男.jpg
>學生_半田●男_.jpg
>學生_潘●.jpg
>學生_潘●_.jpg
>...
>...
>p7
>學生_石井節●.jpg
>學生_石井節●_.jpg
>學生_王裕●.jpg
>學生_王裕●_.jpg
>學生_●垣 勇.jpg
>學生_●垣 勇_.jpg
delete p1/學生_二反田●雄.jpg
rename p1/學生_二反田●雄_.jpg p1_學生-二反田●雄.jpg
The final result looks as below.
p1_ is to prevent repeat filename and - seperate student name.
>upperFolder
>p1_學生-二反田●雄.jpg
>p1_學生-半田●男.jpg
>p1_學生-潘●.jpg
>...
>p7_學生-石井節●.jpg
>p7_學生-王裕●.jpg
>p7_學生-●垣 勇.jpg
How can I make this in bat or php?
I have tried
mkdir tmp
for /f "eol=: delims=" %%D in ('dir /b /s /ad *^|sort /r') do (
pushd "%%D"
for %%F in (*) do move "%%F" "..%%~nxD_%%F" >nul
popd
)
move *_.jpg tmp
del *.jpg
cd tmp
move *.jpg .. 2>NUL
but can't get rid of the last '_'
batch-file
batch-file
edited Nov 21 at 3:46
asked Nov 21 at 3:25
AllenBooTung
9212
9212
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
There are no wildcards in CMD to not match a character, so instead you could temporally change the extension of the files whose names end with _:
Note: remember to run the following snippets from upperFolder and to change the %g for %%g if you're running from a batch file.
:: /r stands for recursive and ~n stands for (file)name
for /r %g in (*_.jpg) do ren %g "%~ng.jphg"
Then you can delete the files whose names do not end with _:
del /s *.jpg
And finally rename and move the files whose names end with _. But you're asking for something tricky and that is to prepend the filename with the directory name containing it, replace the inner _ of the filename with -, remove the trailing _ of the filename, and then move the file to upperFolder:
:: so that the variable expansion inside for's works as expected
setlocal EnableDelayedExpansion
:: loop through all the directories and store the names in g
for /d %g in (*) do (
for %h in ("%g*_.jphg") do (
set "newname=%~nh"
:: remove trailing _
set "newname=!newname:~0,-1!"
:: replace ALL _ with -, assuming the filename only has one inner _
set "newname=!newname:_=-!"
:: prepend directory name and restore extension
set "newname=%g_!newname!.jpg"
:: rename and move to current directory
move %h ".!newname!"
)
)
The source's file path contained space. I added double quote to source into first command: ren "%g" "%~ng.jphg" as well as last command move "%h" ".!newname!" .These scripts worked fine. I am grateful for your first register help. Appreciate.
– AllenBooTung
Nov 21 at 7:05
add a comment |
Something like this should work for you:
for %%a in (*.jpg) do del /y "%%~a" & ren "%%~na_%%~nx" "%%~nxa"
Here we:
- search all files of type
jpg
- delete those found
- and then rename files with same name ending with
_
into names of original files
See for /?
command or this for more information about %%~na
and similar.
Please note that unless your system OEM
codepage supports kanji this will not work. You can get around this problem by switching to UTF-8
encoding by issuing command CHCP 65001
and saving batch file as UTF-8 (no BOM)
. See this for more information.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404835%2freplace-last-when-renaming-files%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are no wildcards in CMD to not match a character, so instead you could temporally change the extension of the files whose names end with _:
Note: remember to run the following snippets from upperFolder and to change the %g for %%g if you're running from a batch file.
:: /r stands for recursive and ~n stands for (file)name
for /r %g in (*_.jpg) do ren %g "%~ng.jphg"
Then you can delete the files whose names do not end with _:
del /s *.jpg
And finally rename and move the files whose names end with _. But you're asking for something tricky and that is to prepend the filename with the directory name containing it, replace the inner _ of the filename with -, remove the trailing _ of the filename, and then move the file to upperFolder:
:: so that the variable expansion inside for's works as expected
setlocal EnableDelayedExpansion
:: loop through all the directories and store the names in g
for /d %g in (*) do (
for %h in ("%g*_.jphg") do (
set "newname=%~nh"
:: remove trailing _
set "newname=!newname:~0,-1!"
:: replace ALL _ with -, assuming the filename only has one inner _
set "newname=!newname:_=-!"
:: prepend directory name and restore extension
set "newname=%g_!newname!.jpg"
:: rename and move to current directory
move %h ".!newname!"
)
)
The source's file path contained space. I added double quote to source into first command: ren "%g" "%~ng.jphg" as well as last command move "%h" ".!newname!" .These scripts worked fine. I am grateful for your first register help. Appreciate.
– AllenBooTung
Nov 21 at 7:05
add a comment |
There are no wildcards in CMD to not match a character, so instead you could temporally change the extension of the files whose names end with _:
Note: remember to run the following snippets from upperFolder and to change the %g for %%g if you're running from a batch file.
:: /r stands for recursive and ~n stands for (file)name
for /r %g in (*_.jpg) do ren %g "%~ng.jphg"
Then you can delete the files whose names do not end with _:
del /s *.jpg
And finally rename and move the files whose names end with _. But you're asking for something tricky and that is to prepend the filename with the directory name containing it, replace the inner _ of the filename with -, remove the trailing _ of the filename, and then move the file to upperFolder:
:: so that the variable expansion inside for's works as expected
setlocal EnableDelayedExpansion
:: loop through all the directories and store the names in g
for /d %g in (*) do (
for %h in ("%g*_.jphg") do (
set "newname=%~nh"
:: remove trailing _
set "newname=!newname:~0,-1!"
:: replace ALL _ with -, assuming the filename only has one inner _
set "newname=!newname:_=-!"
:: prepend directory name and restore extension
set "newname=%g_!newname!.jpg"
:: rename and move to current directory
move %h ".!newname!"
)
)
The source's file path contained space. I added double quote to source into first command: ren "%g" "%~ng.jphg" as well as last command move "%h" ".!newname!" .These scripts worked fine. I am grateful for your first register help. Appreciate.
– AllenBooTung
Nov 21 at 7:05
add a comment |
There are no wildcards in CMD to not match a character, so instead you could temporally change the extension of the files whose names end with _:
Note: remember to run the following snippets from upperFolder and to change the %g for %%g if you're running from a batch file.
:: /r stands for recursive and ~n stands for (file)name
for /r %g in (*_.jpg) do ren %g "%~ng.jphg"
Then you can delete the files whose names do not end with _:
del /s *.jpg
And finally rename and move the files whose names end with _. But you're asking for something tricky and that is to prepend the filename with the directory name containing it, replace the inner _ of the filename with -, remove the trailing _ of the filename, and then move the file to upperFolder:
:: so that the variable expansion inside for's works as expected
setlocal EnableDelayedExpansion
:: loop through all the directories and store the names in g
for /d %g in (*) do (
for %h in ("%g*_.jphg") do (
set "newname=%~nh"
:: remove trailing _
set "newname=!newname:~0,-1!"
:: replace ALL _ with -, assuming the filename only has one inner _
set "newname=!newname:_=-!"
:: prepend directory name and restore extension
set "newname=%g_!newname!.jpg"
:: rename and move to current directory
move %h ".!newname!"
)
)
There are no wildcards in CMD to not match a character, so instead you could temporally change the extension of the files whose names end with _:
Note: remember to run the following snippets from upperFolder and to change the %g for %%g if you're running from a batch file.
:: /r stands for recursive and ~n stands for (file)name
for /r %g in (*_.jpg) do ren %g "%~ng.jphg"
Then you can delete the files whose names do not end with _:
del /s *.jpg
And finally rename and move the files whose names end with _. But you're asking for something tricky and that is to prepend the filename with the directory name containing it, replace the inner _ of the filename with -, remove the trailing _ of the filename, and then move the file to upperFolder:
:: so that the variable expansion inside for's works as expected
setlocal EnableDelayedExpansion
:: loop through all the directories and store the names in g
for /d %g in (*) do (
for %h in ("%g*_.jphg") do (
set "newname=%~nh"
:: remove trailing _
set "newname=!newname:~0,-1!"
:: replace ALL _ with -, assuming the filename only has one inner _
set "newname=!newname:_=-!"
:: prepend directory name and restore extension
set "newname=%g_!newname!.jpg"
:: rename and move to current directory
move %h ".!newname!"
)
)
answered Nov 21 at 5:37
Juan López
794
794
The source's file path contained space. I added double quote to source into first command: ren "%g" "%~ng.jphg" as well as last command move "%h" ".!newname!" .These scripts worked fine. I am grateful for your first register help. Appreciate.
– AllenBooTung
Nov 21 at 7:05
add a comment |
The source's file path contained space. I added double quote to source into first command: ren "%g" "%~ng.jphg" as well as last command move "%h" ".!newname!" .These scripts worked fine. I am grateful for your first register help. Appreciate.
– AllenBooTung
Nov 21 at 7:05
The source's file path contained space. I added double quote to source into first command: ren "%g" "%~ng.jphg" as well as last command move "%h" ".!newname!" .These scripts worked fine. I am grateful for your first register help. Appreciate.
– AllenBooTung
Nov 21 at 7:05
The source's file path contained space. I added double quote to source into first command: ren "%g" "%~ng.jphg" as well as last command move "%h" ".!newname!" .These scripts worked fine. I am grateful for your first register help. Appreciate.
– AllenBooTung
Nov 21 at 7:05
add a comment |
Something like this should work for you:
for %%a in (*.jpg) do del /y "%%~a" & ren "%%~na_%%~nx" "%%~nxa"
Here we:
- search all files of type
jpg
- delete those found
- and then rename files with same name ending with
_
into names of original files
See for /?
command or this for more information about %%~na
and similar.
Please note that unless your system OEM
codepage supports kanji this will not work. You can get around this problem by switching to UTF-8
encoding by issuing command CHCP 65001
and saving batch file as UTF-8 (no BOM)
. See this for more information.
add a comment |
Something like this should work for you:
for %%a in (*.jpg) do del /y "%%~a" & ren "%%~na_%%~nx" "%%~nxa"
Here we:
- search all files of type
jpg
- delete those found
- and then rename files with same name ending with
_
into names of original files
See for /?
command or this for more information about %%~na
and similar.
Please note that unless your system OEM
codepage supports kanji this will not work. You can get around this problem by switching to UTF-8
encoding by issuing command CHCP 65001
and saving batch file as UTF-8 (no BOM)
. See this for more information.
add a comment |
Something like this should work for you:
for %%a in (*.jpg) do del /y "%%~a" & ren "%%~na_%%~nx" "%%~nxa"
Here we:
- search all files of type
jpg
- delete those found
- and then rename files with same name ending with
_
into names of original files
See for /?
command or this for more information about %%~na
and similar.
Please note that unless your system OEM
codepage supports kanji this will not work. You can get around this problem by switching to UTF-8
encoding by issuing command CHCP 65001
and saving batch file as UTF-8 (no BOM)
. See this for more information.
Something like this should work for you:
for %%a in (*.jpg) do del /y "%%~a" & ren "%%~na_%%~nx" "%%~nxa"
Here we:
- search all files of type
jpg
- delete those found
- and then rename files with same name ending with
_
into names of original files
See for /?
command or this for more information about %%~na
and similar.
Please note that unless your system OEM
codepage supports kanji this will not work. You can get around this problem by switching to UTF-8
encoding by issuing command CHCP 65001
and saving batch file as UTF-8 (no BOM)
. See this for more information.
answered Nov 21 at 5:29
Jack White
33416
33416
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404835%2freplace-last-when-renaming-files%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown