user3108650
user3108650

Reputation: 1

Batch file move specific files

I have a very large number of files with this template:

example1.part001.rar
example1.part002.rar
example1.part003.rar
...

example2.part001.rar
example2.part002.rar
example2.part003.rar
...

and sometimes with only two digits after "part"

example3.part01.rar
example3.part02.rar
example3.part03.rar
...

I'm trying to create a batch file that first makes n directories (n is for number of groups of files) with the name " example n ", and then that moves all relatives file into them. I will explain better.

I have

cat.part01.rar
cat.part02.rar
cat.part03.rar
dog.part001.rar
dog.part002.rar
mouse.part01.rar
mouse.part02.rar
mouse.part03.rar
mouse.part04.rar

I want to first make the directories "cat", "dog" and "mouse, and then move all relatives file into them (I mean all "foldername.part*.rar in "foldername"), so "cat.part*.rar" files into "cat" folder and so on. Files are many, and filenames are very very different and random.

Can somebody help me? Thanks

Upvotes: 0

Views: 1011

Answers (4)

foxidrive
foxidrive

Reputation: 41267

This should do it also, and handle filenames with ! in them too.

@echo off
for %%a in (*.rar) do (
 if exist "%%a" for %%b in ("%%~na") do (
    md "%%~nb" 2>nul
    move "%%~nb.part*.rar" "%%~nb" >nul
 )
)
pause

Upvotes: 1

SachaDee
SachaDee

Reputation: 9545

EDIT :

@echo off&cls
setlocal EnableDelayedExpansion

for %%a in (*.rar) do (set $file=%%~na
                       set $file=!$file:^.= !
                       call:work %%a)
exit /b
:work
for %%b in (!$file!) do (if not exist %%b md %%b
                         copy "%1" ".\%%b"
                         exit /b)

Upvotes: 1

D3F4ULT
D3F4ULT

Reputation: 934

Example files of test:

ball.part1.rar
ball.part2.rar

cat.part01.rar
cat.part02.rar

dog.part001.rar
dog.part002.rar

The script creates folders ball, cat, dog, after that, it moves the files to their respective folders, works with partX, partXY and partXYZ in filename

@echo off
@break off
@title Batch file move specific files
@color 0a
@cls

setlocal EnableDelayedExpansion

if "%~1" NEQ "" (
  set "WORKINGDIR=%~1"
) else (
  set "WORKINGDIR=!CD!"
)

if not exist "!WORKINGDIR!\*.rar" (
  set "WORKINGDIR=!CD!"
)

if not exist "!WORKINGDIR!\*.rar" (
  echo There are no winrar files in this directory
  echo.
  echo Exiting...
  echo.
  ping -n 4 localhost>nul
  pause
  exit
)

for %%a in ( "!WORKINGDIR!\*.rar" ) do (
  set "name=%%~nxa"
  if "!name:~-4!" EQU ".rar" (
    set "name=!name:~0,-4!"
  )
  if "!name:~-8,5!" EQU ".part" (
    echo !name:~-7! | find ".">nul
    if "!errorlevel!" NEQ "0" (
      set "name=!name:~0,-8!"
    ) else (
      set "skip=yes"
    )
  ) else if "!name:~-7,5!" EQU ".part" (
    echo !name:~-6! | find ".">nul
    if "!errorlevel!" NEQ "0" (
      set "name=!name:~0,-7!"
    ) else (
      set "skip=yes"
    )
  ) else if "!name:~-6,5!" EQU ".part" (
    echo !name:~-5! | find ".">nul
    if "!errorlevel!" NEQ "0" (
      set "name=!name:~0,-6!"
    ) else (
      set "skip=yes"
    )
  )
  if not defined skip (
    set "skip=no"
  )
  if "!skip!" EQU "no" (
    if not exist "!WORKINGDIR!\!name!\" (
      mkdir "!WORKINGDIR!\!name!\"
    )
    move /y "!WORKINGDIR!\%%~nxa" "!WORKINGDIR!\!name!\%%~nxa"
  )
  set "skip=no"
)

pause
exit

Upvotes: 0

SethB
SethB

Reputation: 2090

Here I get a list of groups by splitting the file name by '.'. I then get the uniq values of the first column. For each of the results I create the directory. Then move all files into the directory

#/usr/bin/sh

groupnames=(`ls *.rar | awk -F"." '{print $1}' | uniq`)

for i in ${groupnames[*]}; do

  mkdir $i
  mv $i.*.rar ./$i/

done

Upvotes: 0

Related Questions