DOS Script - Check if a folder is empty

Posted on 2009-02-09
Last Modified: 2013-11-10
During my daily processing, I need to move files from a source(incoming) folder to a target(processing) folder. Once the file is in my target folder, I kick off my application which processes this file. After my application completes processing, another script moves the file out of the processing folder.
Before I move my files from the incoming to the processing folder, I want to ensure that my processing folder is empty. I saw some solutions to the problem using the FOR command, but that command doesn't work at this client. How do I ensure that my target foler is empty before moving any new files into it?
Question by:mightyestme
    LVL 95

    Expert Comment

    by:Lee W, MVP
    What do you mean the FOR command doesn't work at that client?  The For Command is built in to Windows Server.  How does it now work?  (are you trying to run it in instead of cmd.exe?)

    Accepted Solution

    I did run cmd.exe
    The error I used to get is:
    'for' is not recognized as an internal or external command, operable program or batch file

    Same for find. After reading your comment, when I tried to run for and find again, it says syntax not correct. So these commands do seem to be working now. I am at a client who are extremely restrictive, and I guess something might have changed since the last time I tried this out.

    If possible, I would still like to see a solution to this problem without using for/find simply because I am not sure if my automation user will have access to these commands, and I don't want it to fail the first time it runs.

    One more pointer: My target folder, if not empty, will contain files starting with DET*. I tried using:

    if exist %Tgt%\DET*.* (
    write to log file
    exit 1

    This seems to be working. I was looking for a more elegant solution, where I don't need to use the DET*.* option.
    LVL 95

    Expert Comment

    by:Lee W, MVP
    That kind of error suggests the server's path statement is messed up.  Can you post it?

    Author Comment

    I will post the PATH as soon as possible. I have access to the server only 2 hours a day, and my 2 hours for today are now up. (This is not even production yet).

    Again, a solution without fancy commands and reasonably flexible would be enough.

    Author Comment

    Here is the path variable:

    Path=C:\Program Files\Support Tools\;C:\Program Files\HP\NCU;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\IBM\DB2RuntimeClient 7\bin\;C:\Program Files\IBM\DB2RuntimeClient 7\function;C:\Program Files\IBM\DB2RuntimeClient 7\help\;C:\Program Files\BMC Software\Control-M Agent\Default\EXE\;C:\Program Files\GnuWin32\bin;C:\Program Files\Microsoft SQL Server\90\Tools\binn\
    LVL 95

    Expert Comment

    by:Lee W, MVP
    ok, that looks good... can you take a screen shot of the FOR command failing?  where it displays the command executed and the error message?

    Author Comment

    the for isn't failing now, it used to be failing in the past. I tried again today, and it worked.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    Title # Comments Views Activity
    canBalance challenge 34 55
    scoresIncreasing challenge 10 47
    Enhanced For loop vs ForEach Loop 4 42
    countAbc challenge 9 35
    This is an explanation of a simple data model to help parse a JSON feed
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

    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

    12 Experts available now in Live!

    Get 1:1 Help Now