Batch file script not working in Windows 2008

I have the following script in a batch file that works with on OS Windows 2008 R2 and > but not in Windows 2008 server

In windows 2008 I get
ERROR: Invalid argument/option - '@file'.

forfiles /p "B:\" /s /d -3 /m *.zip /c "cmd /c del @file"

Open in new window

Can someone help me make it work on a Windows 2008 server?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Is the 2008 version the same? Does it have the same options? Run forfiles /?
GerhardpetAuthor Commented:
Yes from what I can tell the options are the same
Bill PrewCommented:
A bit of a longshot, but try:

forfiles -p "B:\" -s -d -3 -m *.zip -c "cmd /c del @file"

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Bill PrewCommented:
If that doesn't work, do the following at a command line and post up the output.

forfiles /?

GerhardpetAuthor Commented:
That did not work. Here is the output

C:\Users\>forfiles /?

FORFILES [/P pathname] [/M searchmask] [/S]
         [/C command] [/D [+ | -] {MM/dd/yyyy | dd}]

    Selects a file (or set of files) and executes a
    command on that file. This is helpful for batch jobs.

Parameter List:
    /P    pathname      Indicates the path to start searching.
                        The default folder is the current working
                        directory (.).

    /M    searchmask    Searches files according to a searchmask.
                        The default searchmask is '*' .

    /S                  Instructs forfiles to recurse into
                        subdirectories. Like "DIR /S".

    /C    command       Indicates the command to execute for each file.
                        Command strings should be wrapped in double

                        The default command is "cmd /c echo @file".

                        The following variables can be used in the
                        command string:
                        @file    - returns the name of the file.
                        @fname   - returns the file name without
                        @ext     - returns only the extension of the
                        @path    - returns the full path of the file.
                        @relpath - returns the relative path of the
                        @isdir   - returns "TRUE" if a file type is
                                   a directory, and "FALSE" for files.
                        @fsize   - returns the size of the file in
                        @fdate   - returns the last modified date of the
                        @ftime   - returns the last modified time of the

                        To include special characters in the command
                        line, use the hexadecimal code for the character
                        in 0xHH format (ex. 0x09 for tab). Internal
                        CMD.exe commands should be preceded with
                        "cmd /c".

    /D    date          Selects files with a last modified date greater
                        than or equal to (+), or less than or equal to
                        (-), the specified date using the
                        "MM/dd/yyyy" format; or selects files with a
                        last modified date greater than or equal to (+)
                        the current date plus "dd" days, or less than or
                        equal to (-) the current date minus "dd" days. A
                        valid "dd" number of days can be any number in
                        the range of 0 - 32768.
                        "+" is taken as default sign if not specified.

    /?                  Displays this help message.

    FORFILES /S /M *.txt /C "cmd /c type @file | more"
    FORFILES /P C:\ /S /M *.bat
    FORFILES /D -30 /M *.exe
             /C "cmd /c echo @path 0x09 was changed 30 days ago"
    FORFILES /D 01/01/2001
             /C "cmd /c echo @fname is new since Jan 1st 2001"
    FORFILES /D +3/27/2015 /C "cmd /c echo @fname is new today"
    FORFILES /M *.exe /D +1
    FORFILES /S /M *.doc /C "cmd /c echo @fsize"
    FORFILES /M *.txt /C "cmd /c if @isdir==FALSE notepad.exe @file"

Open in new window

Try removing the quotes around "B:\".

A Microsoft Technet example excludes the quotes:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
- Do you get errors with...
forfiles /p "B:\" /s /d -3 /m *.zip /c "cmd /c echo del @file"

- Also try:
forfiles /p "B:\" /s /d -3 /m *.zip /c "cmd /c call del @file"
Bill PrewCommented:
Okay, matches the latest version of FORFILEs.  I tried your command here (Windows 8.1 desktop) and it worked fine, so syntax seems okay.

FORFILES should work okay on Windows Server 2008, not sure what is causing you trouble.

Steve KnightIT ConsultancyCommented:
Does it work without the /c argument at all, i.e. using the default to show it on the screen?
I just have a clarifying question.  Is this command really what you use?  If so, do both servers *truly* have a B:\ drive?

I get the same error if the drive does not exist on the server.  Seems like this is a bug in the version of FORFILES in that it resolves an incorrect string literal for the true error which is "Invalid drive or directory name".I don't have a drive B:\ or T:\.  Attempting to run forfiles on the drive ends up giving me the incorrect string literal for the true error which should be "Invalid drive or directory name".On a 2008R2 Server, again, with no T:\ drive.  Notice the difference in error messages from non-existing drive to directory on non-existing drive.-saige-
Interestingly enough, if I remove the \ from in front of the drive letter on the 2008R2 server, I do get the error associated with "The specified directory does not exist".  The same applies to the 2008 server.Capture.JPGCapture.JPG
Please see attached screen shot. I removed the backslash from the drive path and it seems to work fine in my environment.

The drive path must exist or else you will get an error... Also, I just echoed the file names out for illustration purposes. You can change it to fit your needs.
Steve KnightIT ConsultancyCommented:
Interesting, yes it would appear that if you use a \ on the end of the path specified AND quotes it fails... without the quotes it is happy to have a \ on the end, whether at the root or part way down a path.  Odd!

If you have the /c argument  then you get the @file error, otherwise a complaint about the path.

i.e. what was suggested originally in http:#40693091 by LajuanTaylor

Fail saying @file error if /c is specified, otherwise directory name error:

forfiles /p "d:\"

But all these are OK
forfiles /p d:\

cd /d d:\
forfiles /p "D:"

forfiles /p d:\
Bill PrewCommented:
Interestingly, ROBOCOPY has a very similar "feature" as I recall.

There's always the PowerShell option if the batch is not working properly...

Please see attached screen shot where I started PS as Admin. The following line was ran under "RemoteSigned" execution policy in PowerShell. It recursively looks for zip files within a date range and lists them in table format.

Please visit the URL below for a detailed script that will delete specific files for a given time range in number of days old.

Get-ChildItem C:\DATATRANSFER -recurse| Where-Object {$_.extension -eq ".zip"} | Where-Object { $_.CreationTime -ge "03/01/2015" -and (Get-Date).AddDays(-3) } | format-table name

Open in new window

GerhardpetAuthor Commented:
I appreciate all of the help. Removing the quotes from the "B:\" worked.

Thanks again!
Great. Good to hear one of the many possible suggestions worked for you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.