Solved

Batch File to Move Files By date

Posted on 2014-10-29
12
246 Views
Last Modified: 2014-12-26
Below is an example of the script that I am currently using but would like to expand a little.  First I would like to just list all the directories that need it to run in instead of having to repeat the entire script each time.  I have about 30 different directories to maintain.  Also I am looking to find a way to leave out the last months worth of files because they are required to be left in the source directory.  I would also like this to not include zip and rar files in the moving process..


REM \\NAS_01\EDI\204\Customer_A\Archive

      set "source= \\NAS_01\EDI\204\Customer_A\Archive"
      for %%F in ("%source%\*") do (
        for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do (
            if not exist "%source%\%%C" mkdir "%source%\%%C"
            move "%%~fF" "%source%\%%C"
      echo "%%~fF"
      ))
REM C:\EDI\204\Customer_B\Archive

      set "source= C:\EDI\204\Customer_B\Archive"
      for %%F in ("%source%\*") do (
        for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do (
            if not exist "%source%\%%C" mkdir "%source%\%%C"
            move "%%~fF" "%source%\%%C"
      echo "%%~fF"
      ))
0
Comment
Question by:oddessy
  • 5
  • 3
  • 2
12 Comments
 
LVL 26

Expert Comment

by:pony10us
ID: 40410835
What is the difference between:

set "source= \\NAS_01\EDI\204\Customer_A\Archive"

and

set "source= C:\EDI\204\Customer_B\Archive"

Are the source locations on two different machines?

If they the Customer_* folders are all in the same source are there any other folders in that folder?  

What is the significance of the "204"? Will it change?
0
 

Author Comment

by:oddessy
ID: 40410906
The difference between the following two is that they are in different paths local and on a remote machine.

set "source= \\NAS_01\EDI\204\Customer_A\Archive"
and
set "source= C:\EDI\204\Customer_B\Archive"

The 204 is just the path and does not have a significance, yes it does change.
0
 
LVL 26

Expert Comment

by:pony10us
ID: 40411175
So there are different sources that can change.  

What about the destination folder?

if not exist "%source%\%%C" mkdir "%source%\%%C"

Where is %%C defined?

You are placing the moved files on the same system as the source in both cases?
(NAS_01 and C respectively)
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:oddessy
ID: 40411356
The destination is a subfolder of the source that is the 4 digit year which represents the date modified or created I am not sure now.

%%C is not defined but it work to create a folder with the year.  Moved files are almost always in the same directory as the source unless I use the bellow code..

Echo off
      set "source=E:\EDI\EDI204\CustomerA\Archive\Archive 2012-12-11"
      set "dest=E:\EDI\EDI204\CustomerA\Archive"
      for %%F in ("%source%\*") do (
        for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do (
            if not exist "%dest%\%%C" mkdir "%dest%\%%C"
            move "%%~fF" "%dest%\%%C"
      echo "%%~fF"
      ))
0
 
LVL 26

Expert Comment

by:pony10us
ID: 40411796
***   THIS IS UNTESTED  ***

I have commented out the actual move and added an echo line to tell you what it would have done for testing purposes.

Here is what I have come up with. I am sure that someone else could do a better job or perhaps suggest a better method such as VBS or Robocopy.


REM ***********************************
REM * Assign the source folders to a variable *
REM ***********************************

set source[1]=\\NAS_01\EDI\204\Customer_A\Archive
set source[2]=C:\EDI\204\Customer_B\Archive

REM *** For each source variable repeat incrementing the number ***

for /L %%i in (*%start%,1,%end%) do (

            for %%F in ("%L%\*") do (
                 for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do (
                      if not exist "%L%\%%C" mkdir "%L%\%%C"

	       REM *** hide .zip and .rar files so they don't get moved ***
	       ATTRIB +H *.zip
                           ATTRIB +H *.rar

 REM                 move "%%~fF" "%L%\%%C"
                           echo I would have moved "%%~fF" to "%L%\%%C" 

REM *** if satisfied then uncomment the "move" line and comment out the "echo" line above ***

            echo "%%~fF"

                          REM *** un-hide .zip and .rar files ***
                          ATTRIB -H *.zip
                          ATTRIB -H *.rar
       )
   )
)     

Open in new window


I did not address the issue of leaving some files based on date. Do you want to leave the last 30 days or only the current month?
0
 

Author Comment

by:oddessy
ID: 40411825
When I tried the above code it wants to move the files on my C;\ root directory not the source directory..
0
 
LVL 26

Expert Comment

by:pony10us
ID: 40412184
I see some issues. I need to play and see what I can come up with.  I have to leave for the day though.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40439679
I'll take a different approach. First, taking your original script, it is most simple to just apply a small change to allow repeated execution for different paths:
@echo off
setlocal EnableDelayedExpansion
for %%a in (
  \\NAS_01\EDI\204\Customer_A\Archive
  C:\EDI\204\Customer_B\Archive
 "C:\Path with Spaces\and (parens)"
) do call :movefiles "%%~a"
exit /b

:movefiles
set source=%1
for %%F in ("%source%\*") do (
  for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do set year=%%C
  if not exist "%source%\!year!" mkdir "%source%\!year!"
  echo %%~xF | findstr /v /L /i ".ZIP .RAR" >nul && echo move "%%~fF" "%source%\!year!" && echo %%~fF
)

Open in new window

That will leave out ZIP and RAR files. The MOVE is echoed only for testing.
The 30-days-stuff is hard to do in a batch. VBS or PowerShell are better suited for that. The mentioned RoboCopy isn't suited well either, as we would have to call it on single files because of the year subfolder. If that can be dropped, and just using the actual year, it's a cinch.
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40439722
This is the PowerShell way, including the 30-days restriction.
$recent = (get-date).AddDays(-30)

foreach ($source in
   \\NAS_01\EDI\204\Customer_A\Archive,
   C:\EDI\204\Customer_B\Archive,
  "C:\Path with Spaces\and (parens)"
) {
  dir $source\* -exclude *.zip, *.rar | 
    ? { $_.LastWriteTime -lt $recent } |
    % {
      $dest = "$source\$($_.LastWriteTime.Year)"
      if (!(Test-Path $dest -PathType Container)) { mkdir $dest }
      $_ | move-item -Destination $dest -whatIf
    }
}

Open in new window

Again, test mode - remove -whatIf if you want to apply the magic.
0
 
LVL 26

Expert Comment

by:pony10us
ID: 40440218
Thanks Qlemo, I got called away on a family situation and just got back today.  The powershell method looks very good.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now