how to implement delete code in batch file? - batch-file

I have created batch file which starting & stopping celery services.
I want to implement code in batch file so that files older than week will be deleted.
celery_restart.bat
NET STOP "CeleryBeat"
NET START "CeleryBeat"
NET STOP "Celery2"
NET START "Celery2"

For Windows:
ForFiles /p "your\folder\with\logs" /m *.log /d -7 /c "cmd /c del #file"
Add /s to delete log files in subfolders as well. If you want to delete all files in the folder older than 7 days, just remove the /m *.log option.

Or you can just run some for loops:
#echo off
set comms="CeleryBeat" "Celery2"
for %%i in (*.pid *.log do del %%i /Y
for %%a in (%comms%) do (
net stop %%a && net start %%a
)
the && operator here will start the service only if the previous command exited with errorlevel 0

Related

Batch Delete excluding files under specific folder (or file names with specific pattern /wildcard e.g. Test123.csv, Test623.csv, Test854.csv)

FORFILES /P "C:\Temp\" /D -3 /S /C "cmd /c if #isdir==FALSE del /F /Q #path"
above script is working fine and delete all files under Temp and its subdirectory older than 3 days.
I want to exclude all files from specific folder say all files from folder XYZ or full path-> C:\Temp\ABC\XYZ
Note : all files under XYZ folders are having pattern say Test*.*.csv
forfiles does not have an exclude option. You have to use something like findstr /V to exclude the results, but that will not form part of the forfiles command in itself. We simply incorporate a for loop and ecxlude using findstr /V, then delete:
#echo off
for /f "delims=" %%i in ('FORFILES /P "%temp%" /D -3 /S /C "cmd /c if #isdir==FALSE echo #path"^| findstr /VI "C:\Temp\ABC\XYZ"') do del /F /Q "%%~i"

using forfiles to delete files

I have a simple line in a bat file. The line works perfect in a command window but when I add it to a bat file it does not do anything. The line that I am using in below.
forfiles -p E:\Apps\data\Upload-Copy -s -m *.* /d -120 /c "cmd /c del #file"
My goal is that I have multiple locations that I would like to add to this bat file and set up a scheduled task to run it daily.
I have also tried this and it didn't work either.
ForFiles /p "E:\Apps\data\Upload-Copy" /s /d -120/c "cmd /c del /q #file"
I have tried with no luck again this works just by typing in Command prompt but not in the .bat file
%SystemRoot%\System32\forfiles.exe /P "E:\Apps\data\Upload-Copy" /S /C "%SystemRoot%\System32\cmd.exe /D /C 0x22If #IsDir==FALSE Del /F /A #File0x22" /D -120
I have the bat file stored in the root of the C:drive and verified that the path is correct. I am just double clicking on the file C:\File2Delete.bat and the command prompt flashing on the screen for a second or less but does not delete any of the files
this will be on Windows 2008 R2 server and Windows 2019 server

Batch script - dir /b is including directory names and empty lines in output file

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.

Why does my single line batch file require user input on deleting files on a drive?

I am trying to create a batch for a scheduled task to purge a drive every 6 days. The goal is to have it fully automated so I can schedule it and walk away. For some reason my batch keeps asking for user input (y/n) which defeats the purpose.
REM Friday Purge
forfiles /p "G:" /s /m *.* /c "cmd /c Del G:"
Why does my batch file with just a single line (without remark line) require user input on deleting files on drive G: on execution?
The /F switch will force delete a read-only file.
The /Q will prevent the message from popping up.
The /S will delete all files in directories as well.
THE RD will remove a folder.
Therefore; your new code should look like this:
del /f /q /s *.*
del /f /q /s G:
BONUS
If you want this to be automated:
for %%i in (Mon) do (
if "%date:~0,3%"=="%%i" goto yes
)
goto endlocal
:yes
del /f /q /s G:
del /f /q /s G:\*.*
:endlocal
This will check if the day of the week is Monday, and do this once very 7 days. This checks the day and does the schedule part for you; so set it to run everyday.

Nest if exist statement within for statement in batch file

Good morning guys. I have a batch file that searches network PC user folders for XML files, then copies those files to a network location. However, it copies folders for all users, regardless of whether they have the files in question. What I would like to do is only make a folder for that user if the directory in which those files are contained exists.
Here is what the code looks like:
for /d %%g in ("\\computer.domain\c$\users\*") do (
xcopy /d /q /c /i /y "%%g\AppData\Roaming\Program\Folder\*.xml" "\\server.domain\shared_folder\%%g"
)
It works perfectly but for creating a user folder for every user. What I would like to do is only create a folder for users where Appdata\Roaming\Program\Folder contains XML files in the first place. I can't figure out how to get an if exists statement to work within that for statement though.
Also, this creates a directory structure like computer.domain/c$/users/user. If there is a way to strip out the c$/users (so it's computer.domain/user/file.xml), that would be even more amazing.
Thanks in advance!!
(Update) Here is the full code the batch is using. It repeats for each computer:
net use k: \\computer.domain\c$\users
for /d %%g in ("k:\*") do (
forfiles /P "%%g\AppData\Roaming\Program\Folder" /m *.xml /D -7 /C "cmd /C del #path"
)
net use k: /delete
for /d %%g in ("computer.domain\c$\users\*") do (if exist "%%g\Appdata\Roaming\Program\Folder\*.xml" (
xcopy /d /q /c /y "%%g\AppData\Roaming\Program\Folder\*.xml" "\\server.domain\shared_folder\%%~nxg\")
)
forfiles /P /S "M:\MAS500\endicia-postback-archive" /m *.xml /D -60 /C "cmd /C del #path"
The entire functionality is to delete XML files a week or older, then copy the remaining files, then delete files from the server that are two months or older. That line is only at the end, but the other lines are repeated for each computer.
Perhaps there is a better way to go about it? When I run it this way, it seems like I don't get a folder for all users that meet the criteria.
Thanks again!
Test this after altering the paths:
Just commenting here that your use of the /d switch will limit the files being copied.
#echo off
for /d %%g in ("\\computer.domain\c$\users\*") do (
if exist "%%g\AppData\Roaming\Program\Folder\*.xml" (
xcopy /d /q /c /y "%%g\AppData\Roaming\Program\Folder\*.xml" "\\server.domain\shared_folder\computer.domain\%%~nxg\"
)
)

Resources