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
Related
I run this script to list backup files older than 8 days and delete them in a server, and it behaving weird recently. Please see below
::Find backup files older than 8 days
::/p Specifies the path from which to start the search.
::/s Instructs the forfiles command to search into subdirectories recursively.
::/d Selects files with a last modified date within the specified time frame.
::/c Carries out the command specified by String and then stops.
::/b Displays a bare list of directories and files, with no additional information.
PUSHD \\SERVER_NAME\M$\MSSQL
FORFILES /P Backup /s /d -8 /C "cmd /c dir /B #file">K:\ScriptLogs\OutPut
POPD
This was working fine recently until I created more folders inside \SERVER_NAME\M$\MSSQL\Backup like FULL, DIFF, LOG as
\\SERVER_NAME\M$\MSSQL\Backup\FULL
\\SERVER_NAME\M$\MSSQL\Backup\DIFF
\\SERVER_NAME\M$\MSSQL\Backup\LOG
And the K:\ScriptLogs\OutPut file is now including the folder name FULL as below
FULL
SERVER_NAME_DB_DIFF_2021_06_10_040135.diff
SERVER_NAME_DB_DIFF_2021_06_11_040124.diff
SERVER_NAME_DB_DIFF_2021_06_12_040213_1.diff
I'm not able to understand why it is including FULL in the output list (if my cmd is wrong it should include DIFF and LOG too).
And when i modified the code as below
PUSHD \\SERVER_NAME\M$\MSSQL
FORFILES /P Backup /s /d -8 /C "cmd /c dir /A-D /B #file">K:\ScriptLogs\OutPut
POPD
It gives error as below, and output file is not including FULL now (as /A-D excluded Dir's)
K:\DBScripts>PUSHD \\SERVER_NAME\M$\MSSQL
W:\MSSQL>FORFILES /P Backup /s /d -8 /C "cmd /c dir /A-D /B #file" 1>K:\ScriptLogs\OutPut
File Not Found
File Not Found
W:\MSSQL>POPD
My concern here is why it is including FULL directory name in the older code version and the code run gives error with new version. Thanks for your help.
Thanks #compo ....
PUSHD \\SERVER_NAME\M$\MSSQL
FORFILES /P Backup /S /D -%NUMBER_OF_DAYS% /C "cmd /Q /D /C \"If #IsDir==FALSE For %%G In (#File) Do Echo %%~G\"" 1>K:\ScriptLogs\OutPut-%dtStamp%
POPD
This fixed the issue.
I've tried to create a batch file that will remove all files and sub-directories from a folder. The command I found that worked is this
FORFILES /p “X:\DAILY\1 MONDAY” /m *.* /c “cmd /c Del /F /Q #path” /d -7 /s & FORFILES /p “X:\DAILY\1 MONDAY” /S /D -7 /C “cmd /c IF #isdir == TRUE rd /S /Q #path”
Now my users tell me there is a Reference folder under 1 MONDAY that they don't want purged (but all other subdirectories should be emptied and deleted.) Can anyone advise how I might accomplish that?
Thank you!
for /f %%a in ("X:\Daily\1 MONDAY\*") do (if %%a neq "<filename you want to save>" (del %%a))
NOT YET TESTED
This should work though. Put the files you want to save in <filename you want to save>
It is extremely inefficient to use FORFILES for such a task because FORFILES has to start cmd.exe for each file found to delete the file older than seven days and for each folder to remove.
A much better solution would be:
#echo off
%SystemRoot%\System32\robocopy.exe "X:\DAILY\1 MONDAY" "X:\DAILY\1 MONDAY\WeeklyDelete" /E /XD "X:\DAILY\1 MONDAY\WeeklyDelete" "X:\DAILY\1 MONDAY\Reference" /MINAGE:7 /MOVE /NDL /NFL /NJH /NJS
if exist "X:\DAILY\1 MONDAY\WeeklyDelete\" rd /Q /S "X:\DAILY\1 MONDAY\WeeklyDelete"
ROBOCOPY searches
for all files in the directory X:\DAILY\1 MONDAY
and its subdirectories including empty directories because of option /E with
excluding the files in the two directories X:\DAILY\1 MONDAY\WeeklyDelete and X:\DAILY\1 MONDAY\Reference and all their subdirectories because of option /XD "X:\DAILY\1 MONDAY\WeeklyDelete" "X:\DAILY\1 MONDAY\Reference" with
excluding all files last modified in last seven days because of option /MINAGE:7.
The found files and folders matching theses criteria are moved to X:\DAILY\1 MONDAY\WeeklyDelete whereby the destination folder is automatically created on not already existing.
The options /NDL /NFL /NJH /NJS are for not printing the list of moved directories, list of moved files, the header and the summary.
A folder is moved only if being empty after moving all files matched by these criteria. So a folder is not moved on containing a file or subfolder.
The option /S instead of /E and the option /MOV instead of /MOVE can be used to move only files and do not move also folders being empty before or after moving the files.
The file and folder movements are done very fast by ROBOCOPY because of destination folder is on same drive as source folder which means just the file system of this drive must be updated and no file data must be moved at all.
An IF condition is used after ROBOCOPY finished with updating the file system to move the files and folders to verify if the destination folder X:\DAILY\1 MONDAY\WeeklyDelete exists. In this case command RD is used to delete this folder quietly because of option /Q and with all files and subdirectories because of option /S. This action is again just a file system update not really deleting file data stored on storage media and so is processed very fast.
Open a command prompt window and execute there the following commands to read more about the used commands in the three lines above.
echo /?
if /?
rd /?
robocopy /?
The commands are described also
by Microsoft with the documentations for the Windows Commands and
by SS64.com with A-Z index of Windows CMD commands and on many other websites.
Based upon your provided information, I would suggest this as your updated batch file:
#%__AppDir__%forfiles.exe /P "X:\DAILY\1 MONDAY" /C "cmd /C If #IsDir == FALSE 0x28Del /A /F #Path0x29 Else If /I Not #File == 0x22Reference0x22 RD /S /Q #Path" /D -7
That's it, nothing else to be added or modified.
I am trying to create a simple batch file to delete a folder and/or its contents for a user when they logoff or shutdown.
The GPO itself is working and running, I know this because it first creates a .txt document before running the delete commands.
batch file is called "delete.bat"
The GPO first copies over the batch file to C:\delete.bat. the GPO does this every time all the time.
it is set to run this file on either shutdown or logoff. right now it only runs on shutdown.
i have several versions of this file
echo.>"C:\Users\myactualname\testyoyo.txt
del "C:\Users\%username%\cpsi\data_4\*.*" /s /f /q
#RD /S /Q "C:\Users\%username%\cpsi\data_4\"
exit
.
#echo off
sleep 4
echo.>"C:\Users\myactualname\testyoyo.txt"
sleep 2
set fld=C:\Users\myactualname\cpsi\data_4\
for /f %%a in ('dir %fld% /b /a-d') do echo del "%fld%%%a"
for /f %%a in ('dir %fld% /b /ad') do echo rmdir /s /q "%fld%%%a%"
.
#echo off
echo.>"C:\Users\myactualname\testyoyo.txt"
#echo off
rem Delete all files and subfolders in directory for temporary files
rem of current user account, but keep the directory itself. Temporary
rem files and subdirectories currently in use are silently ignored.
del /F /Q "C:\Users\%username%\cpsi\data_4\*" 2>nul
for /D %%D in ("C:\Users\%username%\cpsi\data_4\*") do rd /Q /S "%%~D" 2>nul
rem Do the same as above for system temporary files directory.
rem This cleanup requires administrator privileges.
del /F /Q "C:\Users\%username%\cpsi\data_4\*" 2>nul
for /D %%D in ("C:\Users\%username%\cpsi\data_4\*") do rd /Q /S "%%~D" 2>nul
i also ran some other version which i haven't documented. I've tried hardcoding the file paths for testing and using the %username% wildcard.
on shutdowns it will create the txt file but not on logoffs. and at no time will it delete the files/folders.
the bat files all run fine if manually run.
i am pretty sure the file permissions are good.
however i suspect permissions is what i suspect is the problem.
thanks!!!
I am trying to delete all my temp files by running below batch file. but unfortunately, i am receiving "Access is denied." error. I am not sure on the root cause or how to minimize this.
#echo off
cd %temp%
for /d %%D in (*) do rd /s /q "%%D"
del /f /q *
Also suggest a way to delete all internet temp files(IE).
The way you're doing it is really dangerous:
cd without /D changes directory but not the drive. So if you run this script from drive D: it will change the current directory of drive C then will proceed deleting everything in your D: drive.
If you are lucky, you don't have permissions on your other drive. If you're not you lose all your files.
You would have needed cd /D %TEMP% to make it safe, but it's even better to avoid to change current directory:
#echo off
for /d %%D in (%TEMP%\*) do rd /s /q "%%D"
del /f /q %TEMP%\*
Of course, temp cleanup can fail because some files are in use by running programs. In that case, just reboot and run the script again.
I just ran this script and now there's 3 dirs and 10 files in my temporary directory (there were a lot of old files before)
To delete temporary IE files, it's slightly different as the directories are hidden so we have to list hidden dirs with a special dir command (the FOR command does not see the hidden directories)
#echo off
set IETEMP=%LOCALAPPDATA%\Microsoft\Windows\INetCache
for /F %%D in ('dir /AHD /B %IETEMP%') do rd /s /q "%IETEMP%\%%D"
del /f /q %IETEMP%\*
I'm trying to clear a folder of all it's contents but a certain sub-folder and its contents. My deleting works fine but I can't figure out how to exclude.
cd C:\testfolder
del * /S /Q
rmdire /S /Q "C:\testfolder"
But I do not want to delete the folder C:\testfolder\subf. How can I do this?
If you are using at least windows vista (robocopy command is used), this should do the job
#echo off
setlocal enableextensions disabledelayedexpansion
rem Create a temporary empty folder
set "tempFolder=%temp%\%~nx0.%random%%random%%random%.tmp"
md "%tempFolder%" >nul 2>nul
rem Purge from target folder anything not in the empty source folder,
rem but exclude the indicated folder
robocopy "%tempFolder%" "c:\testfolder" /nocopy /purge /xd "c:\testfolder\subfolder"
rem Cleanup
rmdir "%tempFolder%" /s /q >nul 2>nul
If you mean you want to delete all files but not the subfolder you can just use the following
cd C:\testfolder
del *.* /S /Q
This will delete any files in the "testfolder" and any files in the subfolders but will leave the subfolder there.