批量重命名文件夹结构

时间:2013-09-24 16:28:22

标签: windows batch-file directory rename

为了使公司(我工作的地方)运作更有条理,更有效率,我们为新项目创建了一个批准的文件夹结构。

新项目实时项目驻留在文件服务器上,并且彼此处于同一级别。 (实时项目通过规划实现,新项目有时只能进行规划)

每次我们开始一个新项目时,项目文件夹结构都可以细致地创建,因此我创建了一个Windows批处理文件来创建它。你可以在这里找到它:http://pastebin.com/mnSu5vnk 这部分效果很好!

文件夹结构为:

F:\New Job\Project Name\Project Correspondence

F:\New Job\Project Name\Project Directory

F:\New Job\Project Name\Project Drawings

F:\New Job\Project Name\Project Images

F:\New Job\Project Name\Project Correspondence\Arboriculturalist

F:\New Job\Project Name\Project Correspondence\CGI

F:\New Job\Project Name\Project Correspondence\Client

F:\New Job\Project Name\Project Correspondence\Measured Survey

F:\New Job\Project Name\Project Correspondence\Planning

F:\New Job\Project Name\Project Correspondence\Post Planning Folders (Drop in as and when)

F:\New Job\Project Name\Project Correspondence\Client\Appointments

F:\New Job\Project Name\Project Correspondence\Client\Fees

F:\New Job\Project Name\Project Correspondence\Planning\LA Planning

F:\New Job\Project Name\Project Correspondence\Planning\Planning Consultants

F:\New Job\Project Name\Project Correspondence\Planning\LA Planning\Planning Application

F:\New Job\Project Name\Project Correspondence\Planning\LA Planning\Pre Application

F:\New Job\Project Name\Project Correspondence\Post Planning Folders (Drop in as and when)\Arboriculturalist

F:\New Job\Project Name\Project Correspondence\Post Planning Folders (Drop in as and when)\Contract Administrator

F:\New Job\Project Name\Project Correspondence\Post Planning Folders (Drop in as and when)\Contractor

F:\New Job\Project Name\Project Correspondence\Post Planning Folders (Drop in as and when)\LA Building Control

F:\New Job\Project Name\Project Correspondence\Post Planning Folders (Drop in as and when)\M&E Consultants

F:\New Job\Project Name\Project Correspondence\Post Planning Folders (Drop in as and when)\Structural Engineer

F:\New Job\Project Name\Project Correspondence\Post Planning Folders (Drop in as and when)\Tender, Specification

F:\New Job\Project Name\Project Drawings\CD Series (Construction Details)

F:\New Job\Project Name\Project Drawings\Drawing Register

F:\New Job\Project Name\Project Drawings\EW Series (External Works)

F:\New Job\Project Name\Project Drawings\Outgoing Drawings

F:\New Job\Project Name\Project Drawings\P Series (Planning)

F:\New Job\Project Name\Project Drawings\RL Drawings (Room Layouts)

F:\New Job\Project Name\Project Drawings\SK Series (Sketch Design)

F:\New Job\Project Name\Project Drawings\X Series (Existing)

F:\New Job\Project Name\Project Drawings\Outgoing Drawings\DWG FILES

F:\New Job\Project Name\Project Drawings\Outgoing Drawings\PDF FILES

问题是一些新项目永远不会成为“实时”状态;但是,那些确实被分配了项目编号,因此需要在每个子文件夹前加上项目编号。为每个子文件夹添加项目编号前缀的原因是“ people ”意外拖动/复制/移动到另一个项目中,这会让事情变得非常混乱!

另一个问题是,一些“新项目”已经在那里待了一段时间(1年+)并且没有文件夹结构,因此我需要制作一个批处理文件来检测“新项目”是否具有已批准的文件夹结构,并根据需要重命名。

这是我目前所拥有的,但我相信我的方法是错的。

@Echo off
:start
set /P ProjNo=Project Number:
set /P ProjAd=Project Address:
cls

Echo New Project Name?
Echo Project Number=%ProjNo%
Echo Project Address= %ProjAd%

set /p proceed=Is this correct? [Y/N]
if /i {%proceed%}=={y} (goto :yes)
if /i {%proceed%}=={yes} (goto :yes)
if /i {%proceed%}=={n} (goto :no)
if /i {%proceed%}=={no} (goto :no)

