0m3r
0m3r

Reputation: 12495

Move files to folders based on file name

I am new to batch files although have searched thoroughly and found topics with a similar BUT not covering what I need.

I work with lots of documents (.pdf, .doc, .xls ) saved in C:\Tempfolder. once I am done editing I save the file name with 9 digit numbers e.g( 305123123.pdf or 306123123.pdf or .doc )

I am looking to create a batch file which will automate move the files that start with 305 to C:\Users\Omer\Documents\aaCompany or if 306 to C:\Users\Omer\Documents\bbCompany

I can have upwards of 200 files in the folder at any one time when I decide to process.

I am also curious if the batch file can monitor C:\Tempfolder and move the files 305 or 306 without executing it

Help on this is greatly appreciated.
I hope I have provided sufficient information to see if this is feasible.

Upvotes: 0

Views: 386

Answers (1)

SachaDee
SachaDee

Reputation: 9545

You can make something like this :

@Echo off &cls

::The Input Folder
set $Dossier="C:\Tempfolder"

::The Output Folders
set $Out305="C:\Users\Omer\Documents\aaCompany"
set $Out306="C:\Users\Omer\Documents\bbCompany"

::The extensions to wait
set "$Format=*.pdf,*.xls,*.doc"

setlocal enabledelayedexpansion
:Boucle
cls&echo Waiting for file ...
for /f %%a in ('dir /b/a-d %$Dossier%\%$Format% 2^>nul') do (
 set "$Fichier=%%a"
 echo Treating -^> %%a
 if "!$Fichier:~0,3!"=="305" move "%%~nxa" %$Out305%
 if "!$Fichier:~0,3!"=="306" move "%%~nxa" %$Out306%
)

::Waiting ~5 secondes
ping localhost -n 6 >nul

::Return to the loop
goto:Boucle

Upvotes: 2

Related Questions