Link to home
Start Free TrialLog in
Avatar of indacrypt
indacrypt

asked on

Batch script for win2000 that moves files

Hello,
I have never written any batch files before but I need to move files which are older than 2 days from one directory to another on a windows 2000 server. Can anyone help me out with that?
THanks,
Dee
Avatar of sirbounty
sirbounty
Flag of United States of America image

This should do it - if you're looking for a vbscript method...


Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objOutputFile:Set objOutputFile = objFSO.CreateTextFile("C:\FilesMoved.log")
Dim dtOld
strSourceFolder = "C:\Testing\"
strTargetFolder = "C:\TargetFolder\"
With objOutputFile
  .WriteLine "==========================="
  .WriteLine "Relocation summary for " & Date
  .WriteLine "=-=-=-=-=-=-=-=-=-=-=-=-=-="
  .WriteLine
End With

'intDel is the number of days old you want to check for.
intDel = 2
dtOld = DateAdd("d", -intDel, Date)

ProcessFolder objFSO.GetFolder(strSourceFolder)

With objOutputFile
  .WriteLine
  .WriteLine "Process completed at " & Now
  .WriteLine "==========================="
  .Close
End With

Set objOutputFile = Nothing
Set objFSO = Nothing
wscript.quit

Sub ProcessFolder(strSource)
  ProcessFiles strSource
  For Each fld In strSource.SubFolders
    ProcessFolder fld
  Next
End Sub

Sub ProcessFiles(strSrc)
  For Each file In strSrc.Files
    objOutputFile.WriteLine file.Path & " was relocated at " & Now
    objFSO.MoveFile file.Path, strTargetFolder
  End If
  Next
End Sub
Avatar of indacrypt
indacrypt

ASKER

umm no...just using windows commands... thanks very much though
@echo off

setlocal

REM ** Set debug= to activate code
set debug=echo

if "%~2"=="" echo Usage: %0 sourceDirectory destinationDirectory&goto :EOF
if not exist "%~1" echo %~1 does not exist&goto :EOF
if not exist "%~2" echo %~2 does not exist&goto :EOF

set srcDir=%~1
set destDir=%~2

REM ** This makes sure that the trailing backslash if specified is removed
REM ** I don't believe that works in 2000, so you'll have to make sure
REM ** that you don't specify a trailing backslash for the destination
REM if "%destDir:~-1%"=="\" set destDir=%destDir:~0,-1%

call :GETDATEPARTS "%date%"

call :SUBTRACTDAYS 2

set cutOff=%yy%%mm%%dd%

pushd "%srcDir%"

for /f "tokens=*" %%a in ('dir /b /a-d "%srcDir%" 2^>NUL') do call :PROCESS "%%a" %%~ta

popd

goto :EOF

:PROCESS

call :GETDATEPARTS %~2

%debug% if /i "%yy%%mm%%dd%" LSS "%cutOff%" move "%~1" "%destDir%\%~1"

goto :EOF

:GETDATEPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF

:SUBTRACTDAYS

set dayCnt=%1

if "%dayCnt%"=="" set dayCnt=1

REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100

:CHKDAY

if /I %dd% GTR 0 goto DONESUBTRACT

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yy=%yy% - 1

:ADJUSTDAY

if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through

:SET31

set /A dd=31 + %dd%

goto CHKDAY

:SET30

set /A dd=30 + %dd%

goto CHKDAY

:LEAPCHK

set /A tt=%yy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY

:DONESUBTRACT

if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

goto :EOF

Good Luck,
Steve
oh wow....didnt realise the script would be so long!! Ill try it ...ill have to give it two days to try it coz i manually psuhed the files over today. Will let you know.
Thanks!!
i realised I dont have the expertise to modify that script so IM not going to be using either of these. Thanks!
Don't have to modify mine all you have to do is run it with the parameters you specify. Run it without parameters to get the usage.

Hi Steve...what parameters would I be using..do you mean i just rename srcDir and destDir?
If you named the batch file MyMoveFile.bat you'd say:

MyMoveFile "c:\temp" "d:\temp"
Here's the version that actually moves files:

@echo off

setlocal

REM ** Set debug= to activate code
set debug=

if "%~2"=="" echo Usage: %0 sourceDirectory destinationDirectory&goto :EOF
if not exist "%~1" echo %~1 does not exist&goto :EOF
if not exist "%~2" echo %~2 does not exist&goto :EOF