:no
cls
goto :start

:yes
set "currentpath=%cd%"
set "projfolder=%ProjNo%-%ProjAd%"

:: check if 000-Project Address doesn't exists
echo Does the Project folder exist %currentpath%\%ProjNo%-%ProjAd%?
If NOT exist "%currentpath%\%ProjNo%-%ProjAd%\" 
    (
    echo the folder does NOT exist
        ::check if project folder name doesn't existing
        if NOT exist "%currentpath%\%ProjAd%\" 
        (
            :: rename the folder to 000-Project Address
            echo Renaming Project Folder
            ren "%ProjAd%" "%ProjNo%-%ProjAd%"

            call:TopLevel
        )
        ::if project address name DOES exist
        else 
        (       
            ::enter the folder and check the structure.
            echo Project Folder doesn't need renaming
            call:TopLevel
        )
    )
::-----------------------------------------------------
::      if "000-Project Address" DOES exist
::-----------------------------------------------------

    else 
    (
        Echo There is no need to run this script!
        goto:eof
    )
  )
  ::-----------------------------------------------------
  ::    Recurse Top level
  ::
  ::   If all the "Recommended" directory names exist 
  ::   rename all sub-directories too!
  ::   Else create them all!
  ::-----------------------------------------------------
    :TopLevel 

    CD projfolder
        ::check if there's a project correspondence 
        IF exist "Project Correspondence" 
        (   
            ren "Project Correspondence" "%ProjNo%-Project Correspondence"
            ::rename the child folders
            cd "%ProjNo%-Project Correspondence"
            for /f "tokens=*" %G in ('dir /b /a:d "*"') do ren "%G" "%ProjNo%-%G"
            cd "%ProjNo%-client"
            for /f "tokens=*" %G in ('dir /b /a:d "*"') do ren "%G" "%ProjNo%-%G"
            cd ..\planning\


        )
        else 
        (
            mkdir "%ProjNo%-Project Correspondence"
            callto: 
            ::function makeProjCorrespondence
            call:makeProjCorrespondence %ProjNo% 
        )
        :: check if there's a project directory
        IF exist "Project Directory" 
        (
            ren "Project Directory" "%ProjNo%-Project Directory"
        )
        else 
        (
            mkdir "%ProjNo%-Project Directory"
        )
        if exist "Project Drawings" 
        (
            ren "Project Drawings" "%ProjNo%-Project Drawings"
            ::go to project drawings function
        )
        goto:eof

  :makeProjCorrespondence
  cd "%ProjNo%-Project Correspondence"
    mkdir "%ProjNo%-Planning"
    mkdir "%ProjNo%-Arboriculturalist"
    mkdir "%ProjNo%-CGI"
    mkdir "%ProjNo%-Client"
    mkdir "%ProjNo%-Measured Survey"
    mkdir "%ProjNo%-Post Planning Folders (Drop in as and when)"

    ::Make folders under Planning
    cd "%ProjNo%-Planning"
        mkdir "%ProjNo%-LA Planning"
        mkdir "%ProjNo%-Planning Consultants"
    cd "..\"

    ::Make folders in Client
    cd "%ProjNo%-Client"
        mkdir "%ProjNo%-Appointments"
        mkdir "%ProjNo%-Fees"
    cd "..\"

    ::make Post Planning folders
    cd "%ProjNo%-Post Planning Folders (Drop in as and when)"
        mkdir "%ProjNo%-Arboriculturalist"
        mkdir "%ProjNo%-Contract Administrator"
        mkdir "%ProjNo%-Contractor"
        mkdir "%ProjNo%-LA Building Control"
        mkdir "%ProjNo%-M&E Consultants"
        mkdir "%ProjNo%-Structural Engineer"
        mkdir "%ProjNo%-Tender, Specification"
    cd "..\..\"
    goto:TopLevel

提示和建议将不胜感激!

非常感谢

3 个答案:

答案 0 :(得分:1)

这旨在通过文件夹树递归,称为c:\database\project daredevil,并将001-添加到树中每个文件夹名称的前面。

@echo off
set "num=001-"
for /d /r "c:\database\project daredevil" %%a in (*) do ren "%%a" "%num%%%~nxa"

此代码将报告缺少哪些文件夹,并将创建丢失的文件夹 您可以使num等于空,以创建一组新的文件夹。

EDIT2:

