BATCH脚本-创建一个包含文件名特定部分的文件夹 [英] BATCH Script - Create a folder with a specific part of the filename
本文介绍了BATCH脚本-创建一个包含文件名特定部分的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要你们的帮助,我一直在批量编写脚本,在其中我需要使用文件名的一部分来创建文件夹。
I need help from you guys, I have been writing a script in Batch where I need to create a folder by using a part of the name of the file.
我需要例如:
为此文件:20200614_SAP_ZCMF_MB51_V1.csv
我只需要文件夹SAP_ZCMF_MB51的名称。
I need to have for example : for this file : 20200614_SAP_ZCMF_MB51_V1.csv I only need for the name of the folder SAP_ZCMF_MB51.
我需要删除日期和版本
这是允许我创建文件夹的代码:
Here is the code that allows me to create a folder :
@echo off
setlocal enabledelayedexpansion
for %%A in (*.csv) do (
echo file found %%A
for /f "delims=" %%B in ("%%A") do set fname=%%~nB
for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
for /f "tokens= 2,3 delims=_" %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
if not exist "!folname!" (
echo Folder !folname! does not exist, creating
md "!folname!"
) else (
echo Folder !folname! exists
)
echo Moving file %%A to folder !folname!
move "%%A" "!folname!"
)
echo Finished
pause
谢谢!
推荐答案
此处快速重写了已发布的代码,而没有不必要的set命令,并使用了适当的标记和定界符作为您提供的文件名模式。
Here is a quick rewrite of your posted code, without the unnecessary set commands, and using the appropriate tokens and delimiters for the file name pattern you've provided.
@Echo Off
SetLocal EnableExtensions
For %%A In (????????_*_*_*_*.csv) Do (
Echo File found %%A
For /F "Tokens=2-4 Delims=_" %%B In ("%%~nA") Do (
Echo Folder name %%B_%%C_%%D"
If Not Exist "%%B_%%C_%%D\" (
Echo Folder %%B_%%C_%%D does not exist, creating
MD "%%B_%%C_%%D"
) Else Echo Folder %%B_%%C_%%D exists
Echo Moving file %%A to folder %%B_%%C_%%D
Move /Y "%%A" "%%B_%%C_%%D"
)
)
Echo Finished
Pause
这篇关于BATCH脚本-创建一个包含文件名特定部分的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文