Batch moving files to hundreds of different folders

January 17, 2018 at 06:58:26
Specs: Windows 7
I am trying to move hundred of jpegs based on their name (UT-000000, UT-000001...) to the corresponding folder (named UT-00000, etc.) and so on.

Any ideas on how to accomplish this?


See More: Batch moving files to hundreds of different folders

Report •

#1
January 17, 2018 at 08:14:55
yes, but please tell us what the source-folder&destination-folder are called
also are there any files in the source directory(folder) which are supposed to be NOT copied?
btw i will not respond in the next ~18 hours

i5-6600K@4.670GHz/4.003GHz cache@1.33v | 2x4GB Crucial-DDR4-2133@ 14-14-14-28 1T 2668MHz@1.3v | ASUS Z170-K | Samsung 250GB SSD 850 EVO | MSI RX 570 4GB@1380cc&2087mc bios-powertune-mod | Corsair VS45

message edited by hidde663


Report •

#2
January 17, 2018 at 08:28:15
The source folder will be:
Z:\DOCS\PROJECTS\10195.00 VDOT HR BSI\10195.06 LOA 6 HR 133 OH Sign Base Insp\Photos\001-UT Reports

and the destination folder will be:
Z:\DOCS\PROJECTS\10195.00 VDOT HR BSI\10195.06 LOA 6 HR 133 OH Sign Base Insp\Photos\064XXX1

Where the last 7 characters are the same as the name of the jpeg (with UT as the suffix).

The source folder will only contain jpegs to be copied.


Report •

#3
January 18, 2018 at 01:31:30
This works for me:

right now it'll copy the filed witout deleting the files in the original folder.
if you do want to remove the original files in the source folder the replace "copy" with "move" in the last line of the .bat

::----start of test.bat----
@echo off
set "source=Z:\DOCS\PROJECTS\10195.00 VDOT HR BSI\10195.06 LOA 6 HR 133 OH Sign Base Insp\Photos\001-UT Reports"
set "destination=Z:\DOCS\PROJECTS\10195.00 VDOT HR BSI\10195.06 LOA 6 HR 133 OH Sign Base Insp\Photos\064XXX1"
for /f %%a in ('dir/b/ONG ^| findstr ".jp"') do copy "%source%\%%a" "%destination%\%%a"
::----end of test.bat----

i5-6600K@4.670GHz/4.003GHz cache@1.33v | 2x4GB Crucial-DDR4-2133@ 14-14-14-28 1T 2668MHz@1.3v | ASUS Z170-K | Samsung 250GB SSD 850 EVO | MSI RX 570 4GB@1380cc&2087mc bios-powertune-mod | Corsair VS45

message edited by hidde663


Report •

Related Solutions

#4
January 18, 2018 at 06:44:58
Thanks again for your help but this isn't working for me.

The destination folder needs to change for each jpeg.

UT060216LF, UT060216LR, UT060216RR, and UT060216RF all need to move to folder 060216.
UT060217LF, UT060217LR, UT060217RR, and UT060217RF all need to move to folder 060217. And so on...

Does that make sense?


Report •

#5
January 18, 2018 at 07:44:18
yes my bad, i tend to overlook some details, give me a sec

i5-6600K@4.670GHz/4.336GHz cache@1.33v
2x4GB Crucial-DDR4-2133@ 14-14-14-28 1T 2899MHz@1.4v
ASUS Z170K
Samsung 250GB SSD 850 EVO
MSI Armor RX 570 4GB@1380c/2087m bios-mod
Corsair VS450


Report •

#6
January 18, 2018 at 07:54:38
Let me know ohw this one does:


@echo off&setlocal enabledelayedexpansion
set "source=Z:\DOCS\PROJECTS\10195.00 VDOT HR BSI\10195.06 LOA 6 HR 133 OH Sign Base Insp\Photos\001-UT Reports"
for /f %%a in ('dir/b/ONG ^| findstr ".jp"') do (
set "destination=%%a"
set destination=!destination:~-8!
set destination=!destination:~0,6!
move "%source%\%%a" "Z:\DOCS\PROJECTS\10195.00 VDOT HR BSI\10195.06 LOA 6 HR 133 OH Sign Base Insp\Photos\!destination!\%%a"
)

i5-6600K@4.670GHz/4.336GHz cache@1.33v
2x4GB Crucial-DDR4-2133@ 14-14-14-28 1T 2899MHz@1.4v
ASUS Z170K
Samsung 250GB SSD 850 EVO
MSI Armor RX 570 4GB@1380c/2087m bios-mod
Corsair VS450

message edited by hidde663


Report •

#7
January 18, 2018 at 08:21:17
Still not copying over. I'm saving as cmd.bat with ansi encoding and running it. Is that correct?

Could it be the file names? The convention is UTXXXXXXLF (or LR, RR, RF) and the folder name is XXXXXXX


Report •

#8
January 18, 2018 at 08:30:45
ansi is fine

weird, because all its doing is look for a file with.jg in the name,
then extract the filename (example:UT060216LR) from %%a & put it in variable %destination%
then remove the first 2 from %destination%
then remove the last 2 from %destination%
move it to sourcefolder\%destination% with name %%a (which refreshes every loop, walking through the enite source folder)

