FORFILES - Access is Denied and Invalid syntex Errors

ITCSAdmin
ITCSAdmin used Ask the Experts™
on
I am having issues with a new server (Windows 2003, 64-bit, PS2) when i attempt to run a batch file with FORFILES commands to create and update a log file, copy and delete files older than 60 days of a specific extension. This same batch file works on my Developement Server, but will not on my Production Server. On the Production Server I am getting the following when I run the batch file:
* Access is Denied
* Invalid syntex Errors

I have made sure that FORFILES is installed on hte server, I can do FORFILES /? and get the same results as the Development Server.

I am in the process of deploying a very large Enterprise Document Managment System this weekend and this is one that we need to correct prior to Monday 7AM ET.

I need all the help that i can to complete this Deployment and this issue.

Thanks

Damon
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I don't think it's the FORFILES command that you are having issues with. If you are copying and deleting files as you say, your user needs permission to perform this action. On your local machine you have sufficient permissions, on the server the user you are logging in with does not.

Try start the command promt as an administrator (Right-click -> Run as Administrator) and test your batch script.
Gerwin Jansen, EE MVETopic Advisor
Most Valuable Expert 2016

Commented:
Can you post your batch file?

Author

Commented:
Here is the batch file that I put together from examples of how to Copy and Delete files older than X and include a log file. See below:

----------------------------Beginning of Batch File----------------------------------------

@echo off
   
REM Modified by Damon Sherrod - Website: http://www.ITCS-LLC.com - Email: Damon.Sherrod@ITCS-LLC.com
 
    :: Archive_File.bat
    set Version=v2.04
                                :: VERSION 2.00 12-08-2010 Damon Sherrod         Intial Code Developement.
                                :: VERSION 2.01 12-09-2010 Damon Sherrod         Adding code for Log File.
                                :: Version 2.02 01-11-2011 Damon Sherrod           Changed Location where it will run from.
                                :: Version 2.03 01-12-2011 Damon Sherrod           Changed location for Log File & Name of Log File
                                :: Version 2.04 02-04-2011 Damon Sherrod           Removed "\" from Source and Destination
   
                                :: NOTE: This must NOT be used on a computer with a ‘forfiles’ version created before 2000 as the syntax has since changed.
 
                                :: All you need to do is change the variables at the top of the batch file. Heres what they should be, in case my names aren’t totally self-explanitory.
                                :: This is a very multi-purpose file and can be used for all kinds of stuff! Enjoy!
 
REM the following will allow a date stamp for Log file to benamed using date. Added: 12-09-2010
                                :: possible formats are "mm dd yyyy", "dd mm yyyy" and "yyyy mm dd"
                                                                set DateFormat=yyyy mm dd
                                                                :: Possible separators are - (dash), / (slash) or . (dot)
                                                                set DateSeparator=-
                               
                                :: split up
                                                                :: split up dd/mm/yyyy hh:mm to yyyy, mm and dd        
                                                                if "%DateFormat%"=="dd mm yyyy" (
                                                                for /f "Tokens=1,2,3 Delims=%DateSeparator% " %%i In ('echo !md!') do set md=%%i& set mm=%%j& set my=%%k
                                                                )
                                                                :: split up yyyy-mm-dd hh:mm to yyyy, mm and dd          
                                                                if "%DateFormat%"=="yyyy mm dd" (
                                                                for /f "Tokens=1,2,3 Delims=%DateSeparator% " %%i In ('echo !md!') do set my=%%i& set mm=%%j& set md=%%k
                                                                )
                                                                :: split up mm/dd/yyyy hh:mm to yyyy, mm and dd        
                                                                if "%DateFormat%"=="mm dd yyyy" (
                                                                for /f "Tokens=1,2,3 Delims=%DateSeparator% " %%i In ('echo !md!') do set mm=%%i& set md=%%j& set my=%%k
                                                                )
                                :: merge vars into one var
                                                                call set mdate=!my! !mm! !md!
                               
                                :: remove spaces in mdate
                                                                call set mdate=!mdate: =!
                               
                                :: Make Log File names by Date
                                    ::set logname=%mdate%
                                    set logname=Dev_Archive_File_Log
                                    set logext=txt
 
                                :: loglocation – the location you want your logs to be recorded at
                                    set Loglocation=E:\logs\"%logname%.%logext%"
                                   
REM Set all defaults for this application.
 
                                :: olderthan – number of days old the files you want to target have to be
                                :: source – the location the files will start at (this field must not end with a \)
                                :: destination – the location you want to move the files to before deleting
                                :: extension – The file extension(s) to copy then delete, just use * for all files.
 
    set olderthan=20
    set source=E:\FB_IP_Backup\Med-Advantage\Zips
    set destination=E:\FB_IP_Backup\Med-Advantage
    set extension=zip
   
REM Below is if you want to send and email.
                                :: email – the address that will be notified when the operation sends an email report
                                :: blat – the path to the blat.exe file (this, and all the others, can be a UNC path). Must be downloaded from souce forge most of the time.
                                :: relayserver – Target mail server with relay enabled. (required)
                                :: subject – The subject of the report email that will be sent.
    :: Blat is a Win32 command line utility that sends eMail using SMTP or post to usenet using NNTP.
   
    :set email=Damon.Sherrod@MultiPlan.com; dmitriy.goltseker@multiplan.com
    :set blat=c:\blat.exe
    :set relayserver=localhost
    :set subject="Copyout.bat Report"
 