set srcDir=%~1
set destDir=%~2

REM ** This makes sure that the trailing backslash if specified is removed
REM ** I don't believe that works in 2000, so you'll have to make sure
REM ** that you don't specify a trailing backslash for the destination
REM if "%destDir:~-1%"=="\" set destDir=%destDir:~0,-1%

call :GETDATEPARTS "%date%"

call :SUBTRACTDAYS 2

set cutOff=%yy%%mm%%dd%

pushd "%srcDir%"

for /f "tokens=*" %%a in ('dir /b /a-d "%srcDir%" 2^>NUL') do call :PROCESS "%%a" %%~ta

popd

goto :EOF

:PROCESS

call :GETDATEPARTS %~2

%debug% if /i "%yy%%mm%%dd%" LSS "%cutOff%" move "%~1" "%destDir%\%~1"

goto :EOF

:GETDATEPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF

:SUBTRACTDAYS

set dayCnt=%1

if "%dayCnt%"=="" set dayCnt=1

REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100

:CHKDAY

if /I %dd% GTR 0 goto DONESUBTRACT

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yy=%yy% - 1

:ADJUSTDAY

if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through

:SET31

set /A dd=31 + %dd%

goto CHKDAY

:SET30

set /A dd=30 + %dd%

goto CHKDAY

:LEAPCHK

set /A tt=%yy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY

:DONESUBTRACT

if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

goto :EOF
when i try saying movelogs "D:\<path here>" "w:\" (w is a mapped network drive, no password required), it gives me an error saying the path on the D drive is invalid..!
What exactly does it say?

Does it say:

d:\yourpath does not exist

Or some other message?

Can you reveal the path so that I can check it out?

Here's a version with debug information. Please report back what it says when you run this code:

@echo off

setlocal

REM ** Set debug= to activate code
set debug=

if "%~2"=="" echo Usage: %0 sourceDirectory destinationDirectory&goto :EOF
if not exist "%~1" echo %~1 does not exist&goto :EOF
if not exist "%~2" echo %~2 does not exist&goto :EOF

set srcDir=%~1
set destDir=%~2

REM ** This makes sure that the trailing backslash if specified is removed
REM ** I don't believe that works in 2000, so you'll have to make sure
REM ** that you don't specify a trailing backslash for the destination
REM if "%destDir:~-1%"=="\" set destDir=%destDir:~0,-1%

call :GETDATEPARTS "%date%"

call :SUBTRACTDAYS 2

set cutOff=%yy%%mm%%dd%

echo curOff=%cutOff%
echo srcDir=%srdDir%
echo destDir=%destDir%

pushd "%srcDir%"

echo After pushd command...

for /f "tokens=*" %%a in ('dir /b /a-d "%srcDir%" 2^>NUL') do call :PROCESS "%%a" %%~ta

After for command...

popd

echo After popd command...

goto :EOF

:PROCESS

call :GETDATEPARTS %~2

%debug% if /i "%yy%%mm%%dd%" LSS "%cutOff%" move "%~1" "%destDir%\%~1"

goto :EOF

:GETDATEPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF

:SUBTRACTDAYS

set dayCnt=%1

if "%dayCnt%"=="" set dayCnt=1

REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100

:CHKDAY

if /I %dd% GTR 0 goto DONESUBTRACT

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yy=%yy% - 1

:ADJUSTDAY

if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through

:SET31

set /A dd=31 + %dd%

goto CHKDAY

:SET30

set /A dd=30 + %dd%

goto CHKDAY

:LEAPCHK

set /A tt=%yy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY

:DONESUBTRACT

if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

goto :EOF
C:\>movelogs "D:\Program Files\KANA\Response Server\shareddir\logs\" "W:\"
curOff=20070408
srcDir=
destDir=W:\
After pushd command...
'After' is not recognized as an internal or external command,
operable program or batch file.
After popd command...

C:\>
I forgot a echo statement on the one debugging line:

After for command...

Should be:

echo After for command...

I don't see any problems there. Where is the error about d:?
Umm...well it doesnt show the src directory it just says "srcDIr =", and now even tho it runs without any errors, it hasnt moved any of the files either...
the batchfile doesnt have to be located on the same drive as the srcDir does it?
No it doesn't have to exist in the same directory. Here's another debug batch. Please post back results:

