Microsoft, Windows Schedule Task, Windows Server 2003 Standard, Running Forfiles in a batch-file

Posted on 2007-08-07
Last Modified: 2013-11-05
Computer is a member server running Windows 2003 SVR in an Windows SBS domain.
I have scheduled a simple batch file to run in Windows Schedule Task using the domain\administrator account.
The batch file runs the XCOPY and FORFILES utilities. Objective is to use FORFILES to clean up prior to using XCOPY to backup remaining files to a network share.
It contains the following lines

FORFILES -pR:\EXACT\009 -s -m*.* -d-5 -c"CMD /C del @FILE" > filelogr.txt
xcopy "D:\SQL\GoodHealth\MSSQL\BACKUP\009\*.bak" "\\Pc-store\Raid-Backup\Exact\009\" /D /C /F /Y > synclogr.txt

The batch file runs as scheduled. XCOPY works as expected. However, FORFILES fails. The error is that R:\EXACT\009 is not a valid directory. R: is a mapped drive.
But when I run the same batch file whilst logged on as domain\admnistrator, both runs successfully. FORFILES only fails when I run it via the Windows Scheduler.

I have tried using UNC instead of the mapped drive R:. When I did that, FORFILES complained about not supporting UNC.

Any trouble-shooting advice and suggestions would be gratefully received.

Question by:garychu
    LVL 30

    Accepted Solution

    Don't use a mapped drive in a scheduled task unless you explicitly map it. Use a UNC instead:



    The reason is that the mapped drive you have available under your accounts isn't setup for the user.

    Author Comment

    Thanks SteveGTR
    I have already tried using a UNC instead of a mapped drive. Unfortunately, the utility (from Microsoft no less) FORFILES does not support UNC. Duh!
    LVL 30

    Expert Comment

    Well, well, well...

    You could also ways map a network drive then do it:


    net use z: /del
    net use z: \\server\share

    FORFILES -pz:\EXACT\009 -s -m*.* -d-5 -c"CMD /C del @FILE" > filelogr.txt
    xcopy "D:\SQL\GoodHealth\MSSQL\BACKUP\009\*.bak" "\\Pc-store\Raid-Backup\Exact\009\" /D /C /F /Y > synclogr.txt

    Author Comment

    Thanks again SteveGTR.
    I had actually thought of doing that earlier. But thought it would be redundant. And a system "error" about the mapped drives already being in use etc might cause the batch file not to complete, unattended.
    Well I was wrong. Because I have now tried it and found that it worked.
    Thanks for your pointer!
    LVL 30

    Expert Comment

    Glad to help and I learned something too about forfiles not supporting UNC's. Very interesting.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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.

    I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    761 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