REM Delete Log File if it exists.
    if exist %loglocation% del %loglocation%
 
REM :: start Time of Log File
                                :: Diplay for Command Window
                                echo 0 - Started Relocate_File - %Version% on %Date% at %Time%
                                :: Append to Log File
                                echo 0 - Started Relocate_File - %Version% on %Date% aat %Time% >> %loglocation%
   
REM       Beginning of Log File and Display to user.
                                :: Display for Command Window
    echo 1 - Copying and deleting files older than %olderthan% days with the file extension *.%extension%.
    :: Append to Log File
    echo 1 - Copying and deleting files older than %olderthan% days with the file extension *.%extension%. >> %loglocation%
                                :: Display for Command Window
    echo 2 - Copying from %source% to %destination% and deleting from %source%.
    :: Append to Log File
    echo 2 - Copying from %source% to %destination% and deleting from %source%. >> %loglocation%
                                :: Display for Command Window
    echo This file may take an extremely long time to run while it looks unresponsive.
    echo Check %loglocation% for copy progress.
 
REM Changing to the Source Location
    cd %source%
 
REM List Files to be Copied and Removed
                                :: Diplay for Command Window
    echo 3 - List of files to be copied and removed:
    :: Append to Log File
    echo 3 - List of files to be copied and removed: >> %loglocation%
 
                                :: Diplay for Command Window
    FORFILES /p %source% /d -%olderthan% /m *.%extension% /c "CMD /C Echo "@FILE"
    FORFILES /p %source% /d -%olderthan% /m *.%extension% /c "CMD /C Echo "@FILE" >> %loglocation%
 
REM       Update Log File - Starting Copy with Date and Time
                                :: Diplay for Command Window
    echo 4 - Starting copy...
    :: Append to Log File
    echo 4 - Starting copy... >> %loglocation%
    date /t >> %loglocation%
    time /t >> %loglocation%
 
REM       Copy Files from Source to Destination folder
    FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C XCOPY %source%\@FILE %destination%"
 
REM       Update Log File - Starting Delete with Date and Time
                                :: Diplay for Command Window
    echo 5 - Starting delete...
    :: Append to Log File
    echo 5 - Starting delete... >> %loglocation%
    date /t >> %loglocation%
    time /t >> %loglocation%
 
REM       Delete Files at Source if it exist in Destination Folder
    FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C if exist %destination%\@FILE del %source%\@FILE /Q & echo @FILE Deleted." >> %loglocation%
 
REM       Updating the Log File
    echo Log file located at %loglocation%
 
                                :: Diplay for Command Window
    echo 6 - Copy and Delete finished... Sending Report.
    :: Append to Log File
    echo 6 - Copy and Delete finished... >> %loglocation%
 
REM List Source Directory Contents
                                :: Diplay for Command Window
    echo 7 - Source Directory Contents:
    :: Append to Log File
    echo 7 - Source Directory Contents: >> %loglocation%
    echo. >> %loglocation%
    dir %source% >> %loglocation%
 
    echo. >> %loglocation%
    echo. >> %loglocation%
    echo. >> %loglocation%
 
REM List Destination Directory Contents
                                :: Diplay for Command Window
                                echo 8 - Destination Directory Contents:
    :: Append to Log File
    echo 8 - Destination Directory Contents: >> %loglocation%
    echo. >> %loglocation%
    dir %destination% >> %loglocation%
 
 
REM Send Email that Application ran.
                                :: Diplay for Command Window
                                :echo 9 - Emailing
                                :: Append to Log File
                                :echo 9 - Emailing >> %loglocation%
    :set server=%computername%
    :: Configuration to send an email using Blat.exe
    :%blat% %loglocation% -t %email% -s "%server% %subject%" -server %relayserver% -f do-not-reply@%server%.copyout.bat
 
    echo Report sent!
   
REM :: Stop Time of Log File
                                :: Diplay for Command Window
                                echo 10 - Stop Relocate_File - v2.01 on %Date% at %Time%
                                :: Append to Log File
                                echo 10 - Stop Relocate_File - v2.01 on %Date% aat %Time% >> %loglocation%

---------------------------------End of Batch File----------------------------------------------------

The above file works correctly on my Developement Server (Windows 2003) with no issues, but when I run this same batch file on the Production Server, I get the previously mentioned issues.

Damon
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Topic Advisor
Most Valuable Expert 2016
Commented:
Hi,

These 2 lines have -p -m and -c instead of /p /m and /c :

FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C XCOPY %source%\@FILE %destination%"

FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C if exist %destination%\@FILE del %source%\@FILE /Q & echo @FILE Deleted." >> %loglocation%

This is not correct, should be / for each option, does this fix your syntax error messages?

Author

Commented:
I am sorry that I did not follow up with a response to the both of you, but we were able to resolve the issues. We also found that there was spaces that should not be there around the "=" in the code.

I thank you both. The Go-Live Launch was successful.

ITCS Admin

Damon
Gerwin Jansen, EE MVETopic Advisor
Most Valuable Expert 2016

Commented:
Hi Damon, glad it worked out for you :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial