批处理从文件名中提取一部分并用这部分创建一个文件夹

Batch takes a part from the file name and create a folders with this part

我有这样的文件:

414_gtmlk_videos_Mas_147852_hty1147.xls
414_gtmlk_videos_Mas_P147852_hty1147.txt

我想创建一个作业来检查文件名并取文件名中 Mas 之后的部分 (147852-P147852) 并使用此零件名称创建一个文件夹(文件夹名称应为:147852-P147852)。 最后将每个文件移动到他的文件夹中。

Batch takes a part from the file name and create a folders with this part i have files with names like this: 414_gtmlk_videos_Mas_147852_hty1147.xls 414_gtmlk_videos_Mas_P147852_hty1147.txt (the folder name will be here:147852-P147852)

这是一种使用批处理脚本执行此操作的方法,因为您在问题中将其标记为 batch-file。只需相应地设置你的源目录,其余的就应该根据你提供的细节和我的理解来工作。

我用了简单的批处理FOR /F loop incorporating MD with IF条件。我使用下划线字符作为分隔符并将标记设置为 5 以使其工作。

@ECHO ON

SET Src=C:\Folder\Path
FOR /F "TOKENS=5 DELIMS=_" %%F IN ('DIR /B /A-D "%Src%\*.txt"') DO (
    IF NOT EXIST "%Src%\%%~F-P%%~F" MD "%Src%\%%~F-P%%~F"
    IF EXIST "%Src%\*%%~F*P%%~F*.txt" MOVE /Y "%Src%\*%%~F*P%%~F*.txt" "%Src%\%%~F-P%%~F"
)
GOTO EOF

更多资源

  • FOR /F
  • IF
  • MD
  • FOR /?

    delims=xxx      - specifies a delimiter set.  This replaces the
                      default delimiter set of space and tab.
    
    tokens=x,y,m-n  - specifies which tokens from each line are to
                      be passed to the for body for each iteration.
                      This will cause additional variable names to
                      be allocated.  The m-n form is a range,
                      specifying the mth through the nth tokens.  If
                      the last character in the tokens= string is an
                      asterisk, then an additional variable is
                      allocated and receives the remaining text on
                      the line after the last token parsed.
    

我在下面有一些 C# 代码。第一部分执行以下操作:

  • 获取路径
  • 获取文件名
  • 修改完整路径以获得“147852”部分,在__Mas_和最后一个_

    之间
        string pathToGetFile = @"C:\";
        string[] filePaths = System.IO.Directory.GetFiles(pathToGetFile +@"\", "*_Mas_*");
        string[] fullName = new string[filePaths.Length]; 
    
        for (int i = 0; i < filePaths.Length; i++)
        {
            fullName[i] = filePaths[i].Substring(filePaths[i].LastIndexOf("\") + 1);
    
            filePaths[i] = filePaths[i].Substring(filePaths[i].LastIndexOf("_Mas_") + 5);
            int l = filePaths[i].IndexOf("_"); 
            filePaths[i] = filePaths[i].Substring(0, l);
    

现在您可以用自己的名字创建文件夹 文件路径现在是这样的:147852,P147852

            if (!Directory.Exists(@"C:\" + filePaths[i]))
                System.IO.Directory.CreateDirectory(@"C:\" + filePaths[i]);

        }

现在只需将文件移动到新目录

        for (int i = 0; i < filePaths.Length; i++)
        {
            string sourceFile = System.IO.Path.Combine(pathToGetFile, fullName[i]);
            string destFile = System.IO.Path.Combine(@"C:\" + filePaths[i], @"C:\" + filePaths[i] + "\" + fullName[i]);

            File.Copy(sourceFile,destFile,true);
        }

现在,会发生什么

文件:

  • C:\414_gtmlk_videos_Mas_147852_hty1147.xls
  • C:\414_gtmlk_videos_Mas_P147852_hty1147.txt

它们将根据以下内容进行复制:

  • C:\147852\
  • C:\P147852\