I was running batch file script to delete the files in particular folder.the problem is how can i log the files deleted and the date time it is getting deleted in a log file with log file nameappended with date.
set folder="C:\test"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
#echo off
setlocal
set "YMD="
FOR /F "skip=1 tokens=1,2,3" %%A IN ('WMIC Path Win32_LocalTime Get Year^,Month^,Day /Format:table') DO (
if not defined Y set "YMD=%%B%%A%%C"
)
set folder="C:\test"
break>%YMD%.log
for /F "delims=" %%i in ('dir /b %folder%') do (
echo "%%~fi" deleted at %YMD% >>%YMD%.log
rmdir "%%i" /s/q || del "%%i" /s/q
)
?
Related
echo off
for /d /r "c:\" %%a in (TemporaryFolder) do if exist "%%a" echo Removing %%a & rmdir /s /q "%%a"
This for loop deletes every directory called "TemporaryFolder" from the C:\ drive. How would I go through every mounted volume (ie. A:\ - Z:) to delete the directory "TemporaryFolder"?
Edit (This test didn't work):
#echo off
echo Deleting Temporary Folders... Please be patient.
For /F "Tokens=*" %%A In ('MountVol^|Find ":\"') Do For /F "Delims=" %%B In ('Dir /B/S/AD-S-L "%%ATemporaryFolder" 2^>Nul') Do RD /S/Q "%%B" 2>Nul & echo Deleting %%B
echo Successfully deleted.
pause
Based upon my comment, here's an example of a nested For loop, which should do as asked:
For /F "Tokens=*" %%A In ('MountVol^|Find ":\"') Do For /F "Delims=" %%B In ('Dir /B/S/AD-S-L "%%ATemporaryFolder" 2^>Nul') Do RD /S/Q "%%B" 2>Nul
The outer For loop runs the MountVol command which returns the mounted drive paths as %%A. The nested For loop performs the recursive directory search for those named TemporaryFolder located within %%A.
Edit
Here's a multiline version of the same routine with added messages:
#Echo Off
Set "objFolder=TemporaryFolder"
Echo Please be patient...
For /F "Tokens=*" %%A In ('MountVol^|Find ":\"') Do (
Echo Deleting directories named %objFolder% from %%A
For /F "Delims=" %%B In ('Dir /B/S/AD-S-L "%%A%objFolder%" 2^>Nul') Do (
Echo Deleting %%B
RD /S/Q "%%B" 2>Nul && Echo Successfully deleted %%B
)
)
Pause
You should only modify the objFolder name on line 2.
Instead of MountVol, this one uses PowerShell to get the drives, and matches System and Reparse Points too, (take account that you'd need appropriate permissions to remove system directories):
#Echo Off
Set "objFolder=TemporaryFolder"
Echo Please be patient...
For /F "Tokens=*" %%A In (
'PowerShell -NoP "GDr -P FileSystem|?{!$_.Used -Eq ''}|Select -Exp Root"'
) Do (Echo Deleting directories named %objFolder% from %%A
For /F "Delims=" %%B In ('Dir /B/S/AD "%%A%objFolder%" 2^>Nul') Do (
Echo Deleting %%B
RD /S/Q "%%B" 2>Nul && Echo Successfully deleted %%B
)
)
Pause
Batch
#echo off
set folder="c:\FTP\"
set keep="keep1"
set keeptwo="keep2"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (
if /i "%%~ni" NEQ %keep% if /i "%%~ni" NEQ %keeptwo% (rmdir "%%i" /s/q || del "%%i" /s/q)
)
pause
Situation
folder1/file1.txt
folder2/file1.txt
keep1/file1.txt
keep2/file1.txt
file1.txt
Expected result
I need to keep "keep1" and "keep2" folders and all included files, but "folder1" and "folder2" and "file1.txt" with all subdirectories and files must be deleted.
Current result
It removes all files in all folders, removes "folder1" and "folder2", and keeps "keep1" and "keep2"
Any clue what I'm missing.
You cannot use the /S option with the DELETE command as that will delete the file in the current directory and all subdirectories.
Regardless of that, this is how I would accomplish the task so that you don't get the error from the RMDIR command. I use an IF EXIST command to determine if it is a file or directory.
#echo off
set "folder=c:\FTP\"
set "keep=keep1"
set "keeptwo=keep2"
cd /d %folder%
for /F "delims=" %%G in ('dir /b') do (
if /I NOT "%%G"=="%keep%" (
if /I NOT "%%G"=="%keeptwo%" (
REM check if it is a directory or file
IF EXIST "%%G\" (
rmdir "%%G" /s /q
) else (
del "%%G" /q
)
)
)
)
I'm assuming that this is what you wanted to do:
#Echo Off
Set "folder=C:\FTP"
Set "keep=keep1"
Set "keeptwo=keep2"
CD /D "%folder%" 2>Nul || Exit /B
Del /F/A/Q *
For /D %%A In (*) Do If /I Not "%%A"=="%keep%" If /I Not "%%A"=="%keep2%" RD /S/Q "%%A"
Pause
How to save a log .txt file of all deleted files with this code?
Also I need to change code to search for all backup folders in root directory subfolders.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "BackupFolder=D:\backup"
set "LastDate="
for /F "delims=." %%I in ('dir "%BackupFolder%\????????.*" /AD /B /ON 2^>nul') do (
if not "!LastDate!" == "%%I" (
for /F "skip=2 delims=" %%D in ('dir "%BackupFolder%\%%I.*" /AD /B /O-D-N /TC') do rd /Q /S "%BackupFolder%\%%D"
set "LastDate=%%I"
)
)
endlocal
This code was written by Mofi and posted as answer on How to remove oldest folder(s) of a group of folders in a folder with several folder groups? He posted also an enhanced version logging the files before deletion, but it was not working for me properly.
I tried with the followed command >> but log.txt file has not been generated.
The attempt to use >> was not shown in the question. How about something like:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "BackupFolder=D:\backup"
set "LastDate="
SET "LOGFILE=%TEMP%\%~n0.log"
for /F "delims=." %%I in ('dir "%BackupFolder%\????????.*" /AD /B /ON 2^>nul') do (
if not "!LastDate!" == "%%I" (
for /F "skip=2 delims=" %%D in ('dir "%BackupFolder%\%%I.*" /AD /B /O-D-N /TC') do (
DIR /S /B "%BackupFolder%\%%D" >>"%LOGFILE%"
rd /Q /S "%BackupFolder%\%%D"
set "LastDate=%%I"
)
)
)
endlocal
I have a directory that has a 10 sub-directories. Each of these holds different .bak files. I'm trying to create a script that will check to see if X number of files are there and if the number of files exceeds X, it deletes the oldest file. In other words, I want 20 iterations of a .bak file. When the 21st one shows up, I want the batch file to delete the oldest one.
Is this possible?
If so, can I create a single script that looks in all the sub-directories?
Thanks in advance.
Two options included. The first one will leave %maxFiles% bak files under each of the folders. The second one (windows Vista or later OS is required as robocopy is used to obtain the sorted list of files) will leave %maxFiles% in total
#echo off
setlocal enableextensions disabledelayedexpansion
set "rootFolder=%cd%"
set "maxFiles=20"
rem Option 1 - Keep %maxFiles% inside each of the subfolders
for /d /r "%rootFolder%" %%z in (*) do for /f "skip=%maxFiles% delims=" %%a in (
'dir /tc /o-d /a-d /b "%%~fz\*.bak" 2^>nul'
) do echo del "%%~fz\%%~nxa"
echo ------------------------------
rem Option 2 - Keep %maxFiles% in total under all the subfolders
for /f "skip=%maxFiles% tokens=2,*" %%a in ('
robocopy "%rootFolder%" "%rootFolder%" *.bak /l /nocopy /is /s /njh /njs /ndl /nc /ns /ts
^| findstr /v /r /e /i /c:"%rootFolder:\=\\%\\[^\\]*"
^| sort /r
') do echo del "%%b"
del commands are only echoed to console. If the output is correct, remove the echo command to remove the files
assumes that you want to check the number of files from the parent directory.Can be done also for each sub-directory.
#echo off
setlocal
set "max_number_files=20"
set "parrent_dir=c:\whatever_you_need"
set "extension=.bak"
pushd "%parrent_dir%"
set "count=0"
setlocal enableDelayedExpansion
for /f "delims=" %%a in ('dir /s /b /a:-d /o:-d /t:c *%extension%') do (
set /a count=count+1
if 1!count! GTR 1!max_number_files! (
rem --- remove the echo to activate deletion
echo del /q /f "%%~a"
)
)
popd
endlocal
endlocal
This will check each folder under d:\base\folder and if there are more than 20 *.bak files it will remove the oldest ones so only 20 *.bak file remain, in each folder.
Test it on some sample folders.
#echo off
for /d /r "d:\base\folder" %%a in (*) do (
pushd "%%a"
for /f "skip=20 delims=" %%b in ('dir /b /a-d /o:-d *.bak ') do del "%%b"
popd
)
I have a folder with all jpg and this is format 1UYK08HJ_20140403165858071_SYPTE1-PC but i want it rename like this
1UYK08HJ_SYPTE1-PC_20140403165858071.jpg. but when i run this script it always ends up with
.%~nA_1UYK08HJ.jpg.jpg.jpg
this is my batch script..
#echo off
pushd "C:\Users\IT-Administrator\Desktop\export" || exit /b
for /f "eol=: delims=" %%F in ('dir /b /a-d *_*.jpg') do (
for /f "tokens=1* eol=_ delims=_" %%A in ("%%F") do ren "%%F" "%%~nA_%%B%%~xF"
)
popd
This should do it:
#echo off
pushd "C:\Users\IT-Administrator\Desktop\export" || exit /b
for /f "tokens=1-4 delims=_." %%A in ('dir /b /a-d *_*.jpg') do (
Echo ren %%A_%%B_%%C.%%D %%A_%%C_%%B.%%D
)
popd
Remove the echo when you see correct output.
In the (Window's) folder's command line(cmd) run
ren *.* *.jpg