Results 1 to 5 of 5

Thread: Batch script to delete directory

  1. #1
    Join Date
    Dec 2009
    Posts
    211

    Batch script to delete directory

    Hello,
    I need to write a batch file to delete a folder or directory which is empty. How can I do this? First of all how to identify that a directory is empty? I have also found the commands which work on the command prompt, so that I can use them. But the problem is that when i try to compile the code in the batch file the commands do not work there.
    Here is what I have tried
    Code:
    @echo off
    cd..
    cd..
    cd..
    cd..
    cd seth
    cd desktop
    IF NOT EXIST key.txt
    DEL test.txt
    exit

  2. #2
    Join Date
    Nov 2009
    Posts
    333

    Re: Batch script to delete directory

    Hello,
    Instead of the script that you are using you can use the following script.
    I have modified your script to look something like this
    Code:
    @echo off
    cd..
    cd\documents
    cd seth
    cd desktop
    if not_exist_key.txt del test.txt
    exit
    Please not the below line in the code
    Code:
    if not_exist_key.txt del test.txt
    All are in the lower case and not in the upper case, as you have done in you code. Please correct that.

  3. #3
    Join Date
    Nov 2009
    Posts
    446

    Re: Batch script to delete directory

    Hello,
    Alternatively you can set the location for the batch file which you are trying to execute.
    That is
    @echo off
    To simplify the command line you can set the location to this path
    Code:
    SET Location=C:\Documents and Settings\set\......
    Use the full path name of the file that is the canonical path of the file
    Code:
    IF NOT EXIST "%Location%\somefile.txt" DEL "%Location%\otherfile.txt"

  4. #4
    Join Date
    Nov 2009
    Posts
    583

    Re: Batch script to delete directory

    Hello,
    I have identified you problem.
    This is what you do in your code
    Code:
    IF NOT EXIST somefile.txt
    DEL otherfile.txt
    exit
    If you are programming in a batch file then you can not write multiline if statements in your code. Rather it should be only on a single line or can be even extended.
    Something like this
    IF NOT EXIST somefile.txt DEL otherfile.txt
    exit

  5. #5
    Join Date
    Nov 2009
    Posts
    518

    Re: Batch script to delete directory

    Hello,
    I agree with the above post. I have a code which might interest you. Take a look at it
    Code:
    IF NOT EXIST somefile.txt (
         another command if wanted
         DEL otherfile.txt
         another command if wanted
         )
    exit
    This eve works in window xp
    Code:
    c:\>(
    More? echo 1
    More? echo 2
    More? echo 3
    More? )
    c:\>

Similar Threads

  1. Batch Script Issue
    By spmaguire in forum Software Development
    Replies: 3
    Last Post: 04-09-2013, 10:41 AM
  2. Script needed to Delete Files in a Directory Based on Date
    By adaher008 via WinServerKB.com in forum Windows Server Help
    Replies: 11
    Last Post: 02-01-2012, 01:44 AM
  3. Batch file to check directory
    By Trini Alvarado in forum Software Development
    Replies: 5
    Last Post: 01-04-2010, 11:00 AM
  4. Delete the contents of a directory using Batch
    By GUSSIE in forum Operating Systems
    Replies: 3
    Last Post: 11-09-2009, 12:43 PM
  5. Retrieving the current directory in batch
    By Jacques25 in forum Software Development
    Replies: 3
    Last Post: 15-07-2009, 11:36 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,481,250.94580 seconds with 17 queries