0

I would like to create folders from a prefix of certain file then copy multiple files with a certain string in their file name to that folder. For example:

VI files to a folder called DM0008 with the following files

DM0008d3VI_001.IMI
DM0008d3VI_002.IMI

MI files to a folder called DM0008MI with the following files

DM0008d6MI_002.IMI
DM0008d6MI_003.IMI

I did try this but this created a folder for each individual file:

@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"
)

3 Answers 3

1

Assuming that the first part of the file name (DM####) will always be 6 characters long you could do this:

@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!"
)
Sign up to request clarification or add additional context in comments.

Comments

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 for Windows

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.