如何在一个文件夹及其子文件夹中的多个文件的文件名中更改某些字符(#,%和&,$)?
Ravi Thapliyal回答了如何使用PowerShell,但仅针对特定文件夹(Replace or delete certain characters from filenames of all files in a folder)中的文件。
我需要对所选文件夹中所有文件夹中的所有文件进行概括。
答案 0 :(得分:2)
你自己的答案只会改变一个字符,这里是一个带有一个数组RegEx的解决方案,从$ replace中删除你要保留的字符。
private void setFactoryForTextSwitcher(final TextSwitcher textSwitcher) {
textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
public TextView makeView() {
TextView textView = new TextView(context);
textView.setSingleLine(true);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.MATCH_PARENT));
return textView;
}
});
}
private void loadAnimationForTextSwitcher() {
final Animation slideInAnimation = AnimationUtils.loadAnimation(context, R.animator.slide_in);
final Animation slideOutAnimation = AnimationUtils.loadAnimation(context, R.animator.slide_out);
textSwitcher.setInAnimation(slideInAnimation);
textSwitcher.setOutAnimation(slideOutAnimation);
}
如果输出看起来不错,请删除[char[]]$replace = '!@#$%^&*(){}[]":;,<>/|\+=`~ '''
$regex = ($replace | % {[regex]::Escape($_)}) -join '|'
Get-ChildItem -recurse |
ForEach {
if ($_.Name -match $RegEx){
Ren $_.Fullname -NewName $($_.Name -replace $RegEx, '_') -whatif
}
}
修改从Get-ChildItem删除了-whatif
选项,因为它需要最新的Powershell版本,并且没有必要。
Edit2 我经常声明-File
接受管道输入,所以这是一个更直接的版本:
Rename-Item
答案 1 :(得分:0)
我从this answer找到了Dustin Malone:
Get-ChildItem -recurse -name | ForEach-Object { Move-Item $_ $_.replace(" ", "_") }
它给出了一个错误但是做了伎俩。
答案 2 :(得分:0)
该任务也可以通过Windows cmd
脚本(batch-file)完成 - 但是,除非采用适当的解决方法,否则存在一些限制。
以下脚本替换文件名中的#
,%
,&
,$
,(
和)
。您可以添加除^
,!
,=
和~
之外的其他字符,这些字符无法处理(if
查询可以跳过替换,以便避免语法错误)。如果您为字符提供的代码大于127
= 0x7F
,则可能需要充分切换代码页(请参阅chcp
命令)。请注意,脚本不会重命名任何文件,只会显示它们,,直到您删除大写的ECHO
命令:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=D:\path\to\root\dir" & rem /* (assign `%~1` here instead for the script to
rem accept the root dir. as command line argument) */
set _CHAR="#" "%%" "&" "$" "(" ")" & rem // (a `%` sign needs to be doubled!)
set "_REPL=_" & rem // (replacement character or string; this may also be empty)
if not defined _ROOT set "_ROOT=."
for /R "%_ROOT%" %%F in ("*.*") do (
set "FILE=%%~F"
set "NAME=%%~nxF"
setlocal EnableDelayedExpansion
for %%C in (!_CHAR!) do (
if not "%%~C"=="^" if not "%%~C"=="!" if not "%%~C"=="=" if not "%%~C"=="~" (
set "NAME=!NAME:%%~C=%_REPL%!"
)
)
ECHO ren "!FILE!" "!NAME!"
endlocal
)
endlocal
exit /B
要替换字符^
和!
,脚本需要稍微扩展一下:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=D:\path\to\root\dir" & rem /* (assign `%~1` here instead for the script to
rem accept the root dir. as command line argument) */
set _CHAR="#" "%%" "&" "$" "(" ")" "^" "!" & rem // (a `%` sign needs to be doubled!)
set "_REPL=_" & rem // (replacement character or string; this may also be empty)
if not defined _ROOT set "_ROOT=."
for /R "%_ROOT%" %%F in ("*.*") do (
set "FILE=%%~F"
set "NAME=%%~nxF"
setlocal EnableDelayedExpansion
for %%C in (!_CHAR!) do (
if "%%~C"=="^" (
set "NAME=!NAME:^=^^!"
call set "NAME=%%NAME:^=%_REPL%%%"
) else if "%%~C"=="!" (
call set "NAME=%%NAME:^!=%_REPL%%%"
) else if not "%%~C"=="=" if not "%%~C"=="~" (
set "NAME=!NAME:%%~C=%_REPL%!"
)
)
ECHO ren "!FILE!" "!NAME!"
endlocal
)
endlocal
exit /B
要替换字符=
和~
,脚本需要进一步扩展:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=D:\path\to\root\dir" & rem /* (assign `%~1` here instead for the script to
rem accept the root dir. as command line argument) */
set _CHAR="#" "%%" "&" "$" "(" ")" "^" "!" "=" "~" & rem // (a `%` sign needs to be doubled!)
set "_REPL=_" & rem // (replacement character or string; this may also be empty)
set "$LEN="
if not defined _ROOT set "_ROOT=."
for /R "%_ROOT%" %%F in ("*.*") do (
set "FILE=%%~F"
set "NAME=%%~nxF"
setlocal EnableDelayedExpansion
for %%C in (!_CHAR!) do (
if "%%~C"=="^" (
set "NAME=!NAME:^=^^!"
call set "NAME=%%NAME:^=%_REPL%%%"
) else if "%%~C"=="!" (
call set "NAME=%%NAME:^!=%_REPL%%%"
) else if "%%~C"=="~" (
call :FAST NAME NAME "%%~C" "%_REPL%"
) else if "%%~C"=="=" (
call :SLOW NAME NAME "%%~C" "%_REPL%"
) else (
set "NAME=!NAME:%%~C=%_REPL%!"
)
)
ECHO ren "!FILE!" "!NAME!"
endlocal
)
endlocal
exit /B
:FAST rtn_string ref_string val_char val_repl
rem // This works for `~` and `*`, but NOT for `=`!
setlocal DisableDelayedExpansion
set "STR="
set "CHR=%~3"
if not defined CHR goto :FQUIT
setlocal EnableDelayedExpansion
set "CHR=!CHR:~,1!"
set "STR=!%~2!"
:FLOOP
if defined STR (
set "END=!STR:*%~3=!"
if not "!END!"=="!STR!" (
for /F "eol=%CHR% delims=%CHR%" %%L in ("!STR!") do (
for /F "delims=" %%K in (^""!END!"^") do (
endlocal
set "STR=%%L%~4%%~K"
setlocal EnableDelayedExpansion
)
)
goto :FLOOP
)
)
:FQUIT
for /F "delims=" %%K in (^""!STR!"^") do (
endlocal
set "STR=%%~K"
)
endlocal & set "%~1=%STR%"
exit /B
:SLOW rtn_string ref_string val_char val_repl
rem // This works even for `=`.
setlocal DisableDelayedExpansion
set "STR="
set "CHR=%~3"
set "RPL=%~4"
if not defined CHR goto :SQUIT
if not defined $LEN call :LEN $LEN RPL
setlocal EnableDelayedExpansion
set "CHR=!CHR:~,1!"
set "STR=!%~2!"
set /A "IDX=0"
:SLOOP
set /A "NXT=IDX+1"
if defined STR (
set "POS=!STR:~%IDX%,1!"
if defined POS (
if "!POS!"=="!CHR!" (
set "STR=!STR:~,%IDX%!!RPL!!STR:~%NXT%!"
set /A "NXT=IDX+$LEN"
)
set /A "IDX=NXT"
goto :SLOOP
)
)
:SQUIT
for /F "delims=" %%K in (^""!STR!"^") do (
endlocal
set "STR=%%~K"
set "$LEN=%$LEN%"
)
endlocal & set "%~1=%STR%" & set "$LEN=%$LEN%"
exit /B
:LEN rtn_length ref_string
setlocal EnableDelayedExpansion
set "STR=!%~2!"
if not defined STR (set /A LEN=0) else (set /A LEN=1)
for %%L in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if defined STR (
set "INT=!STR:~%%L!"
if not "!INT!"=="" set /A LEN+=%%L & set "STR=!INT!"
)
)
endlocal & set "%~1=%LEN%"
exit /B