@echo off

setlocal

REM ** Set debug= to activate code
set debug=

if "%~2"=="" echo Usage: %0 sourceDirectory destinationDirectory&goto :EOF
if not exist "%~1" echo %~1 does not exist&goto :EOF
if not exist "%~2" echo %~2 does not exist&goto :EOF

set srcDir=%~1
set destDir=%~2

REM ** This makes sure that the trailing backslash if specified is removed
REM ** I don't believe that works in 2000, so you'll have to make sure
REM ** that you don't specify a trailing backslash for the destination
REM if "%destDir:~-1%"=="\" set destDir=%destDir:~0,-1%

call :GETDATEPARTS "%date%"

call :SUBTRACTDAYS 2

set cutOff=%yy%%mm%%dd%

echo curOff=%cutOff%
echo srcDir=%srdDir%
echo destDir=%destDir%

pushd "%srcDir%"

for /f "tokens=*" %%a in ('dir /b /a-d "%srcDir%" 2^>NUL') do call :PROCESS "%%a" %%~ta

popd

goto :EOF

:PROCESS

call :GETDATEPARTS %~2

echo if /i "%yy%%mm%%dd%" LSS "%cutOff%" move "%~1" "%destDir%\%~1"

goto :EOF

:GETDATEPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF

:SUBTRACTDAYS

set dayCnt=%1

if "%dayCnt%"=="" set dayCnt=1

REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100

:CHKDAY

if /I %dd% GTR 0 goto DONESUBTRACT

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yy=%yy% - 1

:ADJUSTDAY

if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through

:SET31

set /A dd=31 + %dd%

goto CHKDAY

:SET30

set /A dd=30 + %dd%

goto CHKDAY

:LEAPCHK

set /A tt=%yy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY

:DONESUBTRACT

if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

goto :EOF
if /i "20070410" LSS "20070409" move "log20070410_213008.xml" "W:\log20070410_2
3008.xml"
if /i "20070410" LSS "20070409" move "log20070410_213444.xml" "W:\log20070410_2
3444.xml"
if /i "20070410" LSS "20070409" move "log20070410_214029.xml" "W:\log20070410_2
4029.xml"
if /i "20070410" LSS "20070409" move "log20070410_214441.xml" "W:\log20070410_2
4441.xml"
if /i "20070410" LSS "20070409" move "log20070410_214911.xml" "W:\log20070410_2
4911.xml"
if /i "20070410" LSS "20070409" move "log20070410_215448.xml" "W:\log20070410_2
5448.xml"
if /i "20070410" LSS "20070409" move "log20070410_215932.xml" "W:\log20070410_2
5932.xml"
if /i "20070410" LSS "20070409" move "log20070410_220332.xml" "W:\log20070410_2
0332.xml"
if /i "20070410" LSS "20070409" move "log20070410_220651.xml" "W:\log20070410_2
0651.xml"
if /i "20070410" LSS "20070409" move "log20070410_221120.xml" "W:\log20070410_2
1120.xml"
if /i "20070410" LSS "20070409" move "log20070410_221449.xml" "W:\log20070410_2
1449.xml"
if /i "20070410" LSS "20070409" move "log20070410_221912.xml" "W:\log20070410_2
1912.xml"
if /i "20070410" LSS "20070409" move "log20070410_222410.xml" "W:\log20070410_2
2410.xml"
if /i "20070410" LSS "20070409" move "log20070410_222758.xml" "W:\log20070410_2
2758.xml"
if /i "20070410" LSS "20070409" move "log20070410_223113.xml" "W:\log20070410_2
3113.xml"
if /i "20070410" LSS "20070409" move "log20070410_223611.xml" "W:\log20070410_2
3611.xml"
if /i "20070410" LSS "20070409" move "log20070410_224006.xml" "W:\log20070410_2
4006.xml"
i couldnt capture the first half of the debug....coz it started printing thisout...but again, none of the files have really been moved..like the names of the files that it mentions, are still in the src directory on the D drive
Look's like all of the files I can see were updated 4/10/2007. The compare date is 4/9/2007.

I do a less than comparision of:

if 4/10/2007 LSS 4/9/2007 Do the move

