Moving Files to seperate folder using a .BAT

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Excursion
Posts: 8
Joined: 28 Jun 2010 19:35

Moving Files to seperate folder using a .BAT

#1 Post by Excursion » 28 Jun 2010 20:09

Hi all,

This is my first post on the board and want to say hi,

My problem is a real pain, I have tried several other boards without success and it is possible what I am asking cannot be achieved.

I have 100s of client files and 1000s of client folders.

I want a BAT program to automatically move files from a folder into a different folder using the clients name

Folders are named in the following format clients surname, first name (street)Format

Smith, John (High st)TIF
Brown, Craig (Low st)TIF
Rogers, Frank (Middle st)WAV

Files are named the same as the folders

Smith, John (High st)TIF.PDF
Brown, Craig (Low st)TIF.PDF
Rogers, Frank (Middle st)WAV.PDF

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Moving Files to seperate folder using a .BAT

#2 Post by aGerman » 29 Jun 2010 06:22

This should be easy to do, but tell us a bit more about the folder structure: Where are the files placed and where are the destination folders placed?

Regards
aGerman

Excursion
Posts: 8
Joined: 28 Jun 2010 19:35

Re: Moving Files to seperate folder using a .BAT

#3 Post by Excursion » 29 Jun 2010 16:58

The files are placed in...

D:\Files

The folders are placed in

D:\Folders\Customer name there are 1000's
D:\Folders\Brown, Dave (01 Main st)NSW
D:\Folders\Smith, John (02 Third St)ACT
D:\Folders\Jones, Mark (03 Second st)SA

This is good news if you think it is able to be done, I have been doing it manually for 100's of files and I was sure there was a way to get dos to do it.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Moving Files to seperate folder using a .BAT

#4 Post by aGerman » 29 Jun 2010 17:17

Code: Select all

@echo off &setlocal
pushd "D:\Files"
for %%a in (*.pdf) do (
  md "D:\Folders\%%~na" 2>nul
  move "%%a" "D:\Folders\%%~na\"
)

popd
pause


In your first post it seems all files are .pdf files. If that isn't true, come back with further informations.

Regards
aGerman

Excursion
Posts: 8
Joined: 28 Jun 2010 19:35

Re: Moving Files to seperate folder using a .BAT

#5 Post by Excursion » 29 Jun 2010 19:15

this is almost exactly what i am looking for, I did forget one piece of information that may be the difficult part...

The files are named

ABC Brown, Dave (01 Main st)NSW.PDF
ABC Smith, John (02 Third St)ACT.PDF
ABC Jones, Mark (03 Second st)SA.PDF


The folders in

D:\Folders\Brown, Dave (01 Main st)NSW
D:\Folders\Smith, John (02 Third St)ACT
D:\Folders\Jones, Mark (03 Second st)SA

The first ticket ABC in the filename needs to remain as ABC but the folder does not have this ticket so matching the file to the folder becomes tricky.

I do appologize for not including this in my first post.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Moving Files to seperate folder using a .BAT

#6 Post by aGerman » 30 Jun 2010 11:16

It's no problem. You would need one more FOR loop to split the file name on the first space.

Code: Select all

@echo off &setlocal
pushd "D:\Files"
for %%a in (*.pdf) do (
  for /f "tokens=1*" %%b in ("%%~na") do (
    md "D:\Folders\%%c" 2>nul
    move "%%a" "D:\Folders\%%c\"
  )
)

popd
pause



Regards
aGerman

Excursion
Posts: 8
Joined: 28 Jun 2010 19:35

Re: Moving Files to seperate folder using a .BAT

#7 Post by Excursion » 01 Jul 2010 18:03

This worked 100% Perfect,
many thanks for the coding.



I have another little problem of a similar nature.
Same idea as before but in reverse,

I have 1000's of files in a folder called...

D:/Files/