the cmd.bat file should be placed into:
Z:\DOCS\PROJECTS\10195.00 VDOT HR BSI\10195.06 LOA 6 HR 133 OH Sign Base Insp\Photos\001-UT Reports

& run from there(running as admin might help too)

anyways, i'll try to look into it later tonight, but probably not till tommorow or later.

maby some other users can help you in the meantime

i5-6600K@4.670GHz/4.336GHz cache@1.33v
2x4GB Crucial-DDR4-2133@14-14-14-28 1T 2899MHz@1.4v
ASUS Z170K
Samsung 250GB SSD 850 EVO
MSI Armor RX 570 4GB@1380c/2087m bios-mod
Corsair VS450


Report •

#9
January 18, 2018 at 08:39:02
I moved the cmd.bat to the correct folder and it ran but said,

"The system cannot find the path specified. 0 file(s) moved."


Report •

#10
January 18, 2018 at 22:01:36
@hid: did you fix this?
set "source=Z:\DOCS\PROJECTS\10195.00 VDOT HR BSI\10195.06 LOA 6 HR 133 OH Sign Base Insp\Photos\001-UT Reports"

set source="Z:\DOCS\PROJECTS\10195.00 VDOT HR BSI\10195.06 LOA 6 HR 133 OH Sign Base Insp\Photos\001-UT Reports"


Report •

#11
January 19, 2018 at 06:53:55
I changed the file paths just to run tests.

@echo off&setlocal enabledelayedexpansion
set "source=C:\Users\bkamrath\Desktop\BAT Test Folder\001-UT Reports\Renamed"
for /f %%a in ('dir/b/ONG ^| findstr ".jp"') do (
set "destination=%%a"
set destination=!destination:~-8!
set destination=!destination:~0,6!
move "%source%\%%a" "C:\Users\bkamrath\Desktop\BAT Test Folder\!destination!\%%a"
)
pause

With this I get "The system cannot find the path specified."

If I run it without the \!destination!\

move "%source%\%%a" "C:\Users\bkamrath\Desktop\BAT Test Folder\%%a"
)
pause

It successfully moves all the files to the root folder (BAT Test Folder). So something isn't right in finding the specific folder within the BAT Test folder.


Report •

#12
January 19, 2018 at 21:48:43
apologies for butting-in again, but maybe the directories need to created first:
(also: trying to abbreviate the lengthy pathname:)
set base=Z:\DOCS\PROJECTS\10195.00 VDOT HR BSI\10195.06 LOA 6 HR 133 OH Sign Base Insp\Photos"
set source=%base%\001-UT Reports

for /f %%a in ('dir/b/ONG ^| findstr ".jp"') do (
	set "destination=%%a"
	set destination=!destination:~-8!
	set destination=!destination:~0,6!

        if not exist "%base%\!destination!" md "%base%\!destination!"

        move "%source%\%%a" "%base%\!destination!\%%a"
)

message edited by nbrane


Report •

#13
January 22, 2018 at 13:19:18
No apologies needed! I appreciate the help. I think we are getting close...

I ran this,

set base=C:\Users\bkamrath\Desktop\BAT Test Folder
set source=%base%\001-UT Reports\Renamed

for /f %%a in ('dir/b/ONG ^| findstr ".jp"') do (
set "destination=%%a"
set destination=!destination:~-8!
set destination=!destination:~0,6!

move "%source%\%%a" "%base%\!destination!\%%a"
)
pause

And the results are,

C:\Users\bkamrath\Desktop\BAT Test Folder\001-UT Reports\Renamed>(
set "destination=UT0640217RF.jpg"
set destination=!destination:~-8!
set destination=!destination:~0,6!
move "C:\Users\bkamrath\Desktop\BAT Test Folder\001-UT Reports\Renamed\UT064021
7RF.jpg" "C:\Users\bkamrath\Desktop\BAT Test Folder\!destination!\UT0640217RF.jp
g"
)
The system cannot find the path specified.
0 file(s) moved.

So the !destination! isn't being changed to match the &&a value. The directories are all present in the base folder.


Report •

#14
January 22, 2018 at 18:09:39
You omitted this line, which is important:

if not exist "%base%\!destination!" md "%base%\!destination!"

(see post #12)

if the directory does not exist, then "the system cannot find the path specified".

To try and clear things up, here's the basics from your last post with the fix installed and a couple of trivial modifications and some debugging advisories:

set base=C:\Users\bkamrath\Desktop\BAT Test Folder
set source=%base%\001-UT Reports\Renamed
echo moving *.jpg from %source%\*.jpg
for /f %%a in ('dir /b "%source%\*.jpg') do (
set "destination=%%~na"
set destination=!destination:~2,-2!

if not exist "%base%\!destination!" md "%base%\!%destination!"
echo move "%source%\%%a" TO "%base%\!destination!\%%a"
move "%source%\%%a" "%base%\!destination!\%%a"
)
pause
;:--- end recap
other options are to make a log file, and to increase debugging displays and pauses, in order to find the problem.

message edited by nbrane


Report •

#15
January 23, 2018 at 05:58:10
It still is just creating a folder named !destination! in the BAT test folder. Thanks for your help guys, I do appreciate it!

Report •

Ask Question