Batch script to delete files - batch-file

I have a batch script as follows.
D:
del "D:\TEST\TEST1\Archive\*.TSV"
del "D:\TEST\TEST1\Archive\*.TXT"
del "D:\TEST\TEST2\Archive\*.TSV"
del "D:\TEST\TEST2\Archive\*.TXT"
del "D:\TEST\TEST 100%\Archive\*.TSV"
del "D:\TEST\TEST 100%\Archive\*.TXT"
The above code deletes all the ".txt" and ".tsv" files from all the folders except from the folder TEST 100%. For deleting the files from TEST 100% i am getting the error as The Path could not be found. I guess the % symbol in the folder name creates the issue.
Can anyone guide me to resolve the issue and to delete the files from the folder TEST 100%?

You need to escape the % with another...
del "D:\TEST\TEST 100%%\Archive*.TXT"

There's multiple ways of doing things in batch, so if escaping with a double percent %% isn't working for you, then you could try something like this:
set olddir=%CD%
cd /d "path of folder"
del "file name/ or *.txt etc..."
cd /d "%olddir%"
How this works:
set olddir=%CD% sets the variable "olddir" or any other variable name you like to the directory
your batch file was launched from.
cd /d "path of folder" changes the current directory the batch will be looking at. keep the
quotations and change path of folder to which ever path you aiming for.
del "file name/ or *.txt etc..." will delete the file in the current directory your batch is looking at, just don't add a directory path before the file name and just have the full file name or, to delete multiple files with the same extension with *.txt or whatever extension you need.
cd /d "%olddir%" takes the variable saved with your old path and goes back to the directory you started the batch with, its not important if you don't want the batch going back to its previous directory path, and like stated before the variable name can be changed to whatever you wish by changing the set olddir=%CD% line.

Lets say you saved your software onto your desktop.
if you want to remove an entire folder like an uninstaller program you could use this.
cd C:\Users\User\Detsktop\
rd /s /q SOFTWARE
this will delete the entire folder called software and all of its files and subfolders
Make Sure You Delete The Correct Folder Cause This Does Not Have A Yes / No Option

Consider that the files you need to delete have an extension txt and is located in the location D:\My Folder, then you could use the below code inside the bat file.
cd "D:\My Folder"
DEL *.txt

in batch code your path should not contain any Space so pls change your folder name from "TEST 100%" to "TEST_100%" and your new code will be
del "D:\TEST\TEST_100%\Archive*.TXT"
hope this will resolve your problem

Related

How to get the current path of my batch file? [duplicate]

I have a batch file that I intend to distribute to our customers to run a software task.
We distribute them as a folder or .zip with the files inside. Inside, there is the batch files and another folder with the files needed to run the batch.
Normally, when you make a batch, you type the path where the files are. But I won't know where the files are. The files will still be kept inside the master folder, but I need to have the batch find that folder to run the files.
So for example: If they have the master folder on the desktop and they run it, it would need to be something like "C:\Users\Username\Desktop" to run. You would have the batch CD to that location.
But what if they run it from documents? I don't know the username, so I have to somehow have the batch find this. Any code and/or instructions would be great.
There is no need to know where the files are, because when you launch a bat file the working directory is the directory where it was launched (the "master folder"), so if you have this structure:
.\mydocuments\folder\mybat.bat
.\mydocuments\folder\subfolder\file.txt
And the user starts the "mybat.bat", the working directory is ".\mydocuments\folder", so you only need to write the subfolder name in your script:
#Echo OFF
REM Do anything with ".\Subfolder\File1.txt"
PUSHD ".\Subfolder"
Type "File1.txt"
Pause&Exit
Anyway, the working directory is stored in the "%CD%" variable, and the directory where the bat was launched is stored on the argument 0. Then if you want to know the working directory on any computer you can do:
#Echo OFF
Echo Launch dir: "%~dp0"
Echo Current dir: "%CD%"
Pause&Exit
ElektroStudios answer is a bit misleading.
"when you launch a bat file the working dir is the dir where it was launched"
This is true if the user clicks on the batch file in the explorer.
However, if the script is called from another script using the CALL command, the current working directory does not change.
Thus, inside your script, it is better to use %~dp0subfolder\file1.txt
Please also note that %~dp0 will end with a backslash when the current script is not in the current working directory.
Thus, if you need the directory name without a trailing backslash, you could use something like
call :GET_THIS_DIR
echo I am here: %THIS_DIR%
goto :EOF
:GET_THIS_DIR
pushd %~dp0
set THIS_DIR=%CD%
popd
goto :EOF
You can also do
Pushd "%~dp0"
Which also takes running from a unc path into consideration.
Try in yourbatch
set "batchisin=%~dp0"
which should set the variable to your batch's location.

How to delete files matching a certain pattern in their name?

