Operating System - Microsoft
1748209 Members
2788 Online
108759 Solutions
New Discussion юеВ

Re: how to write a batch file in windows??

 
SOLVED
Go to solution
kabucek
Advisor

how to write a batch file in windows??

How to write a .batch file in windows that will delete all files including hidden files. How to write next .batch file for compressing all files in directory and then send them trough ftp???
20 REPLIES 20
Vinay N
Esteemed Contributor
Solution

Re: how to write a batch file in windows??

Hi Kabucek,

Batch file is nothing but a way of executing combination of commands.

For instance a delete command like what you want....

say you want to delete all files in a particular folder(abc)
so open notepad --- type---del c:\abc\*.*

Save this file as del.bat with file type as all files.

Similarly you can use other command along with it in the same batch file ---compressing command and ftp commands...

Regards,
V
(Points appreciated :)

Jon Finley
Honored Contributor

Re: how to write a batch file in windows??

See if these work for you:
--------------------------------

@echo off
rem .:-------------------------------------
rem .: Delete all files within a folder
rem .:-------------------------------------

set /P my_folder="Please enter folder to delete files within: "

if exist %my_folder% goto do_dfile
goto error

:do_dfile

cd %my_folder%

del /F *.*
del /F /AH *.*

goto done

:error
echo Folder was not found.

:done

rem .:-------------------------------------
rem .: End of delete script
rem .:-------------------------------------

==============================================================

@echo off
rem .:-------------------------------------
rem .: Compress (Zip?) then FTP files
rem .:-------------------------------------

set /P my_folder="Please enter folder to zip and FTP: "

if exist %my_folder% goto do_zfile
goto error

:do_zfile

cd %my_folder%

rem .: if your date has '/' in the date, they need to be stripped out.
for /f "delims='/'" %%i in ('%date%') do
set my_date=%%i-%%j-%%k

zip_program /switches *.* %my_date%.zip

set /P my_loc="Please enter FTP site: "
set /P my_id="Please enter your ID: "
set /P my_pass="Please enter your Password: "

echo open %my_loc% > ftp_conn.txt
echo user %my_id% %my_pass%>> ftp_conn.txt
echo binary >> ftp_conn.txt
echo hash >> ftp_conn.txt
echo put %my_date%.zip >> ftp_conn.txt
echo close >> ftp_conn.txt
echo quit >> ftp_conn.txt

ftp -s:ftp_conn.txt

del ftp_conn.txt
del %my_date%.zip

goto done

:error
echo Folder was not found.

:done

rem .:-------------------------------------
rem .: End of zip/ftp script
rem .:-------------------------------------


Jon
"Do or do not. There is no try!" - Yoda
kabucek
Advisor

Re: how to write a batch file in windows??

when i type del *.* /s it isn't deleting directories!! why ????
Vibhor Kumar Agarwal
Esteemed Contributor

Re: how to write a batch file in windows??

Del doesn't go recursively.

Tyep

del /s *.*
Vibhor Kumar Agarwal
Jon Finley
Honored Contributor

Re: how to write a batch file in windows??

If you want to remove the directories rather than just delete the contents of directories, you need to use rm or rmdir

rmdir my_folder or
rmdir /s my_folder

This will also remove the specified folder, so if you are in the folder, you need to go up a level ../ prior to issuing the command.

Jon
"Do or do not. There is no try!" - Yoda
kabucek
Advisor

Re: how to write a batch file in windows??

what if i want to delete files in current directory and all subfolder within.
del *.* /s or del /s *.* isn't working


it's deleting only files not subfolders. why?
kabucek
Advisor

Re: how to write a batch file in windows??

del *.* <- isn't working... why?
after that command i have message ...(somenamefiles)\*.*
what if i want to delete files in current directory and all subfolder within.
del *.* /s or del /s *.* isn't working


it's deleting only files not subfolders. why? i don't want to delete directory that i'm in.
Shijo
Frequent Advisor

Re: how to write a batch file in windows??

Hi kabucek,
I have made some changes to the original commands which Jon had given( which I hope Jon does not mind-Jon pls don't sue me for copyright infringement). Now what this does is since u want to delete the sub directories and files the only way to do that is to delete the directory itself. Now your problem was that you want to delete the original folder contents but leave the folder alone but that's not possible with the command RD or RMDIR so what had to be done is delete the folder and create another folder under the same directory with the same name. And so that's what this does --> deletes folder and all subfolders and then creates a folder with the same name


@echo off
rem .:-------------------------------------
rem .: Delete all files within a folder
rem .:-------------------------------------

set /P my_folder="Please enter folder to delete files within: "

if exist %my_folder% goto do_dfile
goto error

:do_dfile

rd /s %my_folder%
md %my_folder%

goto done

:error
echo Folder was not found.

:done

rem .:-------------------------------------
rem .: End of delete script
rem .:-------------------------------------



Simple ain't it, now on a very personal note i would like to say that u r asking these questions to your peers so please add a couple of pleases and thank yous in your posts. we ain't your slaves just your ordinary technogeeks(of which i do not consider myself worthy to be part of).

Regards,
Shijo
You are only limited by the boundaries that you yoursefl put up.
Jon Finley
Honored Contributor

Re: how to write a batch file in windows??

another way that you can delete ALL of the directories within a folder, except for the current directory, is to add the following 'FOR' command to the original script file.

cd %my_folder%

del /F *.*
del /F /AH *.*
for /f %%i in ('dir /b /ad') do
rmdir %my_folder%\%%i

goto done

Jon
"Do or do not. There is no try!" - Yoda