bat file to delete containing folder when double clicked? - windows

What I would like to do in my batch file is delete ALL the contents of the containing directory AND the directory itself. Currently I am using the following command:
rmdir ..\dir /S /Q & exit
This works in that it deletes ALL of the contents in dir including the batch file but it fails to delete the directory, dir. Is there a way to do this while deleting the dir also? Essentially what I would like to do is create a batch script that would reside inside a ZIP file and deletes everything that gets created from unzipping the file. The above command still leaves behind an empty directory.

What about the following?
cd ..
rmdir .\dir /S /Q & exit
You can't delete a directory which is occupied by a running process.

Related

How to delete a directory from within that same directory with a batch file?

call :deleteSelf&exit /b
:deleteSelf
start "" /D "C:\Windows" /MIN cmd /c RD /S /Q "C:\Windows\test"&&exit /b
This is the code I use. Batch file running it sits within C:\Windows\test
The file is successfully deleted, along with any other files in the directory, but not the directory itself. Does anyone know some way to solve this issue? I'm rather stumped.
You will need, at least, to
leave the current batch file so it is not open
ensure your current active directory is not the one you want to remove
so, if you follow the already pointed dbenham's approach for leaving the current batch file you could use something like
((goto) 2>nul & cd "%~dp0\.." && rmdir /s /q "%~dp0")
That is,
the (goto) will generate an error that will leave current batch file execution
we change the current active directory to the parent of the folder where the batch file is stored
it the active directory has been changed, we try to remove the folder that holds the batch file
Of course, if there is another process/file locking the folder you will not be able to remove it.
surely it's not as easy as adding the following line to your batch file:
cd c:\
rd c:\windows\test

How to find and delete a file with CMD command?

I know the filename, say myfile.pdf. But I do not know its location because my web application created it temporarily. When the user disconnect from the application then I want this file to be deleted. So how to search and delete the file?
del path\filename /s
e.g.
del c:\test\h.bat /s
Will delete h.bat in any sub folders from the test directory.
To search and delete on an entire drive: -
del c:\test.bat /s
The S switch deletes all specified files from all subdirectories.
You can delete files by using this command.
rm [filename]

Command Line to call Batch File within a batch file in the correct directory

I have a batch file in c:\test.bat. The command in this batch file is:
C:
CALL test.bat
Inside my test.bat I have:
\Tables\batch.bat
The command line calls test.bat just fine, but when it gets to the the command \Tables\batch.bat it says:
The system cannot find the path specified.
If I change test.bat to the following it works just fine:
CD Tables
CALL batch.bat
But I can't change test.dat for other reasons. Is there a way to call the test.bat and make sure that my directory is the Tables directory? Test.dat has more than one command, so there is another one for \Sprocs\test2.bat
\Tables\batch.bat means file batch.bat is in a subdirectory Tables in root of current drive.
.\Tables\batch.bat or just Tables\batch.bat means file batch.bat is in a subdirectory Tables of current directory.
..\Tables\batch.bat means file batch.bat is in a subdirectory Tables of parent directory of current directory. In other words Tables is a directory parallel to current directory.
Therefore follow the advice of Liturgist and remove first \ to reference the file batch.bat in subdirectory Tables in current directory.
By the way:
C: switches the current drive to drive C:. But this does not define which directory is the current directory on drive C:. Use the following command to make a specific directory the current directory on drive C: independent on what is currently the current drive and current directory.
cd /D "C:\Path\To\Directory"
In case of batch file is also running and the directory containing the batch file should become the current directory use:
cd /D "%~dp0"
or
pushd "%~dp0"
For details on those two commands, open a command prompt window and run from within the window cd /? and pushd /? to get help for those two commands displayed. call /? explains %~dp0 (drive and path of argument 0 - the batch file).

Self-deleting batch file to delete a directory not fully deleting and not exiting on Windows 7

