Operating System - Microsoft
1828317 Members
3598 Online
109976 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
Shijo
Frequent Advisor

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

@Jon
So you r looping it to find all dir and delete one by one. I haven't checked it out but if it works thats awesome. *bows down* But my way is still better simple yet efficient. Even though the thought occured to me to use a loop I didn't know how to do it. Heck this is the first time I see a 'for' command used as dos command(gotta learn more). But since Kabucek does not want anything to be in the folder I think my option works..just a sweet wam bam thank you mam.

Regards,
Shijo
You are only limited by the boundaries that you yoursefl put up.
kabucek
Advisor

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

what if i don't want to specify directory. just simple write ones the batch file. run it and after that it will delete all files and folder within (include hidden, system, etc.) BUT NOT the bath file!! when it will delete all the files i want to take that batch file to different location and do the same thing.
Shijo
Frequent Advisor

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

@Jon

Jon the for loop does not work, I think the problem is even though the loop might work the my_folder var still has the name of the intial dir, U need to extract the name of each dir in the loop and put it in var my_folder and I don't know how so some guidance please.

Kabucek what u r trying to do does not make any sense, why do you want to put it in the directory when u still have to specify the name of the directory anyway. Put the dat file in the root directory and just execute with the name of the folder u wanna nuke.

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??

Minor change... either ()'s need to be used, or the command goes on one line like below:

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

goto done


**note that it "may" wrap, but the command should be on the same line.

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

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

Ok..... tested and enhanced:

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

for /f %%i in ('cd') do set cur_dir=%%i


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 /Q *.* > nul
del /F /Q /AH *.* > nul

for /f %%i in ('dir /b /ad') do rmdir /S /Q "%my_folder%\%%i" > nul

cd %cur_dir%
goto done

:error
echo Folder was not found.

:done

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

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

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

Thank you very much for all who are involved in that topic. This is very great from yours side to work on that kind of things. I'm very greatfull for your help!! Thank you very much.
Jon Finley
Honored Contributor

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

Glad to help, and welcome to the forum.

If you'd like to help other members with questions, and you're interesed in earning points, here's how:
http://forums1.itrc.hp.com/service/forums/helptips.do?#28

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

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

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

for /f %%i in ('cd') do set cur_dir=%%i


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 /Q *.* > nul
del /F /Q /AH *.* > nul

for /f %%i in ('dir /b /ad') do rmdir /S /Q "%my_folder%\%%i" > nul

cd %cur_dir%
goto done

:error
echo Folder was not found.

:done

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



- i've got message:
The system cannot find specified file.. why?????
kabucek
Advisor

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

ok i know what's wrong but i have question.. hehe again

why it's not deleting new Briefcase file??? any other files are deleted but not that one, why??
Jon Finley
Honored Contributor

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

If a file is in use, (in windows) it can't be deleted. The "My Briefcase" folder is supposted to be a system folder for file transfer (sync'ing of off-line files).

Windows probably believed that it was in use, or was flagged as a system folder so as not to be deleted.

Try running "attrib" on the folder, or view the folder attributes through Windows Explorer.

Jon
"Do or do not. There is no try!" - Yoda
Vibhor Kumar Agarwal
Esteemed Contributor

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

The system cannot find specified file:

This error appears when you are trying to do something with a file, and the system cannot find the file in the path.

Just check whether the file is present form where you are running hte command.
Vibhor Kumar Agarwal