Solved Move files to multiple folders based on filename

October 12, 2014 at 14:22:02
Specs: Windows 7
Hello,
would anybody be able to help me with moving files to various folders using a batch file please? I have various reports that are generated as PDF's using a part name, variant, etc. I would like to move all files to their respective folders but so far I am only able to do this using the "move" command. The problem with this is is that it is not recursive and I don't have enough knowledge of dos to use "For /R" and "Delims". With What I have so far, I would have to run the script from every folder. Can anybody help?

Example file names: -
PartA__400__6101014386__02-006- 2__2014-10-8__2_17_11 pm
PartB__500__6101014416__01-005- 1__2014-10-8__2_17_11 pm

Explained: -
PartType__Derivitive__PartNumber__Model__StatusA-StatusB__Date and time stamp (which can be wild carded *).

What I have so far....
@echo off
move "PartA__400__*__02-006- *__*.pdf" "C:\Parts\PartA\400\02\006"
move "PartB__500__*__01-005- *__*.pdf" "C:\Parts\PartB\500\01\005"
end

Thank you in advance.
Paul.


See More: Move files to multiple folders based on filename

Report •


✔ Best Answer
October 28, 2014 at 18:31:21
Ok, I have a bad habit of posting "corrections" instead of revised scripts. Wires are very crossed! Thankyou for the current benchmark from your side. Here is what the script should look like (including your additional directories 'myscript' and 'end folder':

::--------- begin script
@echo off & setlocal enabledelayedexpansion
:: a simple translate-table
set 761618508=Part A
set 764426408=Part B
for /r %%z in (7*.pdf) do (

::-- this is the part that got hosed in transition
for /f "tokens=1,2,4,5 delims=_-" %%a in ("%%~nz") do (
call :yy "\%%c\%%d"
echo move %%z "c:\myscripts\end folder\!%%a!\%%b!k!"
>>test.bat echo move %%z "c:\myscripts\end folder\!%%a!\%%b!k!"
)
)
goto :eof
::--- the rest was ok from here down
:yy
set kk=%~1
:yy1
set k=%kk%
set kk=%k:\0=\%
if %kk% neq %k% goto :yy1
::======= end script
hope this one gets it. Thanks for being patient.



#1
October 12, 2014 at 20:16:53
Here's a preliminary probe, probably will need tweaking (or more):
@echo off & setlocal enabledelayedexpansion
for /r %%z in (part*.pdf) do (
for /f "tokens=1,2,4,5 deims=_-" %%a in ("%%~nz") do >>test.bat echo move "%%z" "c:\parts\%%a\%%b\%%c\%%d"
)
)
::===== end
not really tested, not armed (the output goes to another .BAT which supposedly hopeffully handles the move, but is not activated). Run this from the root of the PART filebase.

Report •

#2
October 13, 2014 at 07:27:49
Like nbrane's, not tested & safety on.

::====== script starts here ===============
::
:: paul.bat 2014-10-13 20:48:01.00
@echo off & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=* delims= " %%a in ('dir/b/s/a-d *.pdf') do (
set F=%%~Na
call :sub1
echo if not exist !DEST! md !DEST!
echo move "%%a" !DEST!
)
goto :eof

:sub1
for /f "tokens=1,2,4,5 delims=-_ " %%i in ("!F!") do (
set DEST=c:\parts\%%i\%%j\%%k\%%l
)
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#3
October 13, 2014 at 12:31:43
Thank you both for your replies. I've done some tweeking and managed to get your scripts to run and the files were output to the folder, however, each part, derivitive, model, etc has it's own folder on a network. So what I was looking for was for "PartA__400__*__02-006" to go to folder C:\PartA__400__*__02-006 and "PartB__500__*__01-005" to go to folder C:\PartB__500__*__01-005 and so on for each part respectively. There are only about six parts so if you could maybe give one or two examples if at all possible then I can add the rest. Apologies if I was unclear about this and thanks again.

Report •

Related Solutions

#4
October 13, 2014 at 17:54:53
Maybe you could give a good example. I'm totally confused now. Skip the asterisks, and give a couple of examples with no wildcards, then maybe we can come to an understanding. I'm lost at this point.

Report •

#5
October 15, 2014 at 12:54:47
Sorry for any confusion. Basically I need to files below to go to folders that already exist. The reports start out in various folders so I would like a batch file to look through the reports tree and move the files to the folder. I'll try and explain: -

