如果没有特定名称,是否可以制作一个重命名文件夹的批处理文件?
EG:
父目录
- - - >信息
- - - > randomfoldername
我有许多符合上述模式的文件夹。我想做的是制作一个批处理文件,在这个结构中重命名“randomfoldername”。父目录中始终有两个文件夹,一个始终为“info”,另一个为每个案例的更改。批处理文件中是否有一个方法可以用来始终重命名“randomfoldername”目录?我正在思考一些事情,
如果不是==“信息”那么...等。
这可能吗?
答案 0 :(得分:0)
您可以先检查文件夹是否存在,然后重命名文件夹
if not exist c:\info ( call random.bat )
random.bat:
dir /A:D /b | findstr.exe /n /R "." > s:\sites\file-count.txt
FOR /F "tokens=1-10 delims=:" %%A IN ('type "s:\sites\file-count.txt"') do
set NUMBER-OF-FILES=%%A
FOR /L %%A IN (1,1,%number-of-files%) DO CALL RENAME.bat %%A
rename.bat
:rename
FOR /F "tokens=1-10 delims=:" %%A IN ('type "s:\sites\file-count.txt" ^|
findstr "%1:"') do ren %%B %RANDOM%%%B
你也可以使用Bulk Rename Utility之类的免费工具
该应用程序有cli用于bat文件here
您也可以使用PowerShell脚本,如
$a = test-path c:\info
if ( $a -eq "True" ) { Write-Host nothing to do } Else { gic -directory path | %{$n="$pwd\$((get-random).tostring())$($_.name)";$_.moveto($N)} }
答案 1 :(得分:0)
下一个脚本从传入command line parameter的基本检查开始。
使用了 FOR /F
loop against the results of another command(dir
)。下一个解释被盗(see dbenham's original):
FOR /R
(以及简单FOR
)之前立即开始迭代 它已经完成扫描磁盘驱动器。循环是可能的 重申已经命名的文件!这将导致它被重命名 两次,给出了错误的结果。解决方案是使用FOR /F
命令'DIR /B'
,因为FOR /F
总是处理命令 在迭代之前完成。
@ECHO OFF >NUL
SETLOCAL enableextensions disabledelayedexpansion
set "ParentDirectory=%*"
if "%ParentDirectory%"=="" goto :usage1
if not exist "%ParentDirectory%\" goto :usage2
if not exist "%ParentDirectory%\info\" goto :usage3
set "SpecificName=Desired Name"
set /A "count=0"
for /F "delims=" %%G in ('dir /B /A:D "%ParentDirectory%\"') do set /A "count+=1"
if %count% neq 2 goto :usage4
for /F "delims=" %%G in ('dir /B /A:D "%ParentDirectory%\"') do (
if /I not "%%~G"=="info" (
if /I not "%%~G"=="%SpecificName%" (
echo renaming "%ParentDirectory%\%%~G" to "%SpecificName%"
rename "%ParentDirectory%\%%~G" "%SpecificName%"
) else (
echo renamed already: "%SpecificName%"
)
)
)
:endlocal
ENDLOCAL
goto :eof
:usage1
echo no paramater
goto :endlocal
:usage2
echo "%ParentDirectory%" folder does not exist
goto :endlocal
:usage3
echo "%ParentDirectory%\info" folder does not exist
goto :endlocal
:usage4
echo "%ParentDirectory%" folder contains %count% subfolders
goto :endlocal
<强>输出强>:
==>29376745.bat
no paramater
==>29376745.bat x y
"x y" folder does not exist
==>29376745.bat d:\test
"d:\test\info" folder does not exist
==>md "d:\test\info"
==>29376745.bat d:\test
"d:\test" folder contains 6 subfolders
==>29376745.bat d:\test\29376745
renaming "d:\test\29376745\currentName" to "Desired Name"
==>29376745.bat d:\test\29376745
renamed already: "Desired Name"
==>
必读(仅限索引):