Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Writing a batch file  (Read 3822 times)

0 Members and 1 Guest are viewing this topic.

PJ

  • Guest
Writing a batch file
« on: September 21, 2004, 02:31:37 PM »
Hiya
Can any one tell me how to write a batch a file that will delete a folder specifed by the user of the batch file, when they run it.
Eg to delete the folder "user" that is in c:\documentsandsettings\user
all the user of file would have todo is when prompted type in the file name they wanted to delete, "user"
Thanx any help would be much appreciated
Beginner programer

ChrisC

  • Guest
Re: Writing a batch file
« Reply #1 on: September 23, 2004, 02:04:14 AM »
From long ago and not so long ago DOS experience,  when the batch file is called, have the user specify the full path of the file to be deleted
 BATDelete C:\Documentsandsettings\user

And the .BAT file would contain:
ECHO OFF
DEL %1

But that then raises some questions.
What if the file doesn't exist?
What if the user of the .BAT file specifies the wrong file?  Shouldn't we give the user the responsibility to verify and affirm the file to be deleted?  The DOS delete in .BAT files does not place the file in trash - the file just dissapears!

So, using the same batch file call, lets improve what happens:
ECHO OFF
IF EXIST %1% goto XX1
ECHO.
ECHO File %1 does not exist
echo.
GOTO XXX
:XX1
Echo.
echo File %1  Is this the file you want to delete
ECHO Press any key to delete this file.
ECHO Press CTRL-C to terminate without deleting.
PAUSE
DEL %1
:XXX

To learn more about DOS and BATCH files (which work in WindowsXP, do a GOOGLE search for "batch file parameters" without the quotes as an EXACT search in Google.com/advanced_search

Chris C

PJ

  • Guest
Re: Writing a batch file
« Reply #2 on: September 23, 2004, 09:11:43 AM »
thanx for the help but also need to know how i can make it remeber what is firist type at the prompt "what is your username?" then transfer it to the part of script, where it removes the dir the line is "set cmd= rmdir"
can anybody help

This is script so far
:SET
echo.
set SET=
set /p SET=What is your username?
goto remove
:end

:remove
cd c:\docume~1
set cmd= rmdir
echo.
set /p cmd= Please press return to delete file.
%cmd%
goto pause

:pause
pause
:end

???

Thanx

MalikTous

  • Guest
Re: Writing a batch file
« Reply #3 on: September 23, 2004, 02:15:22 PM »
With Win9x, you had a command DELTREE that would unconditionally wipe out a target directory tree. Your batch file could be as simple as

xd user

and the batch file would be XD.BAT:

deltree "c:\my documents\%1"
md "c:\my documents\%1"


With XP CLI emulation, the DELTREE command was stupidly omitted, so the user is stuck with only being able to delete the file content of a single directory:

del "c:\documents and settings\%1\my documents\%1\*.*" /y

The subdirectories of this remain intact, but the user's root content is deleted.