==================================================================
This file:
761618508__400__6101014386__02-006- 2__2014-10-8__2_17_11 pm.pdf

Should go to this folder:
C:\761618508\400\02\006\Reports
==================================================================
This file:
764426408__300__6101014386__01-007- 2__2014-10-8__2_17_11 pm.pdf

Should go to this folder:
C:\764426408\300\01\007\Reports
==================================================================
PartType__Derivitive__PartNumber__Model__StatusA-StatusB__Date and time stamp

PartType = There are only 6 types. All begin with "7" and are 9 digits long.
Derivitive = There are only 5 derivatives 100, 200, 300, 400, 500.
Partnumber = isn't really important as the files will go into the folder based on PartType, derivative, Model and StatusA.
Model = Either 01 or 02.
StatusA = 001 to 007.

I hope this helps and thanks again.
Paul.


Report •

#6
October 15, 2014 at 17:33:27
The only difference I can see is you've added subdir "reports" to the end of the storage-tree. Can't you just add that to the last element in either script?
for /f "tokens=1,2,4,5 deims=_-" %%a in ("%%~nz") do >>test.bat echo move "%%z" "c:\parts\%%a\%%b\%%c\%%d\REPORTS"
Anythihg else looks like it's already been addressed...

Report •

#7
October 17, 2014 at 13:53:47
Thank you for your replies. I have exactly what I initially wanted until today when I hit a glitch. I realised that the part number is given a name in the folder structure. Any ideas how I can run the script that I've got so far and output to a different part name folder? Details are below: -

The original part name is 761618508.
Is is then placed in the folder called "Part A".

So, the pdf starts as:-
761618508__400__6101014386__02-006- 2__2014-10-8__2_17_11 pm.pdf

Should go to this folder
Part A__400__02-006- 2

As I mentioned in earlier posts, there are a few part numbers but each has a name:-
"761618508" goes to folder "Part A"
"764426408" goes to folder "Part B"
And so on....

The script so far is:-

@echo off & setlocal enabledelayedexpansion
for /r %%z in (7*.pdf) do (
for /f "tokens=1,2,4,5 delims=__-" %%a in ("%%~nz") do move "%%z" "C:\%%a\%%b\%%c\%%d"
)
)
end

Once that's done, that's it! Thank you.
Paul.


Report •

#8
October 17, 2014 at 18:38:19
@echo off & setlocal enabledelayedexpansion
:: a simple translate-table
set 761618508=Part A
set 764426408=Part B

for /r %%z in (7*.pdf) do (
for /f "tokens=1,2,4,5 delims=__-" %%a in ("%%~nz") do move "%%z" "C:\!%%a!\%%b\%%c\%%d"
)
)
::========= end script


Report •

#9
October 19, 2014 at 16:07:43
You make it look so easy........ the script works perfectly. Thanks again for all of your help.

Paul.


Report •

#10
October 24, 2014 at 17:08:06
Hi nbrane, sorry, I have another minor tweek (I hope). The script did work at home but when I got in onto the network I realised that the directory contains leading zero's which are output in the pdf but are not in the folder names. Do you know of an easy way to remove the leading zero's in the target folder in the above script? If it's not possible I'll ask the owner to rename all of the folders and amend with leading zero's as below.

So, the pdf starts as:-
761618508__400__6101014386__02-006- 2__2014-10-8__2_17_11 pm.pdf

Should go to this folder
Part A__400__2-6- 2
Part B__200__1-3- 2

Instead of:-
Part A__400__02-006- 2
Part B__200__01-003- 2

Thank you.
Paul.

message edited by par4724


Report •

#11
October 24, 2014 at 19:25:41
I'll make a quick stab. no guarantees. swap out this part:
for /f "tokens=1,2,4,5 delims=__-" %%a in ("%%~nz") do move "%%z" "C:\!%%a!\%%b\%%c\%%d"

swap in this:
for /f "tokens=1,2,4,5 delims=__-" %%a in ("%%~nz") do (
call :yy "\%%c\%%d"
echo move "%%z" "!%%a!\%%b!k!"
move "%%z" "!%%a!\%%b!k!"
)
)
goto :eof

