N
N
Nachalos2020-11-18 15:33:13
cmd/bat
Nachalos, 2020-11-18 15:33:13

CMD Command to create a folder for each file and move the file to the folder?

I need a command that can be run from the command line to create a folder for each file (based on the filename) in a directory and then move the file to the newly created folders.
Start folder:
BCJ-042102_1.jpg
BCJ-042102_2.jpg
BCJ-042102_3.jpg
DZPB-049001_1.jpg
DZPB-049001_2.jpg
DZPB-049001_4.jpg
BLN-021301_2.jpg
BLN-021301_3.jpg

What I want to achieve:
C: \BCJ-042102\BCJ-042102_1.jpg
C:\BCJ-042102\BCJ-042102_2.jpg
C:\DZPB-049001\DZPB-049001_1.jpg
C:\DZPB-049001\DZPB-049001_2.jpg
etc .
There are more than 5 tons of pictures in the main folder.
Can anyone help me with the team to do all this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
wisgest, 2020-11-18
@Nachalos

Getting from a string the part before the underscore:

for /f "delims=_" %J in ("стро_ка") do  rem Что то делаем с %J - частью строки до "_"

In your case:
for %I in (*.jpg) do  for /f "delims=_" %J in ("%I") do (
  if not exist "%J\"  md "%J"
  move "%~I" "%J\"
)

- copy the whole and paste into the console with CMD.EXE launched in the mode of processing commands from the input stream.
Or save to a batch file, replacing everything %with %%.

S
sergey, 2020-11-18
kuzmin @sergueik

dir

 Directory of C:\Users\Serguei\AppData\Local\Temp\xxx

11/18/2020  09:46 AM    <DIR>          .
11/18/2020  09:46 AM    <DIR>          ..
11/18/2020  09:46 AM                 0 BCJ-042102_1.jpg
11/18/2020  09:46 AM                 0 BCJ-042102_2.jpg
11/18/2020  09:46 AM                 0 BCJ-042102_3.jpg
11/18/2020  09:46 AM                 0 BLN-021301_2.jpg
11/18/2020  09:46 AM                 0 BLN-021301_3.jpg
11/18/2020  09:46 AM                 0 DZPB-049001_1.jpg
11/18/2020  09:46 AM                 0 DZPB-049001_2.jpg
11/18/2020  09:46 AM                 0 DZPB-049001_4.jpg
              8 File(s)              0 bytes

team
for /F %. in ('dir /b *.*') ; do mkdir %~n. && move /y %. %~n.

result
dir

Directory of C:\Users\Serguei\AppData\Local\Temp\xxx

1/18/2020  09:47 AM    <DIR>          .
1/18/2020  09:47 AM    <DIR>          ..
1/18/2020  09:47 AM    <DIR>          BCJ-042102_1
1/18/2020  09:47 AM    <DIR>          BCJ-042102_2
1/18/2020  09:47 AM    <DIR>          BCJ-042102_3
1/18/2020  09:47 AM    <DIR>          BLN-021301_2
1/18/2020  09:47 AM    <DIR>          BLN-021301_3
1/18/2020  09:47 AM    <DIR>          DZPB-049001_1
1/18/2020  09:47 AM    <DIR>          DZPB-049001_2
1/18/2020  09:47 AM    <DIR>          DZPB-049001_4
              0 File(s)              0 bytes
C:\Users\Serguei\AppData\Local\Temp\xxx\BCJ-042102_1\BCJ-042102_1.jpg
C:\Users\Serguei\AppData\Local\Temp\xxx\BCJ-042102_2\BCJ-042102_2.jpg
C:\Users\Serguei\AppData\Local\Temp\xxx\BCJ-042102_3\BCJ-042102_3.jpg
C:\Users\Serguei\AppData\Local\Temp\xxx\BLN-021301_2\BLN-021301_2.jpg
C:\Users\Serguei\AppData\Local\Temp\xxx\BLN-021301_3\BLN-021301_3.jpg
C:\Users\Serguei\AppData\Local\Temp\xxx\DZPB-049001_1\DZPB-049001_1.jpg
C:\Users\Serguei\AppData\Local\Temp\xxx\DZPB-049001_2\DZPB-049001_2.jpg
C:\Users\Serguei\AppData\Local\Temp\xxx\DZPB-049001_4\DZPB-049001_4.jpg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question