ABC328231-form_Dave Brown Winston Hills NSW-form.msg
ABC328231 CHARGE - Brown NSW.msg
ABC455332-form_John Smith Winston Hills ACT-form.msg
ABC455332 CHARGE - Smith NSW.msg
ABC767566-form_Mark Adams Winston Hills NT-form.msg
ABC767566 CHARGE - Adams NSW.msg
ABC234334-form_Frank West Winston Hills QLD-form.msg
ABC234334 CHARGE - West NSW.msg

I want to move only the files that correspond to the correct person to be moved into the persons folder.

Each person with a folder in D:\Folders has 2x files in D:\Files that are relevent to them.

I have a directory filled with peoples folders...

D:\Folders\Brown, Dave (01 Main st)NSW
D:\Folders\Smith, John (02 Third St)ACT
D:\Folders\Jones, Mark (03 Second st)NT
D:\Folders\West, Frank (04 Forth st)QLD

The problem is I dont want the BAT to create new folders inside D:\Folders\

It needs to know that if the name doesnt match then not to move the file from D:\Files\

Perhaps only 20% of the files inside D:\Files\ are relevent to the people in D:\Folders\

The left over files in D:\Files get deleted.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Moving Files to seperate folder using a .BAT

#8 Post by aGerman » 02 Jul 2010 11:02

When I read your post I saw that I added a line in the former code that is probably unintendet. If you don't want the batch file creates new folders you could change it this way:

Code: Select all

@echo off &setlocal
pushd "D:\Files"
for %%a in (*.pdf) do (
  for /f "tokens=1*" %%b in ("%%~na") do (
    move "%%a" "D:\Folders\%%c\" 2>nul
  )
)

popd
pause


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Your second issue is not that easy.
I would like to make sure that I understood it the right way:

Is it right that there is no space behind ABC?
Is it right that this ABC part has always 3 characters?
Is it right that the number behind the ABC characterizes a set of files that has to moove to the same folder?
Is it right that (according your example) Mark Adams' files should not be moved.

Regards
aGerman

Excursion
Posts: 8
Joined: 28 Jun 2010 19:35

Re: Moving Files to seperate folder using a .BAT

#9 Post by Excursion » 03 Jul 2010 23:30

Is it right that there is no space behind ABC?
Yes, ABC are the 1st 3 Letters of both files.

ABC328231-form_Dave Brown Winston Hills NSW-form.msg
ABC328231 CHARGE - Brown NSW.msg

Is it right that this ABC part has always 3 characters?
Yes, The layout is always exactly the same.

Is it right that the number behind the ABC characterizes a set of files that has to move to the same folder?
Yes, each number is generated specifically for that particular person making it unique.

Is it right that (according your example) Mark Adams' files should not be moved.
Yes, Mark Adams does not have a folder in this example so his files are not relevant and should not be moved.




This is one of the biggest problems with organizing my files and folders, due to the sheer volume of files, I have never attempted to fix them before. With your previous code it makes it possible to achieve my goal.

I am very interested to see if you are able to do this. Up to this point I have been manually searching the directory for the surname and moving them over 2 files at a time.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Moving Files to seperate folder using a .BAT

#10 Post by aGerman » 04 Jul 2010 06:58

OK, you could try something like that

Code: Select all

@echo off &setlocal

set "files=D:\files"
set "folders=D:\folders"

pushd "%files%"
REM ***find all files ending with -form.msg and call :proc for each found file
for %%a in (*-form.msg) do (
  set "file=%%~na"
  call :proc
)
popd
pause
goto :eof