@echo off
set "num=001-"
set "project=c:\database\project daredevil"

for %%a in (
"Project Correspondence"
"Project Directory"
"Project Drawings"
"Project Images"
"Project Correspondence\%num%Arboriculturalist"
"Project Correspondence\%num%CGI"
"Project Correspondence\%num%Client"
"Project Correspondence\%num%Measured Survey"
"Project Correspondence\%num%Planning"
"Project Correspondence\%num%Post Planning Folders (Drop in as and when)"
"Project Correspondence\%num%Client\%num%Appointments"
"Project Correspondence\%num%Client\%num%Fees"
"Project Correspondence\%num%Planning\%num%LA Planning"
"Project Correspondence\%num%Planning\%num%Planning Consultants"
"Project Correspondence\%num%Planning\%num%LA Planning\%num%Planning Application"
"Project Correspondence\%num%Planning\%num%LA Planning\%num%Pre Application"
"Project Correspondence\%num%Post Planning Folders (Drop in as and when)\%num%Arboriculturalist"
"Project Correspondence\%num%Post Planning Folders (Drop in as and when)\%num%Contract Administrator"
"Project Correspondence\%num%Post Planning Folders (Drop in as and when)\%num%Contractor"
"Project Correspondence\%num%Post Planning Folders (Drop in as and when)\%num%LA Building Control"
"Project Correspondence\%num%Post Planning Folders (Drop in as and when)\%num%M&E Consultants"
"Project Correspondence\%num%Post Planning Folders (Drop in as and when)\%num%Structural Engineer"
"Project Correspondence\%num%Post Planning Folders (Drop in as and when)\%num%Tender, Specification"
"Project Drawings\%num%CD Series (Construction Details)"
"Project Drawings\%num%Drawing Register"
"Project Drawings\%num%EW Series (External Works)"
"Project Drawings\%num%Outgoing Drawings"
"Project Drawings\%num%P Series (Planning)"
"Project Drawings\%num%RL Drawings (Room Layouts)"
"Project Drawings\%num%SK Series (Sketch Design)"
"Project Drawings\%num%X Series (Existing)"
"Project Drawings\%num%Outgoing Drawings\%num%DWG FILES"
"Project Drawings\%num%Outgoing Drawings\%num%PDF FILES"
) do (
   if not exist "%project%\%num%%%~a\" (
      echo "%project%\%num%%%~a\" is missing
      md "%project%\%num%%%~a\" 
   )
)
pause

答案 1 :(得分:1)

最终的代码是那些想要知道如何做的人!

非常感谢@foxidrive帮助解决这个问题! :)

@Echo off
:start
set /P ProjNo=Project Number:
set /P ProjName=Project Name:
::usually we clear screen but for the time being... let's not
::cls

Echo Please Confirm Project Details?
Echo Project Number=%ProjNo%
Echo Project Address= %ProjName%

set /p proceed=Is this correct? [Y/N]
if /i {%proceed%}=={y} (goto :yes)
if /i {%proceed%}=={yes} (goto :yes)
if /i {%proceed%}=={n} (goto :no)
if /i {%proceed%}=={no} (goto :no)

:no
cls
goto :start

:yes

set "project=%cd%\%ProjName%"

for %%a in (
"Project Correspondence"
"Project Directory"
"Project Drawings"
"Project Images"
"Project Correspondence\Arboriculturalist"
"Project Correspondence\CGI"
"Project Correspondence\Client"
"Project Correspondence\Measured Survey"
"Project Correspondence\Planning"
"Project Correspondence\Post Planning Folders (Drop in as and when)"
"Project Correspondence\Client\Appointments"
"Project Correspondence\Client\Fees"
"Project Correspondence\Planning\LA Planning"
"Project Correspondence\Planning\Planning Consultants"
"Project Correspondence\Planning\LA Planning\Planning Application"
"Project Correspondence\Planning\LA Planning\Pre Application"
"Project Correspondence\Post Planning Folders (Drop in as and when)\Arboriculturalist"
"Project Correspondence\Post Planning Folders (Drop in as and when)\Contract Administrator"
"Project Correspondence\Post Planning Folders (Drop in as and when)\Contractor"
"Project Correspondence\Post Planning Folders (Drop in as and when)\LA Building Control"
"Project Correspondence\Post Planning Folders (Drop in as and when)\M&E Consultants"
"Project Correspondence\Post Planning Folders (Drop in as and when)\Structural Engineer"
"Project Correspondence\Post Planning Folders (Drop in as and when)\Tender, Specification"
"Project Drawings\CD Series (Construction Details)"
"Project Drawings\Drawing Register"
"Project Drawings\EW Series (External Works)"
"Project Drawings\Outgoing Drawings"
"Project Drawings\P Series (Planning)"
"Project Drawings\RL Drawings (Room Layouts)"
"Project Drawings\SK Series (Sketch Design)"
"Project Drawings\X Series (Existing)"
"Project Drawings\Outgoing Drawings\DWG FILES"
"Project Drawings\Outgoing Drawings\PDF FILES"
) do (
   if not exist "%project%\%%~a\" (
      echo "%project%\%%~a\" is missing
      md "%project%\%%~a\"
   )
)
::Now add the prefixing number!
for /d /r "%project%" %%a in (*) do ren "%%a" "%ProjNo%-%%~nxa"

