Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Batch File to Move Files By date

Posted on 2014-10-29
12
Medium Priority
?
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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 71

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 71

Accepted Solution

by:
Qlemo earned 2000 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

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

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

Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Simple Linear Regression
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

721 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