I'm creating a batch file that deletes all Rar$DIa0.??? folders in the %TEMP% directory.
Is this possible, and how to do it?
The ??? is three randomized numbers. And that's where I have trouble with - deleting all folders that have Rar$DIa0. in the name.
for /d is designed for just this type of use. Something like this should work (remove one of the % if you're testing from the command line):
for /d %%i in ("%TEMP%\Rar$DIa0.???") do rd "%TEMP%\%%i"
The /d makes it work on directory names instead of file names.
If you want to make it easier on yourself, change to the %TEMP% folder first:
pushdir
cd /d %TEMP%
for /d %%i in ("Rar$DIa0.???") do rd "%%i"
The ??? makes it only act on folders that have three letters after a .. If your folders don't have just a three letter extension, change .??? to .*. If you've got a typo, and there is no actual . in the foldername, just remove it and use Rar$DIa0??? or Rar$DIa0*
You may want to test it first by changing rd to echo to make sure you get the folders you want before actually deleting them.
For more information about for (pun intended) type for /? from a command prompt.
The command line to use in a batch file for this task is:
#for /D %%I in ("%TEMP%\Rar$DIa0.*") do #rd /Q /S "%%I"
Command FOR with option /D searches in folder defined by environment variable TEMP for subfolders with folder name starting with Rar$DIa0. not having hidden or system attribute set.
The loop variable I holds for each found subfolder matching this folder pattern the name of found folder with full path without double quotes although the path to temp folder very often contains 1 or more spaces.
For that reason just the command RD with the parameters /Q for quiet execution and /S for deleting also all subfolders in the specified folder must be called with referencing the current value of loop variable I enclosed in double quotes.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
for /?
rd /?
By the way: WinRAR deletes the temporary folders usually automatically, except a file is opened from within an archive for viewing/modifying it in another application and WinRAR is closed before the other application is exited with the opened file. In this case WinRAR can't delete the temporary folder with temporarily extracted file because the file is still opened in another application. Of course also command RD can't delete the temporary folder if this folder is still the current directory of another application or a file in this folder is still opened by another application with a read/write access lock.

Batch file to check if stated directories are empy and remove all files and documents

I'm looking for a batch file to clear a few directories of all files and folders, and make a report on if the directory was empty/if it was cleared.
I've looked online and tried a few suggestions, however none have worked. I can delete files but not folders, plus that doesn't use an IF statement to output if the directory was empty.
The contents of the directory will be random and have unknown files and folder names as we are using them as temporary locations that need to be deleted at the end of every day.
I have tried:
if EXIST "path\to\directory*.* " del "path\to\directory*.* "
But that doesn't delete folders.
Any help would be greatly appreciated!
Thanks
Maybe this Resolves the issue
#Echo off
Echo Beginning Cleaning Process
Echo Please Have patience
if not exist C:\path\to\file\file.exe goto End
if not exist C:\path\to\folder\folder goto End
if not exist C:\path\to\another\folder goto End
DEL C:\path\to\file\file.exe
DEL C:\path\to\folder\folder
DEL C:\path\to\another\folder
ALL Files Have Been Removed, Hopefully
pause
:End
Echo All Files Not Removed
Pause
If you want to remove all the Text files in the directory than you can use
DEL C:\Path\to\Text\file\*.txt
If you want to reomve all files in the folder then you can try this
Lets name the folder xyz which is in C:\True\xyz
RMDIR /S "C:\True\xyz"
MD "C:\True\xyz"
The RMDIR command will remove the directory and all the folders and files in it. Then the MD command will make a new directory with the same name and will obviously will be empty !

dos command delete folder and files using a wild card character

In a DOS batch command window, I want to delete folders (and corresponding files within that directory) with part of the name that contains the following string (SUB
I want to start at a specfic root directory called C:\app2\proc.
I want to delete the directory and the files contained within the directory.
I want to delete folders where part of the file name is (SUB.
What I have tried so far does not work.
Here is what I have tried to far:
del /f /s /q C:\app2\proc\*(SUB*
Note: the asterik before (SUB and the asterik after (SUB is not showing up in the display of what I have tried to far.
Thus can you tell me how to solve this problem?
You can use a for loop to run through your files and send a variable (%x in this case) to rmdir with your path. Try this:
for /d %x in (*(sub*) do rmdir /s /q c:\app2\proc\%x
You porbably need to escape that ( character so that it does not get evaluated.
Checkout http://www.robvanderwoude.com/escapechars.php it seems you should replace ( with ^(

How to perform reverse of relative path in DOS?

I want to delete some files from a folder, so I do this:
DEL "C:\Documents and Settings\name\My Documents\application\v10-105 CODE\v10-105 CODE\app\bin\Release\ExSimSeqNum-*.txt"
However, the folder "v10-105 CODE" changes very often (as version gets incremented).
In ../application/, there is only one folder and in that single folder, there are two folders.
Is there a way to generalize going down this path? I tried this:
DEL "C:\Documents and Settings\name\My Documents\application\v*\v*\app\bin\Release\ExSimSeqNum-*.txt"
but I get "The filename, directory name, or volume label syntax is incorrect."
Any suggestions?
try this:
C:
CD "C:\Documents and Settings\name\My Documents\application"
CD v*\v*
DEL app\bin\Release\ExSimSeqNum-*.txt
The reason is do not use * inside quote to match multiple characters, it won't work

Resources