0

我想从某个文件的前缀创建文件夹,然后将文件名中带有某个字符串的多个文件复制到该文件夹​​。例如:

VI 文件到名为DM0008的文件夹中,其中包含以下文件

DM0008d3VI_001.IMI
DM0008d3VI_002.IMI

MI 文件到名为DM0008MI的文件夹中,其中包含以下文件

DM0008d6MI_002.IMI
DM0008d6MI_003.IMI

我确实尝试过,但这为每个单独的文件创建了一个文件夹:

@echo off
for %%a in (*VI*) do (
  md "%%~na" 2>nul
  move "%%~na.*" "%%~na"
)
for %%a in (*MI*) do (
  md "%%~na" 2>nul
  move "%%~na.*" "%%~na"
)
4

3 回答 3

1

假设文件名 ( DM####) 的第一部分总是 6 个字符长,您可以这样做:

@echo off

setlocal EnableDelayedExpansion

for %%a in (*VI*) do (
  set "folder=%%~na"
  set "folder=!folder:~0,6!"
  if not exist "!folder!" md "!folder!"
  move "%%~nxa" "!folder!"
)
for %%a in (*MI*) do (
  set "folder=%%~na"
  set "folder=!folder:~0,6!MI"
  if not exist "!folder!" md "!folder!"
  move "%%~nxa" "!folder!"
)
于 2013-08-20T11:06:10.910 回答
0
@ECHO OFF &SETLOCAL
FOR /f "delims=" %%a IN (file) DO (
    FOR /f "delims=" %%b IN ('echo("%%~na"^|sed -r "/VI/s/(..[0-9]+).*/\1/;/MI/s/(..[0-9]+).*/\1MI/"') DO (
        ECHO MD "%%~b" 2>NUL
        ECHO MOVE "%%~fa" "%%~b"
    )
)

sed 用于 Windows

于 2013-08-20T12:32:40.900 回答
0

If your only directories to use are DM0008 and DM0008MI and your file patterns are DM0008d3VI_*.IMI and DM0008d6MI_*.IMI then:

@ECHO OFF
IF EXIST DM0008d3VI_*.IMI (
    MD DM0008
    MOVE DM0008d3VI_*.IMI DM0008
)
IF EXIST DM0008d6MI_*.IMI (
    MD DM0008MI
    MOVE DM0008d6MI_*.IMI DM0008MI
)

If this is not your specification, please, make it clearer is your question.

于 2013-08-20T15:06:53.153 回答