How can I move files to certain folders?

November 18, 2010 at 14:04:52
Specs: Windows 7
I have a question regarding creating a batch file to do the following:


I'm currently have about 100 files (every month I receive a zip with batch file). The file format as follow:

xx_yy_uuuu_mmdd_ccyy_q.pdf (same for all files, where uuuu is the diiferent ID # for each file)

Examples:
LR_INV_2018_1029_2010_3.pdf
LR_INV_2020_1029_2010_3.pdf
LR_INV_2021_1029_2010_3.pdf

So, the uuuu is change in each file name. I like to move the file to the same folder contains that 4 digits name.

I like to move:

LR_INV_2018_1029_2010_3.pdf to folder start with: 2018 St Mary Church Data
LR_INV_2020_1029_2010_3.pdf to folder start with: 2020 St Joseph Church Data
LR_INV_2021_1029_2010_3.pdf to folder start with: 2021 St Michael Church Data

and so on...

Is there a .bat script for this? Or can somebody give me the script? Thanks.


See More: How can I move files to certain folders?

Report •


#1
November 18, 2010 at 19:56:11
If I've understood the problem correctly, this should work:

@ECHO OFF
SETLOCAL EnableDelayedExpansion

FOR /f %%a IN ('DIR /b "P:\a\t\h\*.pdf"') DO (
SET Var=%%a
CALL :Sub1
)

:Sub1

FOR /f "tokens=3 delims=_" %%A IN ('ECHO !Var!') DO (
IF "%%A"=="2018" MOVE "%%a" "2018 St Mary Church Data"
IF "%%A"=="2020" MOVE "%%a" "2020 St Joseph Church Data"
IF "%%A"=="2021" MOVE "%%a" "2021 St Michael Church Data"
)

GOTO :EOF

Can I put a question here?


Report •

#2
November 18, 2010 at 20:10:29
Matt,
Wow, that was quick. However, what I want to do is to move the file with that 4 digits uuuu to the same folder start with uuuu. (example: uuuu is 2018 in the file name, and the folder start with 2018 xxxxxx)

I have 100 files. Each file the 4 digits uuuu is different, and I already have 100 folders created with the first 4 digits uuuu and more characters after that.

Thanks.


Report •

#3
November 19, 2010 at 13:12:22
Well, I've come up with this:

----

@ECHO OFF
SETLOCAL EnableDelayedExpansion

FOR /f %%a IN ('DIR /b "Path:\to\pdfs\*.pdf"') DO (
	SET Var=%%a
	CALL :Sub1
)

GOTO :EOF

:Sub1

FOR /f "tokens=3 delims=_" %%A IN ('ECHO !Var!') DO (
	SET Var2=%%A
	CALL :Sub2
)

GOTO :EOF

:Sub2

FOR /f "tokens=*" %%1 IN ('DIR /ad /b "Path:\to\Folders"') DO (
	SET Folder=%%1
	SET Var3=!Folder:~0,4!
	IF "!Var2!"=="!Var3!" MOVE "!Var!" "Path:\t\Folders\!Folder!\!Var!"
)

GOTO :EOF

----

Sorry it's so ugly looking, but it should work.

Good Luck.

Can I put a question here?


Report •

Related Solutions

#4
November 20, 2010 at 09:33:35
Matt,
I have to study this script over this weekend. I'm sure it will work.

I did try the earlier script and it works too. If I do it that way, then I have to input in the 4 digits file name and my folders name for each line.

Thank you very much for sharing your knowledge. You're good!


Report •

#5
November 20, 2010 at 10:14:25
Well the first way would be the easiest way to accomplish it, but it requires a lot of input. I ad just assumed that you had only 3 folders, for which it would work perfectly.

The second should work perfectly no matter how many files and folders you have. I tested it with only a few, but that shouldn't matter.

Anyways, let me know how it works for you.

Can I put a question here?


Report •

#6
November 20, 2010 at 10:50:22
Hi again,
Yeah, the first method is a lot of input, but I can copy and paste the folder name and find an replace. But when it's in place its set. I'm dissecting the 2nd method and concerning about my weird folders:
Example:
All the files that I will receive will have 1 set for each account#:
RL_ENT_xxxx_1029_2010_3.pdf
RL_DIV_xxxx_1029_2010_3.csv
NU_ENT_xxxx_1029_2010_3.pdf
NU_ENT_xxxx_1029_2010_3.csv
where xxxx is the ID name. (about 100 different account# for each set each month, so approx 400 files total every month). The next month only the name 1029_2010_3 will change. Everything else stay the same.

My folders are more complicated (100 folders), examples:
10-088 St Joseph the Worker (74)
09-102 St Mary of the Wood (35)
.....

So if I have account# 2018 I will move the set of 4 files contains 2018 to the folder example: 10-088 St Joseph the Worker (74).
if account# 2020 I will move to 09-102 St Mary of the Woods (35)... and so on.

I tested your 1st method script and do 3 sets with 3 folders and it works fine. I think I will stay with this method.

Question: I didn't expect to understand all of the scripts. But could you explain to me this command?
FOR /f "tokens=3 delims=_"

I assume that tokens=3 is something has to do with the underscore _ in the file name? I'm bad at this.


Report •

#7
November 20, 2010 at 12:41:46
The FOR command is used to loop through files until they've all been checked.

Delims are what splits something into multiple "parts", and tokens are the "parts" that you want to take a look at.

So for example:

LR_INV_2018_1029_2010_3.pdf

The third token with and _ delimiter is 2018.

A small tutorial I wrote on this can be found (Click>>>) Here

Can I put a question here?


Report •

#8
November 20, 2010 at 13:34:55
Thank you. Thank you. Thank you.

I got it now. So, if my file doesn't have the underscore _ before the 2018, I can use delims= ", correct?
Example:
LR INV 2018_1029_2010_3.pdf
My script should be: FOR /f "tokens=3 delims= "

Do you think its easier for me to use something to search for 2018 in the file name and move to certain folder?


Report •

#9
November 20, 2010 at 14:13:28
The third token for that file would be "2018_1029_2010_3" because there isn't another space to separate it.

You can pipe to the find command and then check for an errorlevel.

FOR /f %%a IN ('DIR /b "P:\a\t\h\*.pdf"') DO (
ECHO %%a | FIND "2018"
IF "%ErrorLevel%"=="0" MOVE "%%a" "New Folder"
)

Or, are the file names all the same formats except some have spaces? Let me know, because you might be able to still use my above script.

Can I put a question here?


Report •

#10
November 20, 2010 at 15:59:58
Untested:
for /d %%d in (*) do for /f %%a in ("%%d") do move *%%a*.pdf "%%d"

How To Ask Questions The Smart Way


Report •

#11
November 20, 2010 at 16:36:50
Oh Razor... =)

Can I put a question here?


Report •

#12
November 20, 2010 at 17:05:58
My mind is spinning now. I like the 3rd method. I need to give you more details:
Each group (100 groups total) will receive 4 files in this format: (no space in it)
LR_INV_uuuu_mmdd_ccyy_q.pdf
LR_DET_uuuu_mmdd_ccyy_q.csv
UN_INV_uuuu_mmdd_ccyy_q.pdf
UN_INV_uuuu_mmdd_ccyy_q.csv

uuuu (group #), mmdd (month/date), ccyy (year), q(quarter)
So, each quarter I receive the 4 files in one group (400 files), just the mmdd, ccyy, q changes.
So, this 4 files will move to the folder depends on the uuuu.

I want to move them to the subfolders name uuuu.

Let's take the first 2 groups for example:
Group 1:
RL_VIN_2018_mmdd_ccyy_q.pdf
RL_TED_2018_mmdd_ccyy_q.csv
NU_VIN_2018_mmdd_ccyy_q.pdf
NU_TED_2018_mmdd_ccyy_q.csv
Move these files to:
W:\Dept Data\10-088 St Joseph the Worker (84)\2018

Group 2:
RL_VIN_2020_mmdd_ccyy_q.pdf
RL_TED_2020_mmdd_ccyy_q.csv
NU_VIN_2020_mmdd_ccyy_q.pdf
NU_TED_2020_mmdd_ccyy_q.csv
Move these files to:
W:\Dept Data\07-102 St Mary of the Wood (77)\2020

and so on....

Is it possible?


Report •

#13
November 20, 2010 at 22:19:16
Well, try razor's, see if it works. It would be a much simpler way that how I am doing it.

And make a few amends to my earlier script:

----

@ECHO OFF
SETLOCAL EnableDelayedExpansion

SET PDF=*Put the location of the PDFs here*
SET FLDR=*Put the location of the folders here*

FOR /f %%a IN ('DIR /b "!PDF!\*.pdf"') DO (
	SET Var=%%a
	CALL :Sub1
)

GOTO :EOF

:Sub1

FOR /f "tokens=3 delims=_" %%A IN ('ECHO !Var!') DO (
	SET Var2=%%A
	CALL :Sub2
)

GOTO :EOF

:Sub2

FOR /f "tokens=*" %%1 IN ('DIR /s /ad /b "!FLDR!"') DO (
	SET Folder=%%1
	IF "!Var2!"=="!Var3!" ECHO MOVE "!Var!" "!FLDR!\!Folder!\!Var!"
)

GOTO :EOF

That should work. Just added to it to include sub directories. Give it a test when you can, and let me know how it works. Nothing will actually be moved, it will just tell you what will be moved to where. Remove the "ECHO" on the 5th last line (before "MOVE") for it to actually move the files.

Can I put a question here?


Report •

#14
November 21, 2010 at 09:10:54
Razor,
Way too cool! You and Matt rock!
Your script works perfectly. Thank you for your time. I have a few more questions.

1. How do you move the files to the subfolder? Your script works fine if the folder is in the same location.
For example: my files is in c:\data\*2018*.* move to c:\data\st mary\2018\


Report •

#15
November 21, 2010 at 09:15:24
Sorry, I haven't finish my question yet, and somehow it posted.
2. Is it possible if it moves the files contains the same name to the subfolder same name that doesn't exist yet? Another word create that subfolder when it moves files.
For Example: c:\data\*2018*.* move to c:\data\st mary\2018 (create this subfolder)

Report •

#16
November 21, 2010 at 09:22:57
Matt,
I learned a lot from you. Your script on #9 post also works. But it also moves files that doesn't have 2018. I had 4 files there for testing:

batch3.bat
batch5.bat
RL_VIN_2018_1029_2010_3.pdf
NU_TED_2018_1029_2010_3.csv

It moved all 4 files to the new folder. Was it because in the batch3.bat and batch5.bat contains the word 2018 in there?

Thanks.


Report •

#17
November 21, 2010 at 10:32:21
1. How do you move the files to the subfolder? Your script works fine if the folder is in the same location.
I'd imagine something like this, but, depending on the directory tree, it might break in a horrible fashion:
for /d /r %%d in (????) do move *%%d*.pdf "%%d"

2. Is it possible if it moves the files contains the same name to the subfolder same name that doesn't exist yet? Another word create that subfolder when it moves files.
No. At least not in a single pass. For two reasons:
1. I didn't answer your question. The question you asked was, "For each file, how do I move it to the correct directory?" The question I answered was, "For each directory, how do I grab the corresponding files?" It's the same results, but the logic is easier to implement in batch.

2. There's no connection between the number "2018" and "st mary"

EDIT: Fixed script.
How To Ask Questions The Smart Way


Report •

#18
November 21, 2010 at 11:13:29
Well, I've become lost in my own script. Razor seems to know what he's doing here more than I do so, I'll back out of this one.

Good luck.

Can I put a question here?


Report •

#19
November 21, 2010 at 14:09:19
Matt, Thank you again for all your help. I'm still using your script to move files to assigned folders with individual commands in the batch file. I know it's a lot of work but I only have to write it once and it set.

Will you help for couple more questions? Not related to previous post.

1. I have one folder (new folder nothing in it). How can I distribute this folder to multiple folders?
Example: the folder name: test in w:\
I want to distribute this test folder to all 100 locations:
w:\st mary\
w:\st joseph\
w:\st charles\
...
...

2. How can I rename a folder (not empty) with the same name in different locations to something else?
From:
w:\st mary\data entry
w:\st joseph\data entry
w:\st charles\data entry
...
...
rename To:
w:\st mary\data entry bk
w:\st joseph\data entry bk
w:\st charles\data entry bk
...
...


Report •

#20
November 21, 2010 at 14:17:13
Matt123: Razor seems to know what he's doing here more than I do
=(
Hang around long enough, and you'll start to spot all of my tricks. For the record, this is how I'd rewrite your script:
@ECHO OFF
SETLOCAL EnableDelayedExpansion

SET PDF=*Put the location of the PDFs here*
SET FLDR=*Put the location of the folders here*

FOR /f "delims=" %%a IN ('DIR /b "!PDF!\*.pdf"') DO (
  FOR /f "tokens=3 delims=_" %%A IN ("%%a") DO (
    CALL :Sub2 "%%A" "%%~Fa"
  )
)

GOTO :EOF

%1 = The directory to find
%2 = The file to move
:Sub2

FOR /f "delims=" %%z IN ('DIR /s /ad /b "!FLDR!\%~1"') DO (
  ECHO MOVE %2 "%%z"
)

GOTO :EOF

EDIT: You might want to hang around; ttran seems more interested in your script.

How To Ask Questions The Smart Way


Report •

#21
November 21, 2010 at 14:28:15
Sorry, I didn't think through when I asked #2 question. There is no way to know if the folder isn't there with the same string.

For this script you gave me for question #1:
for /d /r %%d in (????) do move *%%a*.pdf "%%d"
I've tried but it didn't do anything. Is the question marks are for locations?
to wrap it up so you can understand my plan:
For all the files in w:\ if it has the same 4 digits
Move to the subfolder: w:\whatever\{4 digits}\

So...
if it finds 2018 in any files in w:\ it will move to w:\st mary\2018\
if it finds 2020 in any files in w:\ it will move to w:\st joseph\2020\
if it finds 2021 in any files in w:\ it will move to w:\st charles\2021\

Thank you so much!


Report •

#22
November 21, 2010 at 14:33:39
Matt,
You should listen to Razor. I do need you around here. What does it mean you back out of this one? Are you blocking all my posts? Sorry, I'm new here.

Report •

#23
November 21, 2010 at 14:36:43
I missed a %%a. Try
for /d /r %%d in (????) do move *%%d*.pdf "%%d"

There is no way to know if the folder isn't there with the same string.
After that line, you'll know what's left. From there, you should be able to gather what folders need to be made.

How To Ask Questions The Smart Way


Report •

#24
November 21, 2010 at 17:33:54
ok, I ran your script:
for /d /r %%d in (????) do move *%%d*.* "%%d" (I changed .pdf to .* so it includes other file format)

I got this error message:
F:\Parish Data\move *F:\Parish Data\10-088 St Michael - Remus (89)\CSA*.* "F:\Parish Data\10-088 St Michael - Remus (89)\CSA"
The filename, directory name, or volume label syntax is incorrect.

Before I run your script I had setup:
1 File: test.bat (with your script in it) in F:\Parish Data\
2 Files: RL_VIN_2018_1029_2010_3.pdf and UL_TED_2018_1029_2010_3.csv in F:\Parish Data\
1 Folder: 2018 in F:\Parish Data\10-088 St Michael - Remus (89)\
(This location has 2 folders 2018 and CSA)

What am I doing wrong? Thanks.


Report •

#25
November 21, 2010 at 17:48:39
ttran, sorry, I just meant I would let Razor do his thing and I would watch from the sidelines.

I don't quite understand your first question in post #19, what do you mean by distributing it multiple folders?
----
Razor,

Hang around long enough, and you'll start to spot all of my tricks.

Haha, I hope so. The way you rewrote it seems to make a little more sense. I'll keep that in mind for future reference, Thanks.

Can I put a question here?


Report •

#26
November 21, 2010 at 18:03:08
Matt, thanks for coming back :).
My post #19 question #1.
I would like to copy one folder to multiple subfolders:
I have a folder name: Test (empty folder) in F:\Parish Data\
I would like to copy this test folder to all the subfolders (about 100) in F:\Parish Data\
A few example of Subfolders:
F:\Parish Data\10-088 St Michael - Remus (89)\
F:\Parish Data\09-102 St Mary - Ionia (43)\
F:\Parish Data\04-021 St Charles - Greenville (25)\
...
...
So if I have a script, after I run it. I will have a test folder in each of the subfolders.

Report •

#27
November 21, 2010 at 20:03:48
If the folder is empty, and will always be empty, why can't we just create that directory in these folders?

F:\Parish Data\10-088 St Michael - Remus (89)\
Will "10-088 St Michael - Remus (89)" be the only folder in that directory?

Can I put a question here?


Report •

#28
November 21, 2010 at 20:12:50
Razor and Matt,
According to the Razor's rewrite of Matt's Script, could you advise me if I put in my info. the right way in the script?

@ECHO OFF
SETLOCAL EnableDelayedExpansion

SET ALL=*F:\Parish Data* (my folder that has all the files)
SET FLDR=*Put the location of the folders here*
(What do I need to put here? Since my move to folder is a subfolder:
F:\Parish Data\10-088 St Michael - Remus (89)\2018
F:\Parish Data\09-102 St Mary - Ionia (43)\2020
F:\Parish Data\04-021 St Charles - Greenville (25)\2021
...
...)


FOR /f "delims=" %%a IN ('DIR /b "!ALL!\*.*"') DO (
FOR /f "tokens=3 delims=_" %%A IN ("%%a") DO (
CALL :Sub2 "%%A" "%%~Fa"
)
)

GOTO :EOF

%1 = The directory to find (What do I need to do here?)
%2 = The file to move (What do I need to do here?)
:Sub2

FOR /f "delims=" %%z IN ('DIR /s /ad /b "!FLDR!\%~1"') DO (
ECHO MOVE %2 "%%z"
)

GOTO :EOF


Report •

#29
November 21, 2010 at 20:21:07
Matt, Maybe I didn't ask the question right.
I need to create a folder "test" in each of my subfolders so I can transfer files to that folder later in the future.
I have more than 100 subfolders. I don't want to go to each subfolder and create a new folder "test" (>100 times).
My subfolders are different, here is my first 3 (out of more than a 100) subfolders:
F:\Parish Data\10-088 St Michael - Remus (89)\
F:\Parish Data\09-102 St Mary - Ionia (43)\
F:\Parish Data\04-021 St Charles - Greenville (25)\

I need to somehow use a batch to put that "test" folder in all of my subfolders.


Report •

#30
November 23, 2010 at 17:51:37
Matt, do you have time to check on my question post #28 and #29?

Report •

#31
November 23, 2010 at 17:54:18
Will you have time to look at my question on post #28? How do I rewrite the script. Thanks.

Report •

#32
November 23, 2010 at 20:03:30
Sorry, I didn't notice that you had replied.

SET ALL=*F:\Parish Data* (my folder that has all the files)
SET FLDR=*Put the location of the folders here*
(What do I need to put here? Since my move to folder is a subfolder:

Change the asterisks to double quotes, sorry. Set FLDR to the location where the main directories are (F:\Parish Data)

%1 = The directory to find (What do I need to do here?)
%2 = The file to move (What do I need to do here?)

This was just a note stuck in by Razor, ignore it as the script will skip right over top of it.
----

For your other question... I'm not experienced enough apparently to help you with that. I can't for the life of me figure out a way. I'll ponder it for a while, but I'm sure somebody else can help (@Razor),Sorry.

Can I put a question here?


Report •

#33
November 23, 2010 at 20:26:59
Thanks Matt. So this is what I need right?

@ECHO OFF
SETLOCAL EnableDelayedExpansion

SET ALL="F:\Parish Data\"
SET FLDR="F:\Parish Data\"

FOR /f "delims=" %%a IN ('DIR /b "!ALL!\*.*"') DO (
FOR /f "tokens=3 delims=_" %%A IN ("%%a") DO (
CALL :Sub2 "%%A" "%%~Fa"
)
)

GOTO :EOF

%1 = The directory to find
%2 = The file to move
:Sub2

FOR /f "delims=" %%z IN ('DIR /s /ad /b "!FLDR!\%~1"') DO (
ECHO MOVE %2 "%%z"
)

GOTO :EOF

Thank you. I will wait until someone has the answer for my #29 post.


Report •

#34
November 24, 2010 at 04:38:31
SET ALL="F:\Parish Data\"
SET FLDR="F:\Parish Data\"

Remove the to final backslashes. The script puts them in when it uses that variables, so if you include it there your path will be 'F:\Parish Data\\".

Can I put a question here?


Report •

#35
November 24, 2010 at 20:32:27
Matt, what am I doing worng here? I put 2 files under F:\Parish Data\
LR_INV_2018_1029_2010.pdf and UL_NED_2018_1029_2010.csv.
I created a folder 2018 in F:\Parish Data\09-088 St Mary\
Ran the script. I put a pause in there to see what happen. I even remove the echo before move at the end.
Got the message the system can't find the files specified. Please help!

@ECHO OFF
SETLOCAL EnableDelayedExpansion

SET ALL="F:\Parish Data"
SET FLDR="F:\Parish Data"

FOR /f "delims=" %%a IN ('DIR /b "!ALL!\*.*"') DO (
FOR /f "tokens=3 delims=_" %%A IN ("%%a") DO (
CALL :Sub2 "%%A" "%%~Fa"
)
)

GOTO :EOF

%1 = The directory to find
%2 = The file to move
:Sub2

FOR /f "delims=" %%z IN ('DIR /s /ad /b "!FLDR!\%~1"') DO (
ECHO MOVE %2 "%%z"
)

GOTO :EOF


Report •


Ask Question