我有这样名字的文件:
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循环合并 MD与 IF状况。我使用下划线字符作为分隔符并将 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/