I have a folder, which has files and folders inside it like
C:/MyFolder
C:/MyFolder/File1.txt
C:/MyFolder/File2.txt
C:/MyFolder/File3.sql
C:/MyFolder/Folder1
C:/MyFolder/Folder1/File5.txt
What batch command do I need to use to delete all the folders and contents inside them without deleting files inside my folder. Example : Delete Folder1,Folder1/File5.txt but retain File1.txt,File2.txt and File3.sql?
This shows you the commands - if you are happy with them then remove the echo keyword and run it again.
#echo off
for /d %%a in ("C:\MyFolder\*") do echo rd "%%a" /q /s
pause
from command prompt:
for /f "tokens=* delims=" %a in ('dir /b /a:d "C:\someDir"') do #rd /s /q "%~fa"
from batch file:
for /f "tokens=* delims=" %%a in ('dir /b /a:d "C:\someDir"') do #rd /s /q "%%~fa"
For DOS/Command prompt use
for /d %F in ("path*") do rmdir /s /q "%F"
Use double % if you use it in a batch file.
for /d %%F in ("H:\EDGE-backup*") do rmdir /s /q "%%F"
I used this to backup the EDGE bookmarks and such, and since XCOPY always brings with it it's root directory subfolders i had to delete these after the copy.
The above worked for this. Result,only files remained in H:\EDGE-backup.
Related
I have a bat file which part of looks like:
set test=test
for /d %%x in (..\*.%test%) do xcopy "%%x" c:\path\%test%\%%x\ /S /E /F
xcopy ..\dir c:\path\%test%\dir\ /S /E /F
The for loop does not work, but the xcopy does. If I move the contents of the directory above to the current directory and change the code to remove the "..\":
for /d %%x in (*.%test%) do xcopy "%%x" c:\path\%test%\%%x\ /S /E /F
it works. Can someone please tell me why the bat script in the for loop cannot look up a directory? Am I approaching this wrong?
EDIT: I have now changed the command to after seeing ths answer but it still does not work:
for /d %%~nxx in (..\*.%MUI%) do xcopy "%%~nxx" c:\temp\%test%\%%~nxx\ /S /E /F
I receive the error:
%~nxx was unexpected at this time
EDIT #2:
I still cannot get it to work, my commands have looked like
for /d %%x in (..\*.%test%) do xcopy "%%~nxx" c:\temp\%test%\%%~nxx\ /S /E /F
for /d %%x in (..\*.%test%) do xcopy "%%x" c:\temp\%test%\%%~nxx\ /S /E /F
It can. But %%x will contain ..\xyz.test, not xyz.test, which is probably not what you want in your xcopy target.
Replace it with %%~nxx (for "Name and eXtension of x") to chop of the path.
for /d %%x in (..\*.%test%) do xcopy "%%x" c:\path\%test%\%%~nxx\ /S /E /F
On windows 7, How can i write a batch file to delete a folder which is inside another folder.
this 'another folder' name varies.
Eg: C:\Users\%USERNAME%\AppData\Local\Mozilla\Firefox\Profiles\xxxxxx\cache2
the 'xxxxxx' might change. Now, i want to delete the 'cache2' folder and all its contents.
i tried this:
:: Batch script to clear browsing history, download history, and empty the cache for Mozila Firefox.
:: Script can be run via GFI MAX RM
#echo off
TASKKILL /T /F /IM Firefox.exe
set DataDir=C:\Users\%USERNAME%\AppData\Local\Mozilla\Firefox\Profiles
del /q /s /f "%DataDir%"
rd /s /q "%DataDir%"
for /d %%x in (C:\Users\%USERNAME%\AppData\Roaming\Mozilla\Firefox\Profiles\*) do del /q /s /f %%x\*sqlite
start "" "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
cls
IF %ERRORLEVEL%==0 (
#echo "Success Message"
timeout 10
) ELSE (
#echo "Error Message"
timeout 10
exit 1001
)
but this is deleting the entire profiles folder.
can anyone out there please help me out with this.
for /d %%a in (
"C:\Users\%USERNAME%\AppData\Local\Mozilla\Firefox\Profiles\*"
) do if exist "%%~fa\cache2\" echo rmdir /s /q "%%~fa\cache2"
For each folder under the Profiles folder, if it contains a cache2 subfolder, remove it.
rmdir commands are only echoed to console. If the output is correct, remove the echo command
simple:
rmdir /s C:\Users\%USERNAME%\AppData\Local\Mozilla\Firefox\Profiles\xxxxxx\cache2
If you know that your cache2 folder is a direct child of xxxxxx, then MC ND has a good answer. But if cache2 can appear at any level, then the following works, providing that cache2 is not a direct decendent of "...\Profiles\".
#echo off
for /f "delims=" %%F in ('dir /b /ad /s "C:\Users\%USERNAME%\AppData\Local\Mozilla\Firefox\Profiles\cache2"') do echo rd /s /q "%%F"
If cache2 can be a direct child of "...\Profiles", then the following should work as long as there are no folders with a name like "cache2.x".
#echo off
for /f "delims=" %%F in ('dir /b /ad /s "C:\Users\%USERNAME%\AppData\Local\Mozilla\Firefox\Profiles\cache2.?"') do echo rd /s /q "%%F"
Both commands above simply ECHO the RD statements. Simply remove ECHO when all looks correct.
I need to write a .bat file that deletes all directories in a specified directory, but not files. How could I do that? Thanks in advance.
You could try something like
for /f %%d in ('dir /b /ad') do rmdir %%d
to delete all empty directories in the current working directory.
The /b switch gives just the summary, so just one entry per line.
The /ad switch gives only directories.
rd (or rmdir) deletes only empty directories by default.
Edit:
As deadlyDev pointed out, you could add /S /Q to RD to remove non-empty directories, resulting in
for /f %%d in ('dir /b /ad') do rmdir /s /q %%d
I want to find and delete every desktop.ini and Recycle Bin.BIN file on a network drive, H:, using a windows batch file. I currently have this:
#echo About to delete all desktop.ini and Recycle Bin.BIN files from H:, press Ctrl+C to cancel or Enter to continue.
#pause
#H:
#for /f "usebackq" %%i in (`dir /s /b /x /A:H ^| find "desktop.ini"`) do del /A:H "%%i"
#for /f "usebackq" %%i in (`dir /s /b /x /A:RH ^| find "Recycle Bin.BIN"`) do del /A:RH "%%i"
#echo Deleting complete, press any key to exit.
#pause
Which works but for any file in a sub-folder with a space in the name it fails with a "cannot find file" error. Any suggestions how to fix that?
solution that worked for me was
create bat file "delete_all_desktop_ini.bat"
with
del /s /q /f /a ".\desktop.ini"
put it in a folder and run it
it will delete all desktop inis in current directory and sub directories of this file.
i put it in a project folder that is in google drive
Give this a test:
I've altered the recycle bin name to what I see here in Windows 8.
The name changes with different versions of Windows.
#echo off
del /s /q /f /a "h:\desktop.ini"
del /s /q /f /a "h:\$Recycle.Bin\*.*"
The problem occurs because by default space is a delimiter for the for command, but you can change this using the delims option. If you pick a character that won't ever appear in a file path then it should work fine:
#echo About to delete all desktop.ini and Recycle Bin.BIN files from H:, press Ctrl+C to cancel or Enter to continue.
#pause
#H:
#for /f "usebackq delims=|" %%i in (`dir /s /b /x /A:H ^| find "desktop.ini"`) do del /A:H "%%i"
#for /f "usebackq delims=|" %%i in (`dir /s /b /x /A:RH ^| find "Recycle Bin.BIN"`) do del /A:RH "%%i"
#echo Deleting complete, press any key to exit.
#pause
for /r "H:\" %%a in (desktop.ini $Recycle.Bin) do if exist "%%~fa" echo del /f "%%~fa"
Try it, to make it working remove echo from the script.
del /s /q /f /a ".\desktop.ini"
it should works as charm
save file .bat
put it in any folder
it will delete ini files in folders and sub folders
I am trying to get all the newest file from subdirectories into one network directory with a command without getting the subdirectory structures. They are the SQL Server log files with extension of *.trn. I have the following but it doesn't work.
Trying to get only the newest *.trn files from ...........Backup and it's subdirectories.
for /R E:\SQLSERVER\PRODINSTANCE1\Backup %%f in (*.trn) do xcopy %%f "\\198.152.71.14\NetBackups$\MSSQL\Logs" /B /O:D /d /Y
You can use the dir command with the /od switch and a for loop:
#echo off &setlocal enabledelayedexpansion
for /d /r "E:\SQLSERVER\PRODINSTANCE1\Backup" %%a in (*) do (
for /f "delims=" %%i in ('dir /b /a-d /od "%%~a"') do set "newest=%%~fi"
xcopy "!newest!" "\\198.152.71.14\NetBackups$\MSSQL\Logs" /B /O:D /d /Y
)
For more help enter help dir in the command prompt.
I'm not sure if you're using xcopy correctly. /O doesn't take arguments. It only copies ownership/ACL info. (Is that really what you want for log files?)
Since you didn't describe what "doesn't work" means, my only suggestion is to hedge against file names with spaces in them.
FOR /R "%SRC_DIR%" %%f in (*.trn) do xcopy "%%~f" "%DEST_DIR%" /B /O /D /Y
This worked for me (I tested with .pdf's).