Solved Batch move multiple files into corresponding folders

May 8, 2020 at 09:02:56
Specs: Windows 10
I have a project where I have >90,000 files that need to be batch moved into separate folders (which are already created) where the first string of numbers on the file name will be moved into a folder with those same string of numbers.

What I have so far is this:

@echo on
setlocal enabledelayedexpansion
cls

pushd c:\folders\

for /f "tokens=*" %%1 in ('dir /a-d /b *') do (
    set filename=%%1&set dirname=!filename:~0,7!

        for /f "tokens=*" %%A in ('dir /ad /b') do (
        set dirid=%%A&set dirid=!dirid:~0,7!

        if "!dirid!" equ "!dirname!" move %%1 %%A
  )        
)

The issue I am stuck with is that the string of numbers on the file name and corresponding folder name can vary in length from one digit up to seven digits.

For example, I have a string of files named:

12_lastname_firstname_DOB_1_1_1987_DOS_8_1_2020

12_lastname_firstname_DOB_1_1_1987_DOS_8_2_2020

12_lastname_firstname_DOB_1_1_1987_DOS_8_3_2020

1234_lastname_firstname_DOB_2_2_1986_DOS_8_1_2020

1234_lastname_firstname_DOB_2_2_1986_DOS_8_2_2020

1_lastname_firstname_DOB_3_3_1985_DOS_8_1_2020

1234567_lastname_firstname_DOB_4_4_1984_DOS_8_1_2020

1234567_lastname_firstname_DOB_4_4_1984_DOS_8_2_2020

1234567_lastname_firstname_DOB_4_4_1984_DOS_8_3_2020

1234567_lastname_firstname_DOB_4_4_1984_DOS_8_4_2020

etc...

The destination folders are all named with the matching digits from the first 1-7 digits of the files.

Currently as it stands, I can run the bat and it will move all of the files that meet the requirements of the code with 7 numbers. I wonder if there is a way to set the filename to look for the delimiter of "_" so that it stops after the first "_" for the file, and matched the numbers up to the delimiter?

Any help would be greatly appreciated!!

Thank you for any help you can offer!!


See More: Batch move multiple files into corresponding folders

Reply ↓  Report •

#1
May 8, 2020 at 10:10:26
moves all 1_* files to c:\folders\1
moves all 12_* files to c:\folders\12
moves all 1234_* files to c:\folders\1234
moves all 1234567_* files to c:\folders\1234567

like this:

@echo on
pushd "c:\folders\"
for /f "tokens=1,* delims=^_" %%a in ('dir /a-d/b') do move "%%a_%%b" "c:\folders\%%a"

specs: https://www.userbenchmark.com/UserR...

message edited by hidde663


Reply ↓  Report •

#2
May 8, 2020 at 10:15:40
Dont know how to do that in batch, but in powershell you could do something like this:

Get-ChildItem -Path "folder\path" -Filter "*12_*" | Move-Item -Destination -Path "folder\path"

And if you want to check and move files from any sub folders then use -Recurse like so:

Get-ChildItem -Path "path" -Filter "*part of file name here*" -Recurse


Reply ↓  Report •

#3
May 11, 2020 at 09:31:34
✔ Best Answer
I reworked the entire code and came up with this which works!

Thank you to anyone who offered help!

@echo off
setlocal enabledelayedexpansion
set target=
set /p target=Enter the full path to the working directory:
echo target entered: %target%
pause
if not defined target set target=c:\somewhere
echo tentative: %target%, preparing to CD
cd %target% || (set /p =%target% not accessible&goto :eof)
echo CD was successful to %target%
pause
for /f "tokens=1,* delims=^_" %%a in ('dir /b /a-d *.*') do (
:: TRAILING SPACE REQUIRED IN FOLL LINE!
set copy=move "%%a_%%b" &::
call :alltrim "%%a" x
set copy=!copy!\
call :alltrim "%%a_%%b"
:: i put this back in to show what's trying to happen
echo !copy!
!copy!
rem PAUSE is optional as content of this line, if you want to test
)
goto :eof

:alltrim
set y=%1
:1
set yy=%y%
set y=%y: "="%
set y=%y:" ="%
if %y% neq %yy% goto 1
set copy=%copy%%y%
if "%2" neq "" md %y% 2>nul
::======= end batchscript


Reply ↓  Report •
Related Solutions


Ask Question