Delete all files after date, code is slow - date

I made a question last week about getting a batch file or code to delete all .txt files in a folder that were created before the last 60 days and I was directed to use the code below.
forfiles -p "J:\Test_Files" -s -m *.txt* -d 60 -c "cmd /c del #path"
This code does the job and works fine but it goes too slow deleting 250 files per minute. I need to delete a total of 2,600,000 files and this would take too long.
The code I used below deletes 200 files a second but deletes all .txt files
cd /BASE_PATH
del /s *.txt
How can I edit this code to delete files created before 60 days? I need it to delete at a faster pace.
Thank you for helping! :D

You can try with
#echo off
setlocal enableextensions disabledelayedexpansion
rem Configure script
set "target=J:\Test_Files"
set "fileMask=*.txt"
set "age=60"
rem We will use a temporary file.
for %%t in ("%temp%\%~nx0.%random%%random%%random%.tmp") do (
rem Send to temp file the list of matching files retrieved by the robocopy command
>"%%~ft" robocopy "%target%." "%target%." "%fileMask%" /minage:%age% /l /nocopy /is /s /njh /njs /ndl /nc /ns
rem Process temporary file deleting the selected files
for /f "usebackq tokens=*" %%f in ("%%~ft") do echo del "%%~ff"
rem Once done, remove the temporary file
) & del /q "%%~ft"
del commands are only echoed to console. If the output is correct, remove the echo command.

Related

Folder creation - from .bat file to powershell / sharepoint

I use a .bat file to create folders listed in two .txt files and also for deleting empty folders. All works fine but now I need the same to work on sharepoint.
Can anyone describe how or if this can be done? I have read that maybe I need to find put about powershell?
The .bat file looks like this:
echo off
%CHCP 1252
mkdir C:\Temp\MStruktur
Copy "P:\folderstructure\Niv1.txt" "C:\Temp\MStruktur"
Copy "P:\folderstructure\Niv2.txt" "C:\Temp\MStruktur"
cls
REM -----checking for empty characters
for %%a in (.) do set currentfolder=%%~na
if not "%currentfolder%"=="%currentfolder: =%" GOTO error
:home
echo "Folder structure menu:"
echo -------------------------------------
echo - 1 - create level 1
echo - 2 - create level 2
echo.
echo - S - Delete empty folders
echo.
echo - X - Exit
echo.
set /p web=Valg:
if "%web%"=="1" goto niv1
if "%web%"=="2" goto niv2
if "%web%"=="s" goto slet
if "%web%"=="x" goto slut
goto home
:niv1
cls
for /f %%i in (C:\Temp\MStruktur\niv1.txt) do mkdir %~dp0\%%i
echo.
echo Level 1 created
echo.
pause
exit
:niv2
cls
for /f %%i in (C:\Temp\MStruktur\niv2.txt) do mkdir %~dp0\%%i
echo.
echo Level 2 created
echo.
pause
exit
:slet
cls
for /f "delims=" %%d in ('dir %~dp0 /s /b /ad ^| sort /r') do rd "%%d"
echo.
echo Deleted empty folders
echo.
pause
exit
:error
cls
echo.
echo No blanks in folder name
echo.
pause
exit
:slut
RD /S /Q C:\Temp\MStruktur
exit

How do I configure mongodb to remove old log files?

I've read the mongodb docs and am rotating my log files on Windows 10. However, the number of logs grow until I manually delete them. I'd like to keep 30 days worth of logs.
I was hoping there was a config setting similar to NLog where I can specify how many files to keep before they are re-written. Maybe I am misunderstanding something in the docs. What is the "best practice" for automatically removing old log files?
Looks like the easiest way to do this without 3rd party utilities is to create a batch file that runs each night via Task Scheduler.
years later, but still helpful might be this cmd-script. It's written to remove old stuff in my profile, but really simple to extend:
#echo off
#setlocal
REM Remove everything older then "/D" days (ten/10 days in our examples below)
REM BUT we can not remove directories with files in it, so we do it in two-steps ways:
REM 1st remove all files by using the command: del
REM 2nd remove all files by using the command: rd
REM cleanup %TMP%
forfiles -p "%TMP%" -m * /D -10 /C "cmd /c del /s/q #path > nul 2> nul"
forfiles -p "%TMP%" -m * /D -10 /C "cmd /c rd /s/q #path > nul 2> nul"
REM cleanup %TEMP%
forfiles -p "%TEMP%" -m * /D -10 /C "cmd /c del /s/q #path > nul 2> nul"
forfiles -p "%TEMP%" -m * /D -10 /C "cmd /c rd /s/q #path > nul 2> nul"
REM cleanup the Downloads folder in the current profile
forfiles -p "%USERPROFILE%\Downloads" -m * /D -10 /C "cmd /c del /s/q #path > nul 2> nul"
forfiles -p "%USERPROFILE%\Downloads" -m * /D -10 /C "cmd /c rd /s/q #path > nul 2> nul"
#endlocal
#echo on

Batch file / Powershell script to zip files in folders