::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:proc
setlocal
REM *** first 3 characters to variable %first%
set "first=%file:~,3%"
REM *** everything, excepting the first 3 characters to variable %Xfile%
set "Xfile=%file:~3%"
REM *** split %Xfile% to leading numbers and address (incl. form_)
for /f "delims=- tokens=1,2" %%i in ("%Xfile%") do set "number=%%i" &set "address=%%j"
REM *** extract the address
for /f "delims=_ tokens=2" %%a in ("%address%") do set "address=%%a"
REM *** extract the names
for /f "tokens=1,2" %%a in ("%address%") do set "forename=%%a" &set "surname=%%b"
REM *** find the last part
for %%a in (%address%) do set "last=%%a"
REM *** if the right folder exists ...
for /f "delims=" %%a in ('dir /ad /b "%folders%\%surname%, %forename% (*)%last%" 2^>nul') do (
  REM *** ... move the file ...
  move "%file%.msg" "%folders%\%%a\"
  REM *** ... look for further files starting with the same combination ...
  for /f "delims=" %%b in ('dir /a-d /b "%first%%number% *.msg" 2^>nul') do (
    REM *** ... and move them too
    move "%%b" "%folders%\%%a\"
  )
)
endlocal
goto :eof


Lines starting with REM are comments. You can remove them.

Regards
aGerman

Excursion
Posts: 8
Joined: 28 Jun 2010 19:35

Re: Moving Files to seperate folder using a .BAT

#11 Post by Excursion » 04 Jul 2010 17:57

That works perfectly, can the same process be achieved if the files are named slightly differerent.

Instead of
ABC328231-form_Dave Brown Winston Hills NSW-form.msg
ABC328231 CHARGE - Brown NSW.msg

the files are called
ABC328231 FORM - Dave Brown Winston Hills NSW - ABC328231.msg
ABC328231 CHARGE - Brown NSW.msg

The new emails coming threw have got a slightly different layout and the script doesnt work for them.

Excursion
Posts: 8
Joined: 28 Jun 2010 19:35

Re: Moving Files to seperate folder using a .BAT

#12 Post by Excursion » 04 Jul 2010 23:21

If it makes it easier you could just add a line to rename all the files in D:/Files

from

ABC328231 FORM - Dave Brown Winston Hills NSW - ABC328231.msg
ABC328231 CHARGE - Brown NSW.msg

To

ABC328231 FORM - Dave Brown Winston Hills NSW - FORM.msg
ABC328231 CHARGE - Brown NSW.msg

That would allow the script to identify the FORM.

Just an idea..

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Moving Files to seperate folder using a .BAT

#13 Post by aGerman » 05 Jul 2010 15:04

Well, it's very special and I think it becomes more and more uninteresting for other forum readers. I can't write a code that works for each exception.
(BTW: The purpose of this forum is NOT coding complete batch files. RentACoder.com is a website for such issues. Here you're on DosTips.com. You can get tips to write your own code.)

Last attempt:

Code: Select all

@echo off &setlocal

set "files=D:\files"
set "folders=D:\folders"

pushd "%files%"
for %%a in (*form*.msg) do (
  set "file=%%~na"
  call :proc
)
popd
pause
goto :eof

::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:proc
setlocal
set "first=%file:~,3%"
set "Xfile=%file:~3%"
for /f "tokens=1* delims=- " %%i in ("%Xfile%") do set "number=%%i" &set "address=%%j"
for /f "tokens=1* delims=_- " %%a in ("%address%") do set "address=%%b"
for /f "tokens=1,2" %%a in ("%address%") do set "forename=%%a" &set "surname=%%b"
:loop
for /f "tokens=1* delims=- " %%a in ("%address%") do (
  set "last=%x%"
  set "x=%%a"
  set "address=%%b"
)
if "%address%" neq "" goto :loop
for /f "delims=" %%a in ('dir /ad /b "%folders%\%surname%, %forename% (*)%last%" 2^>nul') do (
  move "%file%.msg" "%folders%\%%a\"
  for /f "delims=" %%b in ('dir /a-d /b "%first%%number% *.msg" 2^>nul') do (
    move "%%b" "%folders%\%%a\"
  )
)
endlocal
goto :eof



Regards
aGerman

Excursion
Posts: 8
Joined: 28 Jun 2010 19:35

Re: Moving Files to seperate folder using a .BAT

#14 Post by Excursion » 05 Jul 2010 16:37

Of Course,

Thank you for your help.

Post Reply