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

标签 c# batch-file vb6 powershell-3.0 jobs

我有这样名字的文件:

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循环合并 MDIF状况。我使用下划线字符作为分隔符并将 token 设置为 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# - 批处理从文件名中提取一部分并用这部分创建一个文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42033925/

相关文章:

c# - 使用来自 VB6 代码的 C# 程序集。一些问题

vb6 - 记录 Visual Basic 6.0 代码

c# - 为 Firebase 生成 JWT

c# - 随机时间间隔后调用方法

ms-access - 检索 Access 表的下一个自动编号

bash - 我可以使用批处理文件调用 git bash 然后向其传递命令吗?

javascript - 通过命令行/批处理文件打开页面时将 javascript 代码注入(inject) Google Chrome

c# - Google OAuth2.0 中的 ProcessUserAuthorization 抛出 400 错误

c# - 从匹配中生成正则表达式

java - WildFly 独立版无法运行