I have a lot of folders with files inside for example:
Folder1 with sample content:
File1.txt
File2.txt
File3.xml
File4.jpg
.
.
.
Result after zip: Folder1.zip
Folder2 with sample content:
File12.txt
File22.txt
File32.xml
File42.jpg
.
.
Result after zip: Folder2.zip
How to zip only all files (not entire folder) in every folder with result filename like "Foldername.zip". I found this code for /d %%a in (*) do (ECHO zip -r -p "%%~na.zip" ".\%%a\*") but it zips entire folders, not only files.
I solved the problem with the following steps:
create empty zip file
copy this file to all folders in "working folder"
rename all zip files with folder name
add .txt, .jpg, .xml files to zip.
This is a working code:
#echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('dir /A:D /B') do (
xcopy /y "Path_to_empty_zip\Test.zip" ".\%%~na\"
rename "%%~na\Test.zip" "%%~na.zip"
)
for /f "delims=" %%a in ('dir /A:D /B') do (
cd "Path_to_working_folder\%%~na\"
zip -r "%%~na.zip" "*.txt"
zip -r "%%~na.zip" "*.jpg"
zip -r "%%~na.zip" "*.xml"
)
pause
I found some similar problems without solutions because publish my solution as "answer on my question".

%%G from For Loop changing from index to %G using

I am trying to write a batch file to automate some routine Matlab processes. The batch file loops through from 0 to a set value (usually between 50 and 75) using the For /L structure. The script copies the main Matlab script to the subfolder and runs it. The batch would normally continue onward so I put a :loop to wait until the Matlab ends.
echo off
setlocal EnableDelayedExpansion
REM The format is matlab_auto.in (max value).
For /L %%G in (0,1,%1) do (
REM Sanity check
echo 1 %%G
REM Create Outputs folder if non-existent
if not exist Outputs md Outputs
REM Copy .m file into deg folder and cd to folder
copy values_calc.m %%Gsort\values_calc.m
cd %%Gsort
echo Got to folder
REM Running .m script and sanity check
echo 2 %%G
matlab -nosplash -nodesktop -noFigureWindows -logfile output.log -r "run('values_calc.m');"
echo 3 %%G
REM Waiting for matlab to finish
:loop
tasklist /fi "imagename eq MATLAB.exe" |find ":" > nul
echo 4 %%G
if errorlevel 1 goto loop
echo Finished Matlab
echo 5 %%G
REM Copy .m outputs into outputs folder, ignoring confirmation
copy Output_*.* ..\Outputs /Y
echo Copied outputs
REM Sanity check and return home
echo 6 %%G
cd %~dp0
echo Home again
)
The problem I'm having is that when it ends after the first iteration of the For loop. Echo 1, 2, 3 are 0. Echo 4 shows 0 the first time through :loop but then it shows %G for the remainder of the loops and at Echos 5 and 6. It also does not continue into further iterations of the For loop. I'm assuming this is because %%G is no longer a number (or in the range specified).
I have tried implementing a call subroutine to use the goto outside the loop but then it opens the Matlab dozens of times, crashing the computer.
Any insight or advice is appreciated. Thank you.
EDIT: Changed the :: for commenting to REM. It did not resolve this issue but looks better.
EDIT 2: I have a test case that demonstrates the problem. Its something with the :loop or goto.
echo off
setlocal EnableDelayedExpansion
for /l %%G in (0,1,5) do (
:loop
echo %%G
pause
if %%G==0 goto loop
)
Yes. The execution of a GOTO command cancel any active (pending) FOR or IF commands that may be nested inside parentheses at any level. This way, the commands placed below the :loop label are executed inside the FOR context the first time, but after the goto command they are executed as if they were placed outside the FOR loop! The way to solve this problem is extracting the code below the label into a subroutine and then call :loop in the FOR.
echo off
setlocal EnableDelayedExpansion
REM The format is matlab_auto.in (max value).
For /L %%G in (0,1,%1) do (
REM Sanity check
echo 1 %%G
REM Create Outputs folder if non-existent
if not exist Outputs md Outputs
REM Copy .m file into deg folder and cd to folder
copy values_calc.m %%Gsort\values_calc.m
cd %%Gsort
echo Got to folder
REM Running .m script and sanity check
echo 2 %%G
matlab -nosplash -nodesktop -noFigureWindows -logfile output.log -r "run('values_calc.m');"
echo 3 %%G
REM Waiting for matlab to finish
call :loop
echo Finished Matlab
echo 5 %%G
REM Copy .m outputs into outputs folder, ignoring confirmation
copy Output_*.* ..\Outputs /Y
echo Copied outputs
REM Sanity check and return home
echo 6 %%G
cd %~dp0
echo Home again
)
goto :EOF
:loop
tasklist /fi "imagename eq MATLAB.exe" |find ":" > nul
REM echo 4 %%G
if errorlevel 1 goto loop
exit /B

Build a command and execute it from within a batch file

I have a batch file that outputs a list of commands which themselves can be executed at the command line.
Here is a simplified version of what I'm doing:
set %foldername%="c:\my_folder"
set %exename%="c:\my_utility.exe"
cd %foldername%
FOR /F "tokens=*" %%G IN ('dir *.xml /s /b /a:-d') DO #echo %exename% /x="%%G"
This basically outputs a batch file. It looks like this:
c:\my_utility.exe /x="c:\my_folder\file1.xml"
c:\my_utility.exe /x="c:\my_folder\file2.xml"
c:\my_utility.exe /x="c:\my_folder\file3.xml"
c:\my_utility.exe /x="c:\my_folder\file4.xml"
I want to execute these commands. Currently I have to redirect the output to a batch file and then run that. Is there any way to just say "execute this command I just constructed" in the dos prompt?
Just remove echo in the FOR loop:
FOR /F "tokens=*" %%G IN ('dir *.xml /s /b /a:-d') DO %exename% /x="%%G"