:yy
set kk=%~1
:yy1
set k=%kk%
set kk=%k:\0=\%
if %kk% neq %k% goto :yy1

message edited by nbrane


Report •

#12
October 26, 2014 at 15:00:04
Thank you for your reply. Where would I insert the target directory (C:\MyScripts)?

Report •

#13
October 26, 2014 at 18:40:49
Can you pls give and example? Where does C:\myscripts enter the picture? Here is where it was left off:
move "%%z" "!%%a!\%%b!k!"

by target, I assume you mean the target of the move? Then pls illustrate with an example where to insert c:\myscripts...


Report •

#14
October 27, 2014 at 17:04:42
Yes, I mean the target of the move as in the previous script which had "do move "%%z" "C:\!%%a!\%%b\%%c\%%d"". As it is at the moment, I can't see "C:\" (the target folder) in your amended script and just wondered if that was correct as the script does not run, saying that the "filename, directory name or syntax is incorrect". As a side note, the guy that owns the directory folders said that, if I wanted to, I could rename all of the folders so that the previous script will run, which it does, so that is another option but I want to understand the amended script.

Thank you.


Report •

#15
October 27, 2014 at 20:04:06
Yeh, my bad! Sorry, I did leave out the drive-letter and root:
echo move "%%z" "!%%a!\%%b!k!"
move "%%z" "!%%a!\%%b!k!"

should be:
echo move "%%z" "c:\!%%a!\%%b!k!"
move "%%z" "c:\!%%a!\%%b!k!"

Let me know if that doesn't fix it.


Report •

#16
October 28, 2014 at 17:38:23
Hi nbrane, thanks for your reply. Here's where we're at at the moment.

The script so far...
@echo off & setlocal enabledelayedexpansion
::Orignal File - 761618508__400_6414100700352__02-006- 1__2014-10-8__7_21_17 pm.pdf
::End file - C:\MyScripts\End Folder\Part A\400-2-6
set 761618508=Part A
set 764426408=Part B
for /r %%z in (7*.pdf) do (for /f "tokens=1,2,4,5 delims=__-" %%a in ("%%~nz") do >>test.txt echo move (
call :yy "\%%c\%%d"
echo move "%%z" "C:\MyScripts\End Folder\!%%a!\%%b!k!"
)
)
goto :eof

:yy
set kk=%~1
:yy1
set k=%kk%
set kk=%k:\0=\%
if %kk% neq %k% goto :yy1

pause


Gives the following....
Press any key to continue . . .
move "C:\MyScripts\761618508__300_6414100700352__02-005- 1__2014-10-8__7_21_17 p
m.pdf" "C:\MyScripts\End Folder\\%b\d"
Press any key to continue . . .
move "C:\MyScripts\764426408__200_6414100700257__01-002- 1__2014-10-8__7_21_17 p
m.pdf" "C:\MyScripts\End Folder\\%b\d"
Press any key to continue . . .

Maybe I screwed up or something? I can see that it's close but not quite right.
Thanks again for all of your time.
Paul.


Report •

#17
October 28, 2014 at 18:31:21
✔ Best Answer
Ok, I have a bad habit of posting "corrections" instead of revised scripts. Wires are very crossed! Thankyou for the current benchmark from your side. Here is what the script should look like (including your additional directories 'myscript' and 'end folder':

::--------- begin script
@echo off & setlocal enabledelayedexpansion
:: a simple translate-table
set 761618508=Part A
set 764426408=Part B
for /r %%z in (7*.pdf) do (

::-- this is the part that got hosed in transition
for /f "tokens=1,2,4,5 delims=_-" %%a in ("%%~nz") do (
call :yy "\%%c\%%d"
echo move %%z "c:\myscripts\end folder\!%%a!\%%b!k!"
>>test.bat echo move %%z "c:\myscripts\end folder\!%%a!\%%b!k!"
)
)
goto :eof
::--- the rest was ok from here down
:yy
set kk=%~1
:yy1
set k=%kk%
set kk=%k:\0=\%
if %kk% neq %k% goto :yy1
::======= end script
hope this one gets it. Thanks for being patient.


Report •

#18
October 29, 2014 at 16:20:44
That's it, you did it nbrane!!!! I have exactly what I wanted in this new script. I promise to stop pestering you now.

Thanks again for all of your time and help, I really appreciate it.
Paul.


Report •


Ask Question