批处理文件以创建具有可变前缀的文件夹

时间:2019-01-11 16:06:47

标签: batch-file directory prefix

我正在尝试使用Prefix批量创建文件夹,前缀是项目的名称。我不知道什么是达到以下结果的最佳方法。提前致谢。波纹管示例:

项目名称=项目1

project-1_export\project-1_DWG  
project-1_export\project-1_IMAGES 
project-1_export\project-1_PDF 
project-1_export\project-1_ZIP 
project-1_LIBRARY 
project-1_SUPPORT DOCUMENTS\project-1_Scans 
project-1_SUPPORT DOCUMENTS\project-1_PDF\project-1_Client 
project-1_SUPPORT DOCUMENTS\project-1_PDF\project-1_Others 

项目名称=项目2

project-2_export\project-2_DWG  
project-2_export\project-2_IMAGES 
project-2_export\project-2_PDF 
project-2_export\project-2_ZIP 
project-2_LIBRARY 
project-2_SUPPORT DOCUMENTS\project-2_Scans 
project-2_SUPPORT DOCUMENTS\project-2_PDF\project-2_Client 
project-2_SUPPORT DOCUMENTS\project-2_PDF\project-2_Others 

这是我在此处浏览资源后的尝试。

@echo off
set /p id= Enter Project ID?
for /f "tokens=*" %%a in (file.txt) do (
mkdir %id%_"%%a"
)

其中“ file.txt”包含文件夹和子文件夹的名称列表。
并且项目ID为Project 1/2/3

我的问题是

使用文本文件创建文件夹没有问题,但是我不知道如何使用它创建子文件夹。有可能吗?

如果是,如何在子文件夹名称的前面放置项目ID前缀?

2 个答案:

答案 0 :(得分:1)

按文件夹前进,我假设您要基于主project#目录重命名树中的所有文件。下面是一个树形波纹管示例:

C:\MainFolder
├───project1
│   ├───export
│   │   ├───DWG
│   │   ├───Images
│   │   ├───PDF
│   │   └───ZIP
│   └───support documents
└───project2
    ├───export
    │   ├───DWG
    │   ├───Images
    │   ├───PDF
    │   └───ZIP
    └───support documents

我们可以使用一系列for循环来使用dir "Path\*" /b /s /a:d收集有关每个文件夹的信息。这将使我们能够从受人尊敬的MainDir-> ProjectX中获取每个文件夹。从这里,我们只需将ProjectX文件夹之后的树重命名为ProjectX_OriginalFolderName格式。这将为我们提供新的树状波纹管。

C:\MainFolder
├───project1
│   ├───project1_export
│   │   ├───project1_DWG
│   │   ├───project1_Images
│   │   ├───project1_PDF
│   │   └───project1_ZIP
│   └───project1_support documents
└───project2
    ├───project2_export
    │   ├───project2_DWG
    │   ├───project2_Images
    │   ├───project2_PDF
    │   └───project2_ZIP
    └───project2_support documents

下面的脚本应该可以解决所有问题。请查看每个Rem | Note,以获取有关脚本中每个进程的更多信息。此外,您将需要将Set "MainDir=C:\MainFolderLocation"更改为包含project#文件夹的受尊重目录。

为了增加安全性,我还添加了Set "MustContain=project",因此该脚本将只修改project#包含project的文件树的身份。如果您在此目录(C:\MainFolderLocation中还有其他文件夹,则不会触摸不包含项目的文件夹。如果这不是您想要的,并且希望完全调整此目录中的所有内容,请删除Rem | Check If Current Folder Contains ProjectX for循环。

EditFolderTree.bat:

@ECHO OFF
@setlocal EnableDelayedExpansion

Rem | Configuration
Set "MainDir=C:\MainFolderLocation"
Set "MustContain=project"

Rem | Get Each Project Folder
for /f "tokens=*" %%A in ('dir "!MainDir!\" /b /a:d') do (

    Set "MainFolder=%%~nA"

    Rem | Check If Current Folder Contains ProjectX
    for /f "tokens=*" %%B in ('echo !MainFolder!^| find /I "!MustContain!"') do (

        Set "BaseFolderName=%%~nB"

        Rem | Get Each Folder In ProjectX Dir
        for /f "tokens=*" %%C in ('dir "!MainDir!\!BaseFolderName!\*" /b /s /a:d') do (

            Set "FolderName=%%C"

            Rem | Check If Current Folder Already has ProjectX_
            for /f "tokens=*" %%D in ('echo !FolderName!^| find /V "!BaseFolderName!_"') do (

                Rem | Save Results To File
                Echo %%D >> DataFile.temp

            )

        )

    )

)

Rem | Check If DataFile.temp Exists
If exist "%~dp0DataFile.temp" (

    Rem | Reverse Tree From TextFile
    for /F "tokens=*" %%A in (DataFile.temp) do (
        set /A I=!I! + 1
        set LINE!I!=%%A
    )
    Del %~dp0DataFile.temp
    for /L %%A in (!I!,-1,1) do (

        echo !LINE%%A! >> DataFile.temp

    )

    Rem | Read TextFile & Get Paths
    for /F "tokens=*" %%A in (DataFile.temp) do (

        Set "FolderName=%%~nA"

        Rem | Get project Number
        Set "String=%%A"
        Set "String=!String:%MainDir%\=!"
        for /F "tokens=1,2* delims=\" %%B in ('Echo !String!') do (

            Set "BaseFolderName=%%B"

        )

        Rem | Rename Folders
        CD "%%A\.."
        rename "!FolderName!" "!BaseFolderName!_!FolderName!"

    )
    Del %~dp0DataFile.temp

)
goto :EOF

要获取有关任何命令的帮助,请执行以下操作:

  • call /?
  • set /?
  • for /?
  • if /?
  • find /?
  • 等等。

答案 1 :(得分:0)

CreateProject.bat

@echo off
if "%~1" == "" goto :help
md "project-%~1_export\project-%~1_DWG  
md "project-%~1_export\project-%~1_IMAGES 
md "project-%~1_export\project-%~1_PDF 
md "project-%~1_export\project-%~1_ZIP 
md "project-%~1_LIBRARY 
md "project-%~1_SUPPORT DOCUMENTS\project-%~1_Scans 
md "project-%~1_SUPPORT DOCUMENTS\project-%~1_PDF\project-%~1_Client 
md "project-%~1_SUPPORT DOCUMENTS\project-%~1_PDF\project-%~1_Others 
goto :eof
:help
echo to create the folderstructure for a project 1, type:
echo %~n0 1

使用项目编号(或名称?)作为参数调用它:

createproject 1234