Solved

Batch File to Move Files By date

Posted on 2014-10-29
12
223 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:oddessy
Comment Utility
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
Comment Utility
***   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
Do email signature updates give you a headache?

Do you feel like you are constantly making changes to email signatures? Are the images not formatting how you want them to? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today.

 

Author Comment

by:oddessy
Comment Utility
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
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
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 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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
Comment Utility
Thanks Qlemo, I got called away on a family situation and just got back today.  The powershell method looks very good.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Our Group Policy work started with Small Business Server in 2000. Microsoft gave us an excellent OU and GPO model in subsequent SBS editions that utilized WMI filters, OU linking, and VBS scripts. These are some of experiences plus our spending a lo…
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

762 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

9 Experts available now in Live!

Get 1:1 Help Now