None of the files meet the requirement.
But I need to move the files for the last 2 days....so I need to move files for the 9th and 10th..
ASKER CERTIFIED SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
GEQ..ah okie...i tried GTE lol. This one gets stuck after it echoes the destDir tho...:( THanks for your prompt replies. Much appreciated!
Where it echo's destDir?

My last solution doesn't echo destDir... what are you talking about?
echo curOff=%cutOff%
echo srcDir=%srdDir%
echo destDir=%destDir%
C:\>movelogstest "D:\Program Files\KANA\Response Server\shareddir\logs" "W:"
curOff=20070409
srcDir=
destDir=W:
if /i "20070410" GEQ "20070409" move "log20070410_160041.xml" "W:\log20070410_16
0041.xml"
if /i "20070410" GEQ "20070409" move "log20070410_160312.xml" "W:\log20070410_16
0312.xml"
if /i "20070410" GEQ "20070409" move "log20070410_160539.xml" "W:\log20070410_16
0539.xml"
if /i "20070410" GEQ "20070409" move "log20070410_160810.xml" "W:\log20070410_16
0810.xml"
if /i "20070410" GEQ "20070409" move "log20070410_161008.xml" "W:\log20070410_16
1008.xml"
if /i "20070410" GEQ "20070409" move "log20070410_161309.xml" "W:\log20070410_16
1309.xml"
if /i "20070410" GEQ "20070409" move "log20070410_161517.xml" "W:\log20070410_16
1517.xml"
if /i "20070410" GEQ "20070409" move "log20070410_161705.xml" "W:\log20070410_16
1705.xml"
if /i "20070410" GEQ "20070409" move "log20070410_161932.xml" "W:\log20070410_16
1932.xml"
if /i "20070410" GEQ "20070409" move "log20070410_162238.xml" "W:\log20070410_16
2238.xml"
if /i "20070410" GEQ "20070409" move "log20070410_162436.xml" "W:\log20070410_16
2436.xml"
if /i "20070410" GEQ "20070409" move "log20070410_162726.xml" "W:\log20070410_16
2726.xml"
if /i "20070410" GEQ "20070409" move "log20070410_163024.xml" "W:\log20070410_16
3024.xml"
if /i "20070410" GEQ "20070409" move "log20070410_163300.xml" "W:\log20070410_16
3300.xml"
Can you run the most recent version I published?
thats the one....i posted the above results from that. ijust echoed the GEQ line
Okay, and it stops or hangs up you say? When does it do that?
it stops soon after it prints the "destDIr=" line
sorry..it hangs soon after that line.not stops
I'm sorry but the code I most recently posted doesn't echo:

destDIr=

Please post the code you are running.
it looks like it worked!! sorry..there were just too man files to move i guess and i thought it had hung becoz nothing was showing up ion the dest dir.! i see that theyre there now tho. THaaank you!!
Glad it worked :)
To run this as a scheduled task, can i use the scheduler in windows and then in the "Run" box I just enter "c:\movelogs "D:.." "w:""
?
You could do that, but you might get into trouble access network resources under the default system account. You'll want to specify run as and set it to a domain user, like yourself. Also, you might to better to use a UNC instead of a mapped drive. Like this:

c:\movelogs d:\yourdir \\server1\share1\directory

c:\movelogs d:\yourdir \\server1\share1\directory.. here movelogs in the name of my batch file..so are you saying i just add the source and destination directories after that? (like  i do from the command prompt)...not sure what a UNC is..sorry!
Basically you are referencing the network resource without the mapped drive.

Type net use and it will show you what the drive making is.
thanks a TON steve!
actually..that doesnt work on the win2000 machine :(
What doesn't work?
net use. I'mmoving the files to the root directory of the mapped drive, so I didnt quite follow what you meant by  c:\movelogs d:\yourdir \\server1\share1\directory..
Net use works on 2000 and XP.

You'd only need to use the UNC (\\server\etc...) if you were accessing a network resource through a mapped drive in a scheduled task.

The problem is that you can't be sure that w: or whatever will be mapped when the scheduled task is run. Do you understand that?
Yep i understood that:) wasnt sure how that UNC works!
aaah...never mind!!! i got what youre saying...youre sayign istead of just saying w: which is the mapped drive, provide the entire path for the mapped drive..correct?
That is correct :)
thanks:)..again!