Solved Move files to different folders based on the content

Microsoft Windows 7 professional - 64-bi...
November 28, 2016 at 02:16:31
Specs: Windows 7, Core i5/4GB
Dear Experts

my scenario is as follows...

I have one source folder "Source" and 2 target folders "Target_123456" & "Target_789101". I get multiple tab delimited .dat files in Source folder. I am trying to get a batch script which can read the first line of each file and move the file to its relevant target folder based on the number in the first line. Assume If I receive a file Test.dat in source folder and in its first line if 789101 number is mentioned then this file needs to be moved to Target_789101 folder with same file name Test.dat.

Please find below samples of the files...

123456 Series files:-
File 1:

VOL1000001 123456 1
HDR1A123456S 112345600000100010001 16194 162000000000
HDR2F0200000100 00

File 2:

PWRD001123456 123456 00
VOL1000001 123456 1
HDR1A123456S 112345600000100010001 16295 162960000000
HDR2F0200000100

File3:

PWRD002123456 123456 00
VOL1000002 123456 1
HDR1A S 112345600000200010001 16294 162960000000
HDR2F0200000100

789101 Series files:-

File 1:

VOL1000001 789101 1
HDR1A789101S 178910100000100010001 16194 162000000000
HDR2F0200000100 00

File2:

PWRD001789101 789101 00
VOL1000001 789101 1
HDR1A789101S 178910100000100010001 16295 162960000000
HDR2F0200000100

File 3:

PWRD002789101 789101 00
VOL1000002 789101 1
HDR1A S 178910100000200010001 16294 162960000000
HDR2F0200000100

Thanks,
Phani Akella


See More: Move files to different folders based on the content

Reply ↓  Report •


#1
November 28, 2016 at 03:32:59
✔ Best Answer
::====== script starts here ===============
::
:: phani.bat 2016-11-28 18:25:54.75
@echo off & setLocal enableDELAYedeXpansioN

dir/b *.dat > myfile

:main
for /f "tokens=* delims= " %%a in (myfile) do (
find "123456" < %%a > nul
if not errorlevel 1 echo move %%a Target_123456
find "789101" < %%a > nul
if not errorlevel 1 echo move %%a Target_789101
)
::====== script ends here =================

=====================

M2 Get custom script or take private lessons


Reply ↓  Report •

#2
November 30, 2016 at 04:29:15
Hi

Thanks for the response...I tried this batch and I am not able to move the files. My requirement is to read first line of all the files in Source folder and move them to different folders based on the value in the first line.

About batch has created myfile file with list of filenames in the directory but did not moved the files to target location.

::====== script starts here ===============
::
:: phani.bat 2016-11-28 18:25:54.75
@echo off & setLocal enableDELAYedeXpansioN
dir/b *.dat > myfile
:main
for /f "tokens=* delims= " %%a in (myfile) do (
find "123456" < %%a > nul
if not errorlevel 1 echo move %%a E:\RnD\Target_123456
find "789101" < %%a > nul
if not errorlevel 1 echo move %%a E:\RnD\Target_789101
)
::====== script ends here =================
=====================

Thanks,
Phani Akella


Reply ↓  Report •

#3
November 30, 2016 at 05:57:12
I put in echo for safety.
To activate the script edit out the echo in front of move so it looks like this:

if not errorlevel 1 move %%a E:\RnD\Target_123456

=====================

M2 Get custom script or take private lessons


Reply ↓  Report •

Related Solutions

#4
November 30, 2016 at 14:13:06
Awesome...thanks a ton...this working fine after removing echo.

Thanks,
Phani Akella


Reply ↓  Report •

#5
November 30, 2016 at 14:16:08
Can I write a log file using this script...like file xyz moved to folder abc on ddmmyyhhmmss.

Thanks,
Phani Akella


Reply ↓  Report •

#6
December 1, 2016 at 03:08:05
Probably messy at best. Ill think about it.

=====================

M2 Get custom script or take private lessons


Reply ↓  Report •

Ask Question