Can we delete the folder too when deleting the file inside it using batch command.I have the file name buy not the folder name.So when my code deletes that file inside the folder i want the folder to be deleted as well,as its empty.
Code
String tmpfolder = System.getProperty("java.io.tmpdir");
Runtime.getRuntime().exec("cmd /c del "+tmpfolder+"IEDriver.dll /f /s /q");
This code deletes the file but not folder.
Runtime.getRuntime().exec("cmd /c \"for /f \"delims=\" %a in ('dir /a-d /b /s \""+tmpfolder+"IEDriver.dll\"') do rd /s /q \"%~dpa\" \"");
This uses dir command to search for the dll and removes the folder in where it is stored.
Not tested.
Related
So i have a dir called c:\user\jdoe\desktop\Folder1\
Inside folder1 i have many folders with subfolder and files.
I have a 7-Zip batch that zips all the folders in place.
So now i have subfolders and subfolders.zip inside folder1.
I need a batch that will detele all the non zipped folders but skip the zip files and the .bat file that lives in c:\user\jdoe\desktop\Folder1\
Any ideas?
If i can get a 7-zip batch that will zip fodlers (example subfolder1.zip subfolder2.zip) and then afterwards delete all non zipped folders that would be great.
Been all over the internet and after deleting half my data by testing my own scripts i have now decided to come here.
So after a long collaboration with a friend we came up with this.
Tested this and it is working 100%
REM create list of directories
SET cwd="C:\Users\user\Desktop\New folder"
SET date=date /T
CD %cwd%
DIR /AD /B >dirlist.txt
REM Zip contents of each directory
for /f "tokens=*" %%a in (dirlist.txt) do (
"c:\program files\7-zip\7z.exe" a "%%a.zip" "%%a"
del /s /q "%%a"
rmdir /s /q "%%a"
)
del dirlist.txt
pause
I have made a bat script as follows
cd "D:\ACT\ACTBKUP"
del /Q *.*
FORFILES /P E:\ACT_Backups /M *.zip /D +1 /C "cmd /c del #D:\ACT\ACTBKUP"
The script is supposed to delete everything in "D:\ACT\ACTBKUP" and then move the newest zip folder to that same folder from E:\ACT_Backups. When I run this script from windows server 2012 it just disappears.
thanks
In order to switch to a directory that is located on a different drive, you need to use cd /d instead of just cd. If you do not do this, the directory will not change.
When you run a script by double-clicking on it, batch considers the current directory to be the directory where the script is currently located. Since you are not using the /d option with cd, you are running del /Q *.* on the directory where the script is located.
To fix this, you have two options:
cd /d "D:\ACT\ACTBKUP"
del /Q *.*
or
del /Q "D:\ACT\ACTBKUP"
There is no option in forfiles to get just the most recent file; /D +1 will return all files with a last-modified date of today or later. In order to get the most recent file and nothing else, you will need a for /f loop:
rem /b returns just the file name and /o:d sorts the files by date
rem Since newest_file gets set for each .zip file in the directory,
rem the last file set will be the newest
for /f "delims=" %%A in ('dir /b /o:d E:\ACT_Backups\*.zip') do set newest_file=%%A
copy %newest_file% D:\ACT\ACTBKUP
I am trying to write a simple batch file which will recursively find and delete a folder. But the following script is not looking under sub folder. Wondering how to do that?
#echo off
cd /d "C:\"
for /d %%i in (temp) do rd /s "%%i"
pause
Thanks!
for /d /r "c:\" %%a in (temp\) do if exist "%%a" echo rmdir /s /q "%%a"
For each folder (/d), recursively (/r) under c:\ test for the presence of a temp folder and if it exist, remove it
directory removal command is only echoed to console. If the output is correct, remove the echo command
The /S switch to rd means
/S Removes all directories and files in the specified directory
in addition to the directory itself. Used to remove a directory
tree.
It does not mean it will search all directories looking for one with the specified name and delete them.
In other words, if you run rd /S Test from the C:\Temp folder, it will delete C:\Temp\Test\*.*, including all subdirectories (of any name) of C:\Temp\Test. It does not mean it will delete C:\Temp\AnotherDir\Test, because that isn't a subfolder of C:\Temp\Test.
I need the correct way to get a simple process to repeat till it reaches end of folder. Inside my Main Folder are multiple subfolders. Inside each of these are more subfolders along with a few files.
I need to run the batch from inside the Main folder and have it enter each subfolder in turn and simply run "dir *. > %date%.txt", then do the same to the next subfolder till all are done.
The only part I cannot get to work right is the change directory to each in turn till all are done.
Thanks
#echo off
for /f %%D in ('dir /b /s /ad') do (
pushd %%D
dir *. >"%date%.txt"
popd
)
to append to a existing file you should use >>, try this:
cd /d X:\main &rem put the path to your main folder here
for /r /d %%i in (*) do dir "%%~fi\*.">>"%date%.txt"
This reports all subfolder from X:\main and there subfolder recursively.
Try these options, if you weren't aware of them:
dir "c:\main folder">"%date%.txt"
dir /a-d "c:\main folder">"%date%.txt"
dir /b /s /a-d "c:\main folder">"%date%.txt"
dir /b /s /ad "c:\main folder">"%date%.txt"
I'm trying find a way to delete a specific folder name and delete all of its contents in Windows. So my D: drive has a 100 folders, inside each of these folders are sub folders called folder1, folder2, folder3, etc. I want to be able to run a command at the root of D: that will search through each 100 folders and delete say folder3, and folder9 and all of its contents.
RD /s /q "folder1"
The above command doesnt like to search through subdirectories.
Anyway of doing with with CMD or do I need a .vbs script or something?
Thanks!
You can use the FOR command to go through the list of subfolders from a given location and run commands on each subfolder. e.g.:
for /F "delims=\" %%I in ('dir /ad /b <someFolder>') DO (
cd "<someFolder>\%%I"
rd /S /Q "folder1"
rd /S /Q "folder3"
)
It would get the list of folders in someFolder, and delete the directories named "folder1" and "folder3" from each subfolder.
Based on the OPs comments, I believe the following will do the trick:
#echo off
for /f "delims=" %%F in (
'dir /b /s /ad "d:\My files"^|findstr /ie /c:"\folder1" /c:"\folder9"'
) do if exist "%%F" rd /s /q "%%F"