user3075679
user3075679

Reputation: 1

I need a .bat file to delete pdf files

I need to write a bat file that will within a certain folder, delete any pdf file older than 48 hours with a file name that DOESNT have _TTO or _OR anywhere in the filename. Can anyone assist?

Upvotes: 0

Views: 1242

Answers (3)

NuclearBatchCode
NuclearBatchCode

Reputation: 1

Simply With del But You Can Try Do It Like This:

@echo off
set /p filedel=Enter File Location:
set /p filenamedel=Enter File Name:
cd %filedel%
del %filenamedel%
echo Done
pause
exit

Some What Works For Me (TESTED ON WINDOWS 7 HOME PREMIUM,WINDOWS XP PRO,WINDOWS 2000 PRO SERVICE PACK 4,And WINDOWS 98 SE) EDIT: You Might Want To Add The Code Below Too:

:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0
:--------------------------------------

EDIT2:That Was A Start ;D

Upvotes: 0

foxidrive
foxidrive

Reputation: 41257

Here's alternative that works in my test:

@echo off
pushd "d:\folder" && (
for /f "delims=" %%a in ('forfiles /m *.pdf /d -2 ^|findstr /i /v "_TTO _OR" ') do del %%a
popd
)

Upvotes: 1

MC ND
MC ND

Reputation: 70941

Not tested, at this moment i can't. Adapt as needed and when the list of files is correct, remove the echo to delete the files.

set "folder=c:\somewhere"

(  forfiles /p "%folder%" /m *.pdf /d -2 /c "echo @path" 
)| for /f "tokens=*" %%f in ('findstr /v /r /c:"\\[^\\]*_TTO[^\\]*$" /c:"\\[^\\]*_OR[^\\]*$" ') do (
    echo del "%%~f"
)

It uses forfiles to select the list of pdf files older than 2 days inside the defined folder, and filters the list for the filenames not containing any of the indicated substrings.

Upvotes: 0

Related Questions