ren "%ProjName%" "%ProjNo%-%ProjName%"
Echo Project %ProjName folder has now been renamed to %ProjNo%-%ProjName%
pause

答案 2 :(得分:1)

我对有关模板目录的建议未被采纳感到有些失望,所以这是另一种解决方案。请注意,对所需结构的更改(更多目录等)将是自动化的 - 无需更改批处理...

@ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir\project template"
SET "destdir=c:\destdir"

SET "num=001-"
SET "project=project daredevil"

FOR /f "delims=" %%a IN (
 ' dir /s/b /ad "%sourcedir%" '
) DO (
 CALL :makecopy "%%a"
)

GOTO :EOF
:makecopy
SET "srce=%~1"
CALL SET "dest=%%srce:%sourcedir%\=%destdir%\%%
CALL SET "dest=%%dest:#=%num%%%
ECHO MD "%dest%"
IF NOT EXIST "%srce%\*" GOTO :eof
FOR %%i IN ("%srce%\*") DO (
IF NOT EXIST "%dest%\%%~nxi" ECHO COPY /b "%%i" "%dest%\%%~nxi"
)
GOTO :EOF

(注意MDCOPY仅仅ECHO用于测试 - 删除ECHO以执行

这里的一个优点是通过在%sourcedir%下面设置一个结构,当结构发生变化时,你所需要做的就是修改模板。

请注意,目录名中的任何#在执行时都会被%num%替换,因此可以轻松选择是否以及在何处注入项目编号。

另一个优点是,常见的表单 - 如空白电子表格或包含免责声明,表单,合法文件夹的doc文件 - 也可以放在模板结构中。例如,如果提交需要一系列步骤,您可以使用一个简单的电子表格来指定需要采取的步骤,并使用列来记录参考号码的人员,时间和文档。


澄清,扩展和示例,num设置为001-

@ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir\project template"
SET "destdir=c:\destdir"

SET "num=001-"
SET "project=project daredevil"

FOR /f "delims=" %%a IN (
 ' dir /s/b /ad "%sourcedir%" '
) DO (
 CALL :makecopy "%%a"
)

GOTO :EOF
:makecopy
SET "srce=%~1"
CALL SET "dest=%%srce:%sourcedir%\=%destdir%\%%
CALL SET "dest=%%dest:#=%num%%%
ECHO MD "%dest%"
IF NOT EXIST "%srce%\*" GOTO :eof
FOR %%i IN ("%srce%\*") DO (
 SET fname=%%~nxi
 CALL :subsfn
)
GOTO :EOF
:subsfn
CALL SET "mfname=%%fname:#=%num%%%
IF NOT EXIST "%dest%\%mfname%" ECHO COPY /b "%sourcedir%\%fname%" "%dest%\%mfname%"
GOTO :EOF

Dir structure:

c:\sourcedir\project template\some directory
c:\sourcedir\project template\some#directory
c:\sourcedir\project template\some#directory\a filename.txt
c:\sourcedir\project template\some#directory\a#filename.txt

Run result:

MD "c:\destdir\some directory"
MD "c:\destdir\some001-directory"
COPY /b "c:\sourcedir\project template\a filename.txt" "c:\destdir\some001-directory\a filename.txt"
COPY /b "c:\sourcedir\project template\a#filename.txt" "c:\destdir\some001-directory\a001-filename.txt"