Delete all files in \bin directories using the Windows command line - windows

I want to clean up my binary files in my development directory. Is there a way I can recurse through the directory structure and delete all files that are in a bin directory using the Windows command line? (I am using Windows 7.)
Per Nathan, I tried to make a batch foo.bat:
#ECHO OFF
for /R %%x in (.) do call:myExistFunc %%x
GOTO:EOF
:myExistFunc
if exist %~1\.\bin call:myDeleteFunc %%~1\bin
GOTO:EOF
:myDeleteFunc
echo. Deleting files from %~1
del %~1*.* /Q
...and this worked.

Create a batch file that contains the following:
for /R %%x in (bin) DO del "%%x\\*.*" /Q
This will recursively walk through all child directories (from the current directory) and delete all files from every BIN folder. You could change the *.* to whatever file type you'd like to delete.

This should work in a batch file:
for /R %%x in (bin) do if exist "%%x" del /q "%%x\*.*"
If running this command directly from the command line, replace all instances of %%x with just %x.

It's been awhile since I've had use any DOS but..
DEL BIN\*.*
That should delete the files in the directory named BIN below the current directory.

You should write a bat file:
for /r %%f in (*) do 'WHATEVER YOU WANT TO DO'

Related

Batch file to run all .bat files in folders and sub-folders with different names

I need a simple script to run all .bat files in folders and sub-folders with their different names. here is an example :
Main Folder> Bat Folder 1> Bat File 1.bat
Main Folder> Bat Folder 2> Bat File 2.bat
. . .
Main Folder> Bat Folder N> Bat File N.bat
There are many topics out there asking the same question but the one that really worked for me was the following :
#echo off
pushd C:\Users\%USERNAME%\Desktop\Bat Folder 1\
for /f "delims=" %%x in ('dir /b /a-d *.bat') do start "" "%%x"&timeout /t 2 >nul
popd
However the problem is the direct folder address. I can't manually enter the folder names and run them one by one. it would take a long time. I want the script to go through all folders and sub-folders ignoring their names and run them all. it would be better to have the script run inside the main folder instead of the folder address !
Not a full answer, but I think this might help give you a start:
for /r "C:\Users\%USERNAME%\Desktop" %%a in (*.bat) do start "" "%%~a"
You might need call "something.bat" or start "" cmd /c "something.bat"
I suppose, if your batch files need a working directory, you could try this:
:: Make sure you're in the correct drive
c:
for /r "C:\Users\%USERNAME%\Desktop" %%a in (*.bat) do (
cd "%%~pa"
call "%%~a"
)
pushd "\Path\To\Main Folder"
for /r %%F in ("*.bat") do start /wait "" "%%~F"
for /r will iterate recursively over all *.bat files in all subdirectories to any depth.
I found an alternative. I used the code dir /s /b *.bat > runall.bat to get the full directory list and then replaced them all with pushd C:\Users\%USERNAME%\Desktop\Bat Folder 1\Bat File 1.bat
It's definitely not a good way to go but at least it fixed my issue.
Edit: It simply doesn't work. just reads the first line and ignores the rest.

DEL *.fileType not working for local directly

I am having some trouble deleting local files. The point is to clean up the solution directory of trash files. Here is what I have for a batch:
del /F /S /AH *.exe
del /F /S /AH *.ilk
del /F /S /AH *.pdb
del /F /S /AH *.sdf
The *.sdf file is in the same directory as the batch file. The rest of the files are in the Debug folder (which is generated by Visaul Studio). I don't want to delete all the files in the Debug folder, just those types specifically specifically. If I understand correctly, the \S specifies that I want the script to go into subfolders as well.
When I run the batch script, I get:
Could not find [ProjectLocation]\*.exe
for each of the the file types
Any help is greatly appreciated!
You can do that cleaning task with a single line:
#del /A /F /Q /S *.exe *.ilk *.pdb *.sdf >nul 2>nul
This command deletes all files with one of the 4 specified file extensions even if read-only or hidden in current directory and all subdirectories without any message being displayed.
PS: Care should be taken that this command is not executed on wrong directory tree.

Batch File to execute all .exe in a folder

I need to create a batch script that will run all .exe files in a folder. This must include subfolders.
I am running windows 7, and the batch file is stored in the root folder
I have tried several variations with no success. The two main variations are as follows
REM dir *.exe /S /B > tmpFile
REM set /p listVar= < tmpFile
REM del tmpFile
FOR %%F IN (%listVar%) DO %%F
=======================================
FOR /F "usebackq" %%F IN ('dir *.exe /S /B') DO %%F
I have looked through several variations on the above methods but none work. The top version (which I would like to avoid) only reads in one line from the file. The Bottom version outputs an unmodified "dir" command in the prompt window.
Ultimately I would like help identifying how to solve this issue without the need for a temp file.
for /r "." %%a in (*.exe) do start "" "%%~fa"
This will start all the executable files under the current folder and subfolders. Change the reference to the current folder (".") to your needs.
for %1 in (%USERPROFILE%\*.exe) do "%1"
This will start all executables in your user folder. Of course, that includes installers and stuff like that, and you probably don't want to reinstall all your apps, so replace %USERPROFILE% with the directory you want.

Windows batch command to delete files from a folder except one file

I am trying to create a batch script to move a set of files from one folder (root) to another and delete files of extension .dll in the root folder except one file. The command I tried is able to copy but not delete the files.
MOVE D:\mpgdata\sync\*.txt D:\data\sync\QDB_TXT_FILES
for %%i in (d:\data\sync*.dll) do if not "%%i"=="work.dll" del /f "%%i"
It had a case sensitive compare. /i fixes that.
There was also a missing backslash.
The %%~nxi makes it compare the filename only.
MOVE "D:\mpgdata\sync\*.txt" "D:\data\sync\QDB_TXT_FILES"
dir d:\data\sync\*.dll /b
pause
for %%i in (d:\data\sync\*.dll) do if /i not "%%~nxi"=="work.dll" del "%%i"

dos command to delete a folder except the named sub folders

Assume I have a directory in your computer under C:/dir1
And inside "dir1" we have more directories dir11, dir12, dir13,
And in each of the above directories we have more like below
dir11- dir111, dir112 dir113
dir1-dir121, dir122, dir123
dir13 - dir132,dir132,dir133
Now I need to find a command or a small script, which can delete everything under dir1 except couple of directories say dir122 and dir132.
Can you find something using DOS commands ?
You can use dir filespec /b >tmp.bat to list the file names in a text file called tmp.bat. Then edit that file to add del before each file name you want to delete. You can remove the file names you want to keep from the batch file and then do a "change all" to add del to each line. When it is edited properly, execute the batch file.
Alternatively, you could write a quick program under vb.net to do it.
Using the given examples: this should echo all the rd commands for the other folders.
#echo off
for /f "delims=" %%a in (' dir "c:\dir1" /b /s /ad ^| findstr /v /i "dir122 dir132" ') do echo rd /s /q "%%a"

Resources