2

I have a requirement to create a batch file and that should work in windows 2003 server.

  1. I have source folder "c:\Source" and destination folder "c:\Destination".
  2. I want to check all the file modified dates in source folder:

    • If the file modified date is less than current date then that file will move into destination folder
    • Otherwise, do nothing.

Note: as my server is production server so, I am unable to install Resource kit and robocopy.exe. The only way is write the batch script.

Robocopy and forfiles are not working in Windows2003 server.

Jason Faulkner
  • 6,378
  • 2
  • 28
  • 33
Kranthi
  • 23
  • 1
  • 4
  • is PowerShell installed on your server? It will much simpler to do that in PowerShell – Nazar554 Dec 30 '14 at 18:42
  • You don't have to "install" the Resource Kit, you can just copy RoboCopy.exe to the server and start using it immediately. – Jason Faulkner Dec 30 '14 at 19:05
  • @Kranthi Welcome to Stack Overflow! If my answer below was helpful, please consider marking it as accepted. [See this page](http://meta.stackexchange.com/q/5234/275822) for an explanation of why this is important. – rojo Dec 31 '14 at 01:37

1 Answers1

2

Update

Since you have forfiles on your server, this is easy. To check for files older than 1 day old, just use forfiles /D -1. For files over 2 days old, /D -2.

forfiles /D -2 /M *.log /P C:\Source /C "cmd /c move @file c:\Destination"

Enter forfiles /? in a console window for full syntax.


Original answer

Consider revising your requirement. Instead of saying "If file modified date is less than the current date", you should say, "If file modified date does not equal current date". That absolves you from having to do date math, and makes your task profoundly simpler. After all, the last modified date is not going to be a date in the future, right?

After that, it's a simple matter of scraping today's date from %date% and comparing it with the date from each file's %%~tX substitution property in a for loop. Type help for in a console window and see the last two pages for more information about this syntax.

I don't think there will be any locale date formatting issues with this. As long as your system's %date% variable is in the format of dayOfWeek Date and %%~tX is formatted as Date Time etc. then this script should work regardless of whether you handle dates locally as MM/DD/YYYY or YYYY/MM/DD or DD/MM/YYYY or something else. I hope.

@echo off
setlocal enableextensions

set "source=c:\Source"
set "destination=c:\Destination"

:: store today's date in %today%
for /f "tokens=2" %%I in ('echo %date%') do set "today=%%I"

for %%I in ("%source%\*") do (
    rem :: scrape MM/DD/YYYY from %%~tI
    for /f %%a in ('echo %%~tI') do (

        rem :: compare the two dates
        if "%%a" neq "%today%" (
            echo %%~nxI: %%a does not equal %today%.  Moving.
            >NUL move /y "%%~fI" "%destination%"
        ) else (
            echo %%~nxI: %%a equals %today%.  Skipping.
        )
    )
)
Community
  • 1
  • 1
rojo
  • 24,000
  • 5
  • 55
  • 101
  • Hi Rojo, Thank you very much for your help,LSS is not working, if i want to move files older than 2 days. – Kranthi Jan 05 '15 at 16:20
  • That's a whole other issue. Really, you'll save yourself a lot of hassle if you just copy `c:\windows\system32\forfiles.exe` onto your 2K3 server. The batch language is horrible for date math. You might be able to employ a JScript solution, [something like this](http://stackoverflow.com/a/27297874/1683264) perhaps but with `WSH.Echo(Math.floor(age / 1000 / 60 / 60 / 24));` to get the age in days. – rojo Jan 05 '15 at 16:48
  • Hi Rojo, Yes, i have "Forfiles.exe" in c:\windows\System32 folder, can we do more on this? – Kranthi Jan 06 '15 at 10:52
  • Hi Rojo, I have tried this command and it working as expectd. "forfiles /P "d:\source" /S /M *.* /D -2 /C "cmd /C move @file "d:\dest"". Thanks a lot for your inputs. – Kranthi Jan 06 '15 at 13:12
  • @Kranthi Cool. I was updating my answer as you were doing your own testing it seems. :) – rojo Jan 06 '15 at 13:16