rename files with certain characters with cmd
I found no other posts about this particular need.
Example:
Naruto NA - 480p.mp4
I want to erase the "NA"
so it will be
Naruto - 480p.mp4
I tried with
for /f "tokens=1*delims=NA" %%a in ('dir /b "*NA*.*"') do ren "%%aNA%%b" "%%a%%~xb"
but the output is
Naruto .mp4
not only "- 480p" is gone, but there's a space between Naruto and the .mp4
windows cmd rename
add a comment |
I found no other posts about this particular need.
Example:
Naruto NA - 480p.mp4
I want to erase the "NA"
so it will be
Naruto - 480p.mp4
I tried with
for /f "tokens=1*delims=NA" %%a in ('dir /b "*NA*.*"') do ren "%%aNA%%b" "%%a%%~xb"
but the output is
Naruto .mp4
not only "- 480p" is gone, but there's a space between Naruto and the .mp4
windows cmd rename
You could use string substitution for this, but 1st this is case insensitive and 2nd requres a normal string (and in a code block also delayed expansion) and 3rd you'll need context as otherwise theNafromNarutowould also be replaced.
– LotPings
Nov 23 '18 at 18:30
add a comment |
I found no other posts about this particular need.
Example:
Naruto NA - 480p.mp4
I want to erase the "NA"
so it will be
Naruto - 480p.mp4
I tried with
for /f "tokens=1*delims=NA" %%a in ('dir /b "*NA*.*"') do ren "%%aNA%%b" "%%a%%~xb"
but the output is
Naruto .mp4
not only "- 480p" is gone, but there's a space between Naruto and the .mp4
windows cmd rename
I found no other posts about this particular need.
Example:
Naruto NA - 480p.mp4
I want to erase the "NA"
so it will be
Naruto - 480p.mp4
I tried with
for /f "tokens=1*delims=NA" %%a in ('dir /b "*NA*.*"') do ren "%%aNA%%b" "%%a%%~xb"
but the output is
Naruto .mp4
not only "- 480p" is gone, but there's a space between Naruto and the .mp4
windows cmd rename
windows cmd rename
asked Nov 23 '18 at 17:19
MELEgrane DellabiMELEgrane Dellabi
14
14
You could use string substitution for this, but 1st this is case insensitive and 2nd requres a normal string (and in a code block also delayed expansion) and 3rd you'll need context as otherwise theNafromNarutowould also be replaced.
– LotPings
Nov 23 '18 at 18:30
add a comment |
You could use string substitution for this, but 1st this is case insensitive and 2nd requres a normal string (and in a code block also delayed expansion) and 3rd you'll need context as otherwise theNafromNarutowould also be replaced.
– LotPings
Nov 23 '18 at 18:30
You could use string substitution for this, but 1st this is case insensitive and 2nd requres a normal string (and in a code block also delayed expansion) and 3rd you'll need context as otherwise the
Na from Naruto would also be replaced.– LotPings
Nov 23 '18 at 18:30
You could use string substitution for this, but 1st this is case insensitive and 2nd requres a normal string (and in a code block also delayed expansion) and 3rd you'll need context as otherwise the
Na from Naruto would also be replaced.– LotPings
Nov 23 '18 at 18:30
add a comment |
1 Answer
1
active
oldest
votes
There are multiple solutions possible.
The first one works as long as no file name contains one or more !:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for %%I in ("* NA -*") do (
set "FileName=%%~nI"
ren "%%I" "!FileName:NA -=-!%%~xI"
)
endlocal
The file name of current file is assigned without file extension to environment variable FileName. Before execution of command REN a string substitution is done by Windows command processor to replace case-insensitive all occurrences of NA - by just - in file name to remove the not wanted part of the file name.
The problem is the line set "FileName=%%~nI" which results in file name referenced with %%~nI containing one or more exclamation marks that Windows command processor parses this command line once again after replacing %%~nI by file name of current file and interprets each ! as begin/end of an environment variable referenced delayed.
The second solution works also for file names with exclamation marks:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for %%I in ("* NA -*") do (
set "FileName=%%~nI"
setlocal EnableDelayedExpansion
ren "%%I" "!FileName:NA -=-!%%~xI"
endlocal
)
endlocal
The assignment of file name of current file to environment variable FileName is done while delayed environment variable expansion is disabled and so ! is interpreted as literal character. Then delayed expansion is enabled to be able to use the string substitution before renaming the file and restoring previous local environment.
One more solution for renaming Naruto NA - 480p.mp4 to Naruto - 480p.mp4 is:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "eol=| delims=" %%I in ('dir "* NA -*" /A-D-H /B 2^>nul') do for /F "eol=| tokens=1,2* delims= " %%A in ("%%~nxI") do ren "%%I" "%%A %%C"
endlocal
The outer FOR with option /F and a string in round brackets enclosed in straight single quotes runs in a separate command process started with cmd.exe /C in the background the command line:
dir "* NA -*" /A-D-H /B 2>nul
DIR searches in current directory for
- non-hidden files because of option
/A-D-H(attribute not directory and not hidden) - matching the wildcard pattern
* NA -*and - outputs in bare format line by line to handle STDOUT just the file names with file extension without path because of option
/B.
DIR could also output an error message to handle STDERR in case of no non-hidden file found matching the wildcard pattern. This error message is redirected with 2>nul to device NUL to suppress it.
Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line in a separate command process started in background.
FOR captures all lines output to handle STDOUT of started command process and processes those lines after started cmd.exe terminated.
Empty lines are ignored by FOR which do not occur here.
Lines starting with ; would be also ignored by FOR because of being default for option end of line. A file name can start with a semicolon. For that reason eol=| is used to define the vertical bar as end of line character which no file name can contain.
FOR would split up the line into substrings using normal space and horizontal tab as string delimiters and would assign just first space/tab delimited string to specified loop variable I. This behavior is not wanted here because of the file names contains multiple spaces. Therefore delims= is used to define an empty list of delimiters to turn off this string splitting behavior of command FOR.
The current file name assigned with the used options completely to specified loop variable I even on starting with ; or with one or more spaces is processed as string by an inner FOR.
eol=| is used again on the inner FOR to avoid that a file name starting with ; is not ignored. For the inner FOR splitting the file name string on spaces is wanted and so option delims= is used to explicitly define just space as delimiter. The file name string should be split up on all occurrences of one or more spaces into three parts for Naruto NA - 480p.mp4:
- First substring (token)
Narutoshould be assigned to specified loop variableA. - Second substring
NAshould be assigned to next loop variableBaccording to ASCII table. - And rest of the string after all spaces after second space separated substring should be assigned to next but one loop variable
Cwhich is for this example- 480p.mp4.
This can be achieved with option tokens=1,2*.
On command REN the second substring is omitted and so the new file name is Naruto - 480p.mp4.
Of course this third solution does not work for example for a file with name Naruto Uzumaki NA - 480p.mp4 because new name would be Naruto NA - 480p.mp4 because of second space delimited substring is in this case Uzumaki and not NA.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?echo /?endlocal /?for /?ren /?set /?setlocal /?
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%2f53450747%2frename-files-with-certain-characters-with-cmd%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are multiple solutions possible.
The first one works as long as no file name contains one or more !:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for %%I in ("* NA -*") do (
set "FileName=%%~nI"
ren "%%I" "!FileName:NA -=-!%%~xI"
)
endlocal
The file name of current file is assigned without file extension to environment variable FileName. Before execution of command REN a string substitution is done by Windows command processor to replace case-insensitive all occurrences of NA - by just - in file name to remove the not wanted part of the file name.
The problem is the line set "FileName=%%~nI" which results in file name referenced with %%~nI containing one or more exclamation marks that Windows command processor parses this command line once again after replacing %%~nI by file name of current file and interprets each ! as begin/end of an environment variable referenced delayed.
The second solution works also for file names with exclamation marks:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for %%I in ("* NA -*") do (
set "FileName=%%~nI"
setlocal EnableDelayedExpansion
ren "%%I" "!FileName:NA -=-!%%~xI"
endlocal
)
endlocal
The assignment of file name of current file to environment variable FileName is done while delayed environment variable expansion is disabled and so ! is interpreted as literal character. Then delayed expansion is enabled to be able to use the string substitution before renaming the file and restoring previous local environment.
One more solution for renaming Naruto NA - 480p.mp4 to Naruto - 480p.mp4 is:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "eol=| delims=" %%I in ('dir "* NA -*" /A-D-H /B 2^>nul') do for /F "eol=| tokens=1,2* delims= " %%A in ("%%~nxI") do ren "%%I" "%%A %%C"
endlocal
The outer FOR with option /F and a string in round brackets enclosed in straight single quotes runs in a separate command process started with cmd.exe /C in the background the command line:
dir "* NA -*" /A-D-H /B 2>nul
DIR searches in current directory for
- non-hidden files because of option
/A-D-H(attribute not directory and not hidden) - matching the wildcard pattern
* NA -*and - outputs in bare format line by line to handle STDOUT just the file names with file extension without path because of option
/B.
DIR could also output an error message to handle STDERR in case of no non-hidden file found matching the wildcard pattern. This error message is redirected with 2>nul to device NUL to suppress it.
Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line in a separate command process started in background.
FOR captures all lines output to handle STDOUT of started command process and processes those lines after started cmd.exe terminated.
Empty lines are ignored by FOR which do not occur here.
Lines starting with ; would be also ignored by FOR because of being default for option end of line. A file name can start with a semicolon. For that reason eol=| is used to define the vertical bar as end of line character which no file name can contain.
FOR would split up the line into substrings using normal space and horizontal tab as string delimiters and would assign just first space/tab delimited string to specified loop variable I. This behavior is not wanted here because of the file names contains multiple spaces. Therefore delims= is used to define an empty list of delimiters to turn off this string splitting behavior of command FOR.
The current file name assigned with the used options completely to specified loop variable I even on starting with ; or with one or more spaces is processed as string by an inner FOR.
eol=| is used again on the inner FOR to avoid that a file name starting with ; is not ignored. For the inner FOR splitting the file name string on spaces is wanted and so option delims= is used to explicitly define just space as delimiter. The file name string should be split up on all occurrences of one or more spaces into three parts for Naruto NA - 480p.mp4:
- First substring (token)
Narutoshould be assigned to specified loop variableA. - Second substring
NAshould be assigned to next loop variableBaccording to ASCII table. - And rest of the string after all spaces after second space separated substring should be assigned to next but one loop variable
Cwhich is for this example- 480p.mp4.
This can be achieved with option tokens=1,2*.
On command REN the second substring is omitted and so the new file name is Naruto - 480p.mp4.
Of course this third solution does not work for example for a file with name Naruto Uzumaki NA - 480p.mp4 because new name would be Naruto NA - 480p.mp4 because of second space delimited substring is in this case Uzumaki and not NA.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?echo /?endlocal /?for /?ren /?set /?setlocal /?
add a comment |
There are multiple solutions possible.
The first one works as long as no file name contains one or more !:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for %%I in ("* NA -*") do (
set "FileName=%%~nI"
ren "%%I" "!FileName:NA -=-!%%~xI"
)
endlocal
The file name of current file is assigned without file extension to environment variable FileName. Before execution of command REN a string substitution is done by Windows command processor to replace case-insensitive all occurrences of NA - by just - in file name to remove the not wanted part of the file name.
The problem is the line set "FileName=%%~nI" which results in file name referenced with %%~nI containing one or more exclamation marks that Windows command processor parses this command line once again after replacing %%~nI by file name of current file and interprets each ! as begin/end of an environment variable referenced delayed.
The second solution works also for file names with exclamation marks:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for %%I in ("* NA -*") do (
set "FileName=%%~nI"
setlocal EnableDelayedExpansion
ren "%%I" "!FileName:NA -=-!%%~xI"
endlocal
)
endlocal
The assignment of file name of current file to environment variable FileName is done while delayed environment variable expansion is disabled and so ! is interpreted as literal character. Then delayed expansion is enabled to be able to use the string substitution before renaming the file and restoring previous local environment.
One more solution for renaming Naruto NA - 480p.mp4 to Naruto - 480p.mp4 is:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "eol=| delims=" %%I in ('dir "* NA -*" /A-D-H /B 2^>nul') do for /F "eol=| tokens=1,2* delims= " %%A in ("%%~nxI") do ren "%%I" "%%A %%C"
endlocal
The outer FOR with option /F and a string in round brackets enclosed in straight single quotes runs in a separate command process started with cmd.exe /C in the background the command line:
dir "* NA -*" /A-D-H /B 2>nul
DIR searches in current directory for
- non-hidden files because of option
/A-D-H(attribute not directory and not hidden) - matching the wildcard pattern
* NA -*and - outputs in bare format line by line to handle STDOUT just the file names with file extension without path because of option
/B.
DIR could also output an error message to handle STDERR in case of no non-hidden file found matching the wildcard pattern. This error message is redirected with 2>nul to device NUL to suppress it.
Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line in a separate command process started in background.
FOR captures all lines output to handle STDOUT of started command process and processes those lines after started cmd.exe terminated.
Empty lines are ignored by FOR which do not occur here.
Lines starting with ; would be also ignored by FOR because of being default for option end of line. A file name can start with a semicolon. For that reason eol=| is used to define the vertical bar as end of line character which no file name can contain.
FOR would split up the line into substrings using normal space and horizontal tab as string delimiters and would assign just first space/tab delimited string to specified loop variable I. This behavior is not wanted here because of the file names contains multiple spaces. Therefore delims= is used to define an empty list of delimiters to turn off this string splitting behavior of command FOR.
The current file name assigned with the used options completely to specified loop variable I even on starting with ; or with one or more spaces is processed as string by an inner FOR.
eol=| is used again on the inner FOR to avoid that a file name starting with ; is not ignored. For the inner FOR splitting the file name string on spaces is wanted and so option delims= is used to explicitly define just space as delimiter. The file name string should be split up on all occurrences of one or more spaces into three parts for Naruto NA - 480p.mp4:
- First substring (token)
Narutoshould be assigned to specified loop variableA. - Second substring
NAshould be assigned to next loop variableBaccording to ASCII table. - And rest of the string after all spaces after second space separated substring should be assigned to next but one loop variable
Cwhich is for this example- 480p.mp4.
This can be achieved with option tokens=1,2*.
On command REN the second substring is omitted and so the new file name is Naruto - 480p.mp4.
Of course this third solution does not work for example for a file with name Naruto Uzumaki NA - 480p.mp4 because new name would be Naruto NA - 480p.mp4 because of second space delimited substring is in this case Uzumaki and not NA.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?echo /?endlocal /?for /?ren /?set /?setlocal /?
add a comment |
There are multiple solutions possible.
The first one works as long as no file name contains one or more !:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for %%I in ("* NA -*") do (
set "FileName=%%~nI"
ren "%%I" "!FileName:NA -=-!%%~xI"
)
endlocal
The file name of current file is assigned without file extension to environment variable FileName. Before execution of command REN a string substitution is done by Windows command processor to replace case-insensitive all occurrences of NA - by just - in file name to remove the not wanted part of the file name.
The problem is the line set "FileName=%%~nI" which results in file name referenced with %%~nI containing one or more exclamation marks that Windows command processor parses this command line once again after replacing %%~nI by file name of current file and interprets each ! as begin/end of an environment variable referenced delayed.
The second solution works also for file names with exclamation marks:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for %%I in ("* NA -*") do (
set "FileName=%%~nI"
setlocal EnableDelayedExpansion
ren "%%I" "!FileName:NA -=-!%%~xI"
endlocal
)
endlocal
The assignment of file name of current file to environment variable FileName is done while delayed environment variable expansion is disabled and so ! is interpreted as literal character. Then delayed expansion is enabled to be able to use the string substitution before renaming the file and restoring previous local environment.
One more solution for renaming Naruto NA - 480p.mp4 to Naruto - 480p.mp4 is:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "eol=| delims=" %%I in ('dir "* NA -*" /A-D-H /B 2^>nul') do for /F "eol=| tokens=1,2* delims= " %%A in ("%%~nxI") do ren "%%I" "%%A %%C"
endlocal
The outer FOR with option /F and a string in round brackets enclosed in straight single quotes runs in a separate command process started with cmd.exe /C in the background the command line:
dir "* NA -*" /A-D-H /B 2>nul
DIR searches in current directory for
- non-hidden files because of option
/A-D-H(attribute not directory and not hidden) - matching the wildcard pattern
* NA -*and - outputs in bare format line by line to handle STDOUT just the file names with file extension without path because of option
/B.
DIR could also output an error message to handle STDERR in case of no non-hidden file found matching the wildcard pattern. This error message is redirected with 2>nul to device NUL to suppress it.
Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line in a separate command process started in background.
FOR captures all lines output to handle STDOUT of started command process and processes those lines after started cmd.exe terminated.
Empty lines are ignored by FOR which do not occur here.
Lines starting with ; would be also ignored by FOR because of being default for option end of line. A file name can start with a semicolon. For that reason eol=| is used to define the vertical bar as end of line character which no file name can contain.
FOR would split up the line into substrings using normal space and horizontal tab as string delimiters and would assign just first space/tab delimited string to specified loop variable I. This behavior is not wanted here because of the file names contains multiple spaces. Therefore delims= is used to define an empty list of delimiters to turn off this string splitting behavior of command FOR.
The current file name assigned with the used options completely to specified loop variable I even on starting with ; or with one or more spaces is processed as string by an inner FOR.
eol=| is used again on the inner FOR to avoid that a file name starting with ; is not ignored. For the inner FOR splitting the file name string on spaces is wanted and so option delims= is used to explicitly define just space as delimiter. The file name string should be split up on all occurrences of one or more spaces into three parts for Naruto NA - 480p.mp4:
- First substring (token)
Narutoshould be assigned to specified loop variableA. - Second substring
NAshould be assigned to next loop variableBaccording to ASCII table. - And rest of the string after all spaces after second space separated substring should be assigned to next but one loop variable
Cwhich is for this example- 480p.mp4.
This can be achieved with option tokens=1,2*.
On command REN the second substring is omitted and so the new file name is Naruto - 480p.mp4.
Of course this third solution does not work for example for a file with name Naruto Uzumaki NA - 480p.mp4 because new name would be Naruto NA - 480p.mp4 because of second space delimited substring is in this case Uzumaki and not NA.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?echo /?endlocal /?for /?ren /?set /?setlocal /?
There are multiple solutions possible.
The first one works as long as no file name contains one or more !:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for %%I in ("* NA -*") do (
set "FileName=%%~nI"
ren "%%I" "!FileName:NA -=-!%%~xI"
)
endlocal
The file name of current file is assigned without file extension to environment variable FileName. Before execution of command REN a string substitution is done by Windows command processor to replace case-insensitive all occurrences of NA - by just - in file name to remove the not wanted part of the file name.
The problem is the line set "FileName=%%~nI" which results in file name referenced with %%~nI containing one or more exclamation marks that Windows command processor parses this command line once again after replacing %%~nI by file name of current file and interprets each ! as begin/end of an environment variable referenced delayed.
The second solution works also for file names with exclamation marks:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for %%I in ("* NA -*") do (
set "FileName=%%~nI"
setlocal EnableDelayedExpansion
ren "%%I" "!FileName:NA -=-!%%~xI"
endlocal
)
endlocal
The assignment of file name of current file to environment variable FileName is done while delayed environment variable expansion is disabled and so ! is interpreted as literal character. Then delayed expansion is enabled to be able to use the string substitution before renaming the file and restoring previous local environment.
One more solution for renaming Naruto NA - 480p.mp4 to Naruto - 480p.mp4 is:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "eol=| delims=" %%I in ('dir "* NA -*" /A-D-H /B 2^>nul') do for /F "eol=| tokens=1,2* delims= " %%A in ("%%~nxI") do ren "%%I" "%%A %%C"
endlocal
The outer FOR with option /F and a string in round brackets enclosed in straight single quotes runs in a separate command process started with cmd.exe /C in the background the command line:
dir "* NA -*" /A-D-H /B 2>nul
DIR searches in current directory for
- non-hidden files because of option
/A-D-H(attribute not directory and not hidden) - matching the wildcard pattern
* NA -*and - outputs in bare format line by line to handle STDOUT just the file names with file extension without path because of option
/B.
DIR could also output an error message to handle STDERR in case of no non-hidden file found matching the wildcard pattern. This error message is redirected with 2>nul to device NUL to suppress it.
Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line in a separate command process started in background.
FOR captures all lines output to handle STDOUT of started command process and processes those lines after started cmd.exe terminated.
Empty lines are ignored by FOR which do not occur here.
Lines starting with ; would be also ignored by FOR because of being default for option end of line. A file name can start with a semicolon. For that reason eol=| is used to define the vertical bar as end of line character which no file name can contain.
FOR would split up the line into substrings using normal space and horizontal tab as string delimiters and would assign just first space/tab delimited string to specified loop variable I. This behavior is not wanted here because of the file names contains multiple spaces. Therefore delims= is used to define an empty list of delimiters to turn off this string splitting behavior of command FOR.
The current file name assigned with the used options completely to specified loop variable I even on starting with ; or with one or more spaces is processed as string by an inner FOR.
eol=| is used again on the inner FOR to avoid that a file name starting with ; is not ignored. For the inner FOR splitting the file name string on spaces is wanted and so option delims= is used to explicitly define just space as delimiter. The file name string should be split up on all occurrences of one or more spaces into three parts for Naruto NA - 480p.mp4:
- First substring (token)
Narutoshould be assigned to specified loop variableA. - Second substring
NAshould be assigned to next loop variableBaccording to ASCII table. - And rest of the string after all spaces after second space separated substring should be assigned to next but one loop variable
Cwhich is for this example- 480p.mp4.
This can be achieved with option tokens=1,2*.
On command REN the second substring is omitted and so the new file name is Naruto - 480p.mp4.
Of course this third solution does not work for example for a file with name Naruto Uzumaki NA - 480p.mp4 because new name would be Naruto NA - 480p.mp4 because of second space delimited substring is in this case Uzumaki and not NA.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?echo /?endlocal /?for /?ren /?set /?setlocal /?
answered Nov 24 '18 at 15:55
MofiMofi
28k83777
28k83777
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.
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%2f53450747%2frename-files-with-certain-characters-with-cmd%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
You could use string substitution for this, but 1st this is case insensitive and 2nd requres a normal string (and in a code block also delayed expansion) and 3rd you'll need context as otherwise the
NafromNarutowould also be replaced.– LotPings
Nov 23 '18 at 18:30