At uninstall time I run an executable to create a self-deleting batch file to do some directory cleanup. It always works great and has been for years.
I have run into a case where I need to do something similar to do some cleanup at install time. I have a different .Net console app that creates and runs a batch file that I am launching from setup.exe generated by InstallShield 2013. Here is the sequence of events:
Installer creates a directory and extracts some files, one of which is setup.exe.
An exe is run that ultimately runs setup.exe.
Setup.exe does the installation and just before exit I call LaunchApp to run my console app, DirectoryDelete.exe, with the directory to delete as an argument.
DirectoryDelete.exe creates a batch file in the parent directory of the directory to delete and runs it.
For example the resulting command line is:
DirectoryDelete.exe -dir "C:\Users\AUser\AppData\Local\Temp\STD1234.tmp"
The resulting batch file is:
#echo off
:repeatExe
del "C:\Users\AUser\AppData\Local\Temp\STD1234.tmp\setup.exe"
if exist "C:\Users\AUser\AppData\Local\Temp\STD1234.tmp\setup.exe" goto repeatExe
:repeatDir
rmdir "C:\Users\AUser\AppData\Local\Temp\STD1234.tmp" /s /q
if exist C:\Users\AUser\AppData\Local\Temp\STD1234.tmp\nul goto repeatDir
del %~F0&exit
So, basically the batch file waits on setup.exe to finish, then quietly deletes the directory, then itself.
However, what is happening is the contents of the directory get deleted, but neither the directory nor the batch file get deleted. It looks like the batch file is stuck in a loop - likely blocking itself. When I try to delete the directory myself (command line or explorer), it says another process is using it (the cmd.exe of the batch file). If I try to re-run the batch file, it is busy. If I kill the cmd.exe, then I can manually clean up.
Since this technique works so well at uninstall time, I am pretty confused about why it is not working during my cleanup at install time. Everything is running with elevated privileges. My best guess is it has to do with the context - the console application being launched from setup.exe.
Any help or ideas would be greatly appreciated.
EDIT:
I got the batch file to exit and delete itself, but the directory still did not get deleted. I modified the batch file by adding a few of lines:
#echo off
:repeatExe
del "C:\Users\AUser\AppData\Local\Temp\STD1234.tmp\setup.exe"
if exist "C:\Users\AUser\AppData\Local\Temp\STD1234.tmp\setup.exe" goto repeatExe
:repeatDir
if not exist "C:\Users\AUser\AppData\Local\Temp\STD1234.tmp\DeleteDirectory.exe" goto cleaned
rmdir "C:\Users\AUser\AppData\Local\Temp\STD1234.tmp" /s /q
if exist C:\Users\AUser\AppData\Local\Temp\STD1234.tmp\nul goto repeatDir
:cleaned
rmdir "C:\Users\AUser\AppData\Local\Temp\STD1234.tmp" /s /q
del %~F0&exit
My idea was to look for another file I knew was there. If it had been deleted, then I know the rmdir did part of its work. That way I could at least break the loop and take another shot at removing the directory, then clean up and get out.
What I learned there is that the rmdir is failing, thus creating an infinite loop in the original bat file. Now, if I run the batch file myself, even on a directory created by the install process, everything works fine....
For rmdir /s to not work would mean a file is in use, a file is locked or a permissions problem. Could there also be confusion about working directory?
I figured out what the problem was: The rmdir /s was failing because the current directory was set to the directory I was trying to delete. Apparently that is not the case during the uninstall operation.
I changed my batch file to move up to the directory DeleteDirectory.exe is launched from:
#echo off
pushd "%~dp0"
:repeatExe
del "C:\Users\AUser\AppData\Local\Temp\STD1234.tmp\setup.exe"
if exist "C:\Users\AUser\AppData\Local\Temp\STD1234.tmp\setup.exe" goto repeatExe
:repeatDir
rmdir "C:\Users\AUser\AppData\Local\Temp\STD1234.tmp" /s /q
if exist C:\Users\AUser\AppData\Local\Temp\STD1234.tmp\nul goto repeatDir
del %~F0&exit
Works fine now.

How can I do a batch file to copy and rename a file?

I am trying to create a batch file to copy a "folder" as a backup to a new destination. However, I need to add a date stamp of the file name.
for example folder is myFolder when I copy it I want to to be named myFolder_todays_date
I want to be able to copy the folder and it's content.
I found the xCopy but I am not sure how to write it correctly.
now I tried xCopy /e/i dir newDir
How can I append the date to the folder name?
I appreciated your help on how to write this batch file correctly.
Thanks
#echo off
mkdir %1%DATE%
xcopy /s %1 %1%DATE%
This accepts the name of the folder as a command line argument.
For example if the above batch file is called bkup.bat and you want to make a backup of a directory called work, you run it as
bkup.bat work
It copies it into the current directory. You can accept a 2nd command line argument (%2) & also use that if you want to copy to a different directory
xcopy /s %1 %2\%1%DATE%

Resources