在文件夹名称中搜索子字符串

时间:2016-08-25 07:02:45

标签: batch-file search substring directory

我希望有人可以帮我解决问题。

每个订单都有一个包含文件夹的目录,例如

L:\Med_Department\Orders\
L:\Med_Department\Orders\T0012345_AB100_CustomerA_site2
L:\Med_Department\Orders\T0012346_CD350-CustomerB site1
...

如何通过搜索订单号获取文件夹的全名。 当我输入订单号时,例如:

  

T0012345

, 我希望脚本存储

  

T0012345_AB100_CustomerA_site2

在一个变量中,所以我可以用它为我的新项目而不是订单号创建文件夹。

订单号是唯一的,格式为Txxxxxxx。文件夹名称的其余部分可以用下划线,空格或其他字符分隔。

如果未找到订单号(例如订单号未正确输入),则用户应再次输入订单号。

这是我迄今取得的成就。 我输入订单号,脚本将模板文件夹复制到我的项目目录中,订单号为文件夹名称。

@Echo off

rem directories
set template= "N:\Documentation\New_folder_OrderNo-Type-Customer-site"
set dirOrder= "L:\Med_Department\Orders\"
SET dirProjects= "Z:\Projects\2016\"

rem input Order No.
echo Please enter the order number?
echo.
set /p OrderNo=OrderNo:

rem target directory 
set dirNewProject=%dirProjects%%OrderNo%\
echo %dirnewProject%

pause
rem copy content from template into target directory
xcopy %template% %dirNewProject% /S /E /C /H /O /R /Y /D /V

rem open target directory
explorer %dirNewProject%

;exit

提前致谢。

4 个答案:

答案 0 :(得分:1)

类似的东西:

        topology.newStream("process", new RawDataSpout())
                .parallelismHint(1)
                .shuffle()
                .each(new Fields("frameID"), new RawToRgb(), new Fields("frameIDRawToRgbCompleted"))
                .parallelismHint(1)
                .shuffle()
                .each(new Fields("frameID", "frameIDRawToRgbCompleted"), new StereoInit(), new Fields("frameIDInitCompleted"))
                .parallelismHint(1)
                .setMemoryLoad(6144)
                .shuffle()
                .each(new Fields("frameID", "frameIDRawToRgbCompleted", "frameIDInitCompleted"), new StereoRing(), new Fields("frameIDRingCompleted"))
                .parallelismHint(1)
                .shuffle()
                .each(new Fields("frameID", "frameIDRawToRgbCompleted", "frameIDInitCompleted", "frameIDRingCompleted"), new Rendering(), new Fields("frameIDRenderingCompleted"))
                .parallelismHint(1)
                .setMemoryLoad(6144);

答案 1 :(得分:0)

使用for获取命令的输出。使用dir和正确的开关进行通配符搜索(注意:使用dir通配符只能在路径的最后一个元素内工作。)

:again
set /p "OrderNo=OrderNo: "
set "folder=none"
for /f "delims=" %%a in ('dir /s /ad /b %dirOrder%%OrderNo%*') do set folder=%%a
if %folder% == none goto :again
echo found: %folder%

有关详细信息,请参阅for /?dir /?

答案 2 :(得分:0)

您只需对模式for /D使用%OrderNo%*即可。如果有名称以%OrderNo%开头的文件夹(应该只有一个,如果我做对了),dirNewProject将保持其路径;如果没有,则变量将为空。之后您可以if [not] defined dirNewProject chek并按顺序跳转到:LABEL

rem (skipping data entry part of script)

rem directory 
set "dirNewProject="
for /D %%I in ("%dirProjects%\%OrderNo%*") do set "dirNewProject=%%I"
if not defined dirNewProject goto :LABEL

rem (skipping file copy part of script)

答案 3 :(得分:0)

示例... (未测试的)

@Echo Off
SetLocal

Rem directories
(Set template=N:\Documentation\New_folder_OrderNo-Type-Customer-site)
(Set dirOrder=L:\Med_Department\Orders)
(Set dirProjects=Z:\Projects\2016)

Rem exit if above directories do not exist
For %%a In (template dirOrder dirProjects) Do (If Not Exist "%%%%a%%\" Exit/B)

Rem make search dir current
If /I Not "%CD%" Equ "%dirOrder%" PushD %dirOrder%

:AskNum
Rem input Order No.
Echo(
Echo( Please enter the order number?
Echo(
Set/P "OrderNo=OrderNo: "

For /D %%a In ("%OrderNo%*") Do (Set OrderDir=%%~a)
If Not Defined OrderDir (Choice /C /M "Would you like to try again?"
    If Errorlevel 2 Exit/B
    GoTo :AskNum)

Rem new project directory 
(Set dirNewProject=%dirProjects%\%OrderDir%)

Rem copy content from template to new project directory
If Not Exist "%dirNewProject%\" (
    RoboCopy "%template%" "%dirNewProject%" /E /Copy:DATSO)

Rem open new project directory
Explorer "%dirNewProject%"