• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 614
  • Last Modified:

TempDrive autodelete script stopped/never did delete files by full name

Follow up to question: http://www.experts-exchange.com/Operating_Systems/MSDOS/Q_21811325.html

I am currently running the following script, the same one from the question above, and have noticed that it has either stopped or never did delete files or folders that don't follow the 8.3 naming convention.  Here's what i get when i run the script...

Could Not Find D:\TempDrive\kidzpix\kidzpix 017.jpg

If i try to manually delete the file from the cmd prompt, i get same error...however, if i put quotes around it, poof...works fine...

The "remove directory" part of the script removes empty directories fine, even ones with 2 words in them (New Folder), but i'm wondering if there is something wrong with the script or is this the common nature (or fixable error) of DOS?  i could have sworn i've deleted that way before...i know that i can do a change directory (cd) to a folder with a long filename, and even one with a space.  

Any help will be greatly appreciated...oh yah, here's the script

______________________________

@echo off

setlocal enabledelayedexpansion

set fileDir=%~1

REM **Sets default folder to be cleaned if none entered...
if "%fileDir%"=="" set fileDir=d:\tempdrive

call :GETPARTS "%date%

REM ** Here's where to change the days
call :SUBTRACTDAYS 7

set _CutOff=%yy%/%mm%/%dd%

pushd.

cd /d "%fileDir%"

call :PROCESSDIR

for /f "delims=" %%a in ('dir /b /s /ad') do call :PROCESSSUBDIR "%%a"

set sortKey=10000

if exist _temp.txt del _temp.txt>NUL

for /f "delims=" %%a in ('dir /b /s /ad 2^>NUL') do (echo !sortKey!%%a)>>_temp.txt&set /a sortKey+=1

if exist _temp.txt (
  for /f "delims=" %%a in ('type _temp.txt ^| sort /R') do call :CHECKDIR "%%a"
)

popd

goto :EOF

:CHECKDIR

set dirName=%~1
set dirName=%dirName:~5%

for /f "delims=" %%a in ('dir "%dirName%" ^| findstr /L /C:"0 File(s)"') do echo Removing: %dirName%&rd /Q "%dirName%"

goto :EOF

:PROCESSSUBDIR

pushd.

cd /d %1

call :PROCESSDIR

popd

goto :EOF

:PROCESSDIR

for /F "tokens=1-4*" %%a in ('dir /TC /a-d ^| find "/" ^| findstr /V /I DO_NOT_REMOVE_NtFrs') do call :CHECK "%%e" %%a

goto :EOF

:CHECK

call :GETPARTS %2

if /i "%yy%/%mm%/%dd%" GTR "%_CutOff%" goto :EOF

REM **This is where the "safety" is.  Remove the echo statement to arm.
del /f /a:s %1

goto :EOF

:SUBTRACTDAYS

set dayCnt=%1

if "%dayCnt%"=="" set dayCnt=1

REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100

:CHKDAY

if /I %dd% GTR 0 goto DONESUBTRACT

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yy=%yy% - 1

:ADJUSTDAY

if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through

:SET31

set /A dd=31 + %dd%

goto CHKDAY

:SET30

set /A dd=30 + %dd%

goto CHKDAY

:LEAPCHK

set /A tt=%yy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY

:DONESUBTRACT

if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

goto :EOF

:GETPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF

0
tspa
Asked:
tspa
  • 22
  • 20
1 Solution
 
tspaAuthor Commented:
Just a side note, based on the date i posted the previous thread, and the date of the files i was cleaning off by hand today, it wasn't dumping any files with long paths/filenames...so i'm bout 80% sure that it has never deleted those type filenames.  Maybe the script needs to be modified to add "quotes" around the path of the delete command it generates?  Of course being a script noob, i know nothing of how to do that...

again, thanks for your help in advance.
0
 
WelkinMazeCommented:
Hi,
To put quotes in the delete command change this
del /f /a:s %1
with
del /f /a:s "%1"

Also if you start tha bat file with parameters put them in quotes too.
0
 
tspaAuthor Commented:
Well that didn't work...but it did make a difference...here's my results...

Could Not Find D:\TempDrive\kidzpix\kidzpix    <--note that it left off everything after the space...

also, i am not using any parameters when calling the bat file.
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
SteveGTRCommented:
The problem is your modification to the del command:

del /f /a:s %1

What is your intent here with the /a:s parameter?

What you are saying to del is to delete files with the system attribute.

Is it your intent to delete the file from this directory and all subdirectories? If so the command should be formatted:

del /f /s %1

Good Luck,
Steve
0
 
tspaAuthor Commented:
from the thread link at the top of the page, i'm pasting my reasoning for that below...

_____
One note, if you don't add /a:s to the line below, thumbs.db will prevent the removal of "empty" directories...go me for figuring that one out...

REM **This is where the "safety" is.  Remove the echo statement to arm.
echo del /f /a:s %1
_____

the problem before was that thumbs.db (or any system file) would prevent an "empty" folder from being removed...and i thought i was being smart by doing something on my own...so i broke it?  maybe there's a second call somewhere that i can do to delete system files? so far only thumbs.db has been an obvious problem...

0
 
SteveGTRCommented:
The key would be the dir command in the for statement. This drives the file selection:

for /F "tokens=1-4*" %%a in ('dir /TC /a-d ^| find "/" ^| findstr /V /I DO_NOT_REMOVE_NtFrs') do call :CHECK "%%e" %%a

I couldn't see a way to tell it to process regular files (non-dirs) and system files at the same time. So one solution would be to just replicate the for statement with a revised dir command:

for /F "tokens=1-4*" %%a in ('dir /TC /a-d ^| find "/" ^| findstr /V /I DO_NOT_REMOVE_NtFrs') do call :CHECK "%%e" %%a
for /F "tokens=1-4*" %%a in ('dir /TC /a-ds ^| find "/" ^| findstr /V /I DO_NOT_REMOVE_NtFrs') do call :CHECK "%%e" %%a
0
 
tspaAuthor Commented:
Yah there's no reason that it all has to happen in one command right?  i mean that's why we use scripts in the first place :)  

i think i can see what each line is doing in your above reply, but i'm not sure what to replace...should i replace my:

for /F "tokens=1-4*" %%a in ('dir /TC /a-d ^| find "/" ^| findstr /V /I DO_NOT_REMOVE_NtFrs') do call :CHECK "%%e" %%a

with both like this:

for /F "tokens=1-4*" %%a in ('dir /TC /a-d ^| find "/" ^| findstr /V /I DO_NOT_REMOVE_NtFrs') do call :CHECK "%%e" %%a
for /F "tokens=1-4*" %%a in ('dir /TC /a-ds ^| find "/" ^| findstr /V /I DO_NOT_REMOVE_NtFrs') do call :CHECK "%%e" %%a

???

also, what do i need to change this back to:

del /f /a:s %1

???

again, thanks for your help on this...
0
 
SteveGTRCommented:
Include both the for statements. Are remove the /a:s from the del command.
0
 
tspaAuthor Commented:
Man, where did u learn this stuff...it appears that it worked perfectly...i'm gonna look around a little more after lunch, and then i'll know for sure...i can't believe how popular the tempdrive got...i'd say there's an average of 5-10gig on it at any time...

just want to run my script by you again as it is now, with the changes you told me...

__________

@echo off

setlocal enabledelayedexpansion

set fileDir=%~1

REM **Sets default folder to be cleaned if none entered...
if "%fileDir%"=="" set fileDir=d:\tempdrive

call :GETPARTS "%date%

REM ** Here's where to change the days
call :SUBTRACTDAYS 7

set _CutOff=%yy%/%mm%/%dd%

pushd.

cd /d "%fileDir%"

call :PROCESSDIR

for /f "delims=" %%a in ('dir /b /s /ad') do call :PROCESSSUBDIR "%%a"

set sortKey=10000

if exist _temp.txt del _temp.txt>NUL

for /f "delims=" %%a in ('dir /b /s /ad 2^>NUL') do (echo

!sortKey!%%a)>>_temp.txt&set /a sortKey+=1

if exist _temp.txt (
  for /f "delims=" %%a in ('type _temp.txt ^| sort /R') do call :CHECKDIR "%%a"
)

popd

goto :EOF

:CHECKDIR

set dirName=%~1
set dirName=%dirName:~5%

for /f "delims=" %%a in ('dir "%dirName%" ^| findstr /L /C:"0 File(s)"') do echo

Removing: %dirName%&rd /Q "%dirName%"

goto :EOF

:PROCESSSUBDIR

pushd.

cd /d %1

call :PROCESSDIR

popd

goto :EOF

:PROCESSDIR

for /F "tokens=1-4*" %%a in ('dir /TC /a-d ^| find "/" ^| findstr /V /I

DO_NOT_REMOVE_NtFrs') do call :CHECK "%%e" %%a
for /F "tokens=1-4*" %%a in ('dir /TC /a-ds ^| find "/" ^| findstr /V /I

DO_NOT_REMOVE_NtFrs') do call :CHECK "%%e" %%a


goto :EOF

:CHECK

call :GETPARTS %2

if /i "%yy%/%mm%/%dd%" GTR "%_CutOff%" goto :EOF

REM **This is where the "safety" is.  Remove the echo statement to arm.
del /f %1

goto :EOF

:SUBTRACTDAYS

set dayCnt=%1

if "%dayCnt%"=="" set dayCnt=1

REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100

:CHKDAY

if /I %dd% GTR 0 goto DONESUBTRACT

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yy=%yy% - 1

:ADJUSTDAY

if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through

:SET31

set /A dd=31 + %dd%

goto CHKDAY

:SET30

set /A dd=30 + %dd%

goto CHKDAY

:LEAPCHK

set /A tt=%yy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY

:DONESUBTRACT

if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

goto :EOF

:GETPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set

%%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF

___________

let me know if all looks good to you, and after lunch, i'll let you know if all looks good on my end...
0
 
SteveGTRCommented:
That looks great :)
0
 
tspaAuthor Commented:
It didn't remove dir's with thumbs.db in it :(

also i see FILE NOT FOUND several times, but i don't know if that's normal or not...
Here's the result as i just ran it...i ran it before and it removed old files with long names, but now we're still stuck because it can't delete thumbs.db

C:\>cleantempdrive
File Not Found
File Not Found
File Not Found
File Not Found
File Not Found
File Not Found
File Not Found
File Not Found
File Not Found
File Not Found
File Not Found
File Not Found
Removing: D:\TempDrive\kidzpix
The directory is not empty.
Removing: D:\TempDrive\JoseA
The directory is not empty.
Removing: D:\TempDrive\James
The directory is not empty.
Removing: D:\TempDrive\Faun
The directory is not empty.
File Not Found

what's weird about the removing of dirs is it is only trying to remove 4 of em here...there are about 8 actually, and only 1 of the above folders is actually empty (minus the thumbs.db).  I'm not 100% sure how your script is calling them or whatever, but if it's acting right then just let me know...i just need it to be able to trash the thumbs.db (and any system file) so that it can remove the folder.

thanks!

0
 
SteveGTRCommented:
It appears as if the del command can't locate system files. You can fix this by including the following statement prior to the delete:

REM **This is where the "safety" is.  Remove the echo statement to arm.
attrib -s %1
del /f %1
0
 
tspaAuthor Commented:
no change :(

same exact result when i added that line
0
 
SteveGTRCommented:
Looks like this file is also hidden and you have to do this:

attrib -s -h %1

Swap out the attrib command with the one above and give it a wing.
0
 
tspaAuthor Commented:
again, same result...

remember above where you asked me what i did this for:

del /f /a:s %1

that killed the thumbs.db...but i think it broke the long filename deletion (which is fixed now)

as i look at the above line, does that remove ONLY system files or ALSO system files?

just throwing more thoughts out there...
0
 
SteveGTRCommented:
Please post the script you are running now. Thanks :)
0
 
tspaAuthor Commented:
ok sorry, i thought i should have done that...currently i don't have the /a:s in there, here's what i have...

__________
@echo off

setlocal enabledelayedexpansion

set fileDir=%~1

REM **Sets default folder to be cleaned if none entered...
if "%fileDir%"=="" set fileDir=d:\tempdrive

call :GETPARTS "%date%

REM ** Here's where to change the days
call :SUBTRACTDAYS 7

set _CutOff=%yy%/%mm%/%dd%

pushd.

cd /d "%fileDir%"

call :PROCESSDIR

for /f "delims=" %%a in ('dir /b /s /ad') do call :PROCESSSUBDIR "%%a"

set sortKey=10000

if exist _temp.txt del _temp.txt>NUL

for /f "delims=" %%a in ('dir /b /s /ad 2^>NUL') do (echo

!sortKey!%%a)>>_temp.txt&set /a sortKey+=1

if exist _temp.txt (
  for /f "delims=" %%a in ('type _temp.txt ^| sort /R') do call :CHECKDIR "%%a"
)

popd

goto :EOF

:CHECKDIR

set dirName=%~1
set dirName=%dirName:~5%

for /f "delims=" %%a in ('dir "%dirName%" ^| findstr /L /C:"0 File(s)"') do echo

Removing: %dirName%&rd /Q "%dirName%"

goto :EOF

:PROCESSSUBDIR

pushd.

cd /d %1

call :PROCESSDIR

popd

goto :EOF

:PROCESSDIR

for /F "tokens=1-4*" %%a in ('dir /TC /a-d ^| find "/" ^| findstr /V /I

DO_NOT_REMOVE_NtFrs') do call :CHECK "%%e" %%a
for /F "tokens=1-4*" %%a in ('dir /TC /a-ds ^| find "/" ^| findstr /V /I

DO_NOT_REMOVE_NtFrs') do call :CHECK "%%e" %%a


goto :EOF

:CHECK

call :GETPARTS %2

if /i "%yy%/%mm%/%dd%" GTR "%_CutOff%" goto :EOF

REM **This is where the "safety" is.  Remove the echo statement to arm.
attrib -s -h %1
del /f %1

goto :EOF

:SUBTRACTDAYS

set dayCnt=%1

if "%dayCnt%"=="" set dayCnt=1

REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100

:CHKDAY

if /I %dd% GTR 0 goto DONESUBTRACT

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yy=%yy% - 1

:ADJUSTDAY

if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through

:SET31

set /A dd=31 + %dd%

goto CHKDAY

:SET30

set /A dd=30 + %dd%

goto CHKDAY

:LEAPCHK

set /A tt=%yy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY

:DONESUBTRACT

if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

goto :EOF

:GETPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set

%%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF
0
 
SteveGTRCommented:
I tested out the file deletion part and that appears to be working fine know. I believe the noice you are receiving now is from dir commands that don't return results. That can be fixed with the following:

@echo off

setlocal enabledelayedexpansion

set fileDir=%~1

REM **Sets default folder to be cleaned if none entered...
if "%fileDir%"=="" set fileDir=d:\tempdrive

call :GETPARTS "%date%

REM ** Here's where to change the days
call :SUBTRACTDAYS 7

set _CutOff=%yy%/%mm%/%dd%

pushd.

cd /d "%fileDir%"

call :PROCESSDIR

for /f "delims=" %%a in ('dir /b /s /ad 2^>NUL') do call :PROCESSSUBDIR "%%a"

set sortKey=10000

if exist _temp.txt del _temp.txt>NUL

for /f "delims=" %%a in ('dir /b /s /ad 2^>NUL') do (echo

!sortKey!%%a)>>_temp.txt&set /a sortKey+=1

if exist _temp.txt (
  for /f "delims=" %%a in ('type _temp.txt ^| sort /R') do call :CHECKDIR "%%a"
)

popd

goto :EOF

:CHECKDIR

set dirName=%~1
set dirName=%dirName:~5%

for /f "delims=" %%a in ('dir "%dirName%" ^| findstr /L /C:"0 File(s)"') do echo Removing: %dirName%&rd /Q "%dirName%"

goto :EOF

:PROCESSSUBDIR

pushd.

cd /d %1

call :PROCESSDIR

popd

goto :EOF

:PROCESSDIR

for /F "tokens=1-4*" %%a in ('dir /TC /a-d 2^>NUL ^| find "/" ^| findstr /V /I DO_NOT_REMOVE_NtFrs') do call :CHECK "%%e" %%a
for /F "tokens=1-4*" %%a in ('dir /TC /a-ds 2^>NUL ^| find "/" ^| findstr /V /I DO_NOT_REMOVE_NtFrs') do call :CHECK "%%e" %%a


goto :EOF

:CHECK

call :GETPARTS %2

if /i "%yy%/%mm%/%dd%" GTR "%_CutOff%" goto :EOF

REM **This is where the "safety" is.  Remove the echo statement to arm.
attrib -s -h %1
del /f %1

goto :EOF

:SUBTRACTDAYS

set dayCnt=%1

if "%dayCnt%"=="" set dayCnt=1

REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100

:CHKDAY

if /I %dd% GTR 0 goto DONESUBTRACT

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yy=%yy% - 1

:ADJUSTDAY

if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through

:SET31

set /A dd=31 + %dd%

goto CHKDAY

:SET30

set /A dd=30 + %dd%

goto CHKDAY

:LEAPCHK

set /A tt=%yy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY

:DONESUBTRACT

if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

goto :EOF

:GETPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set

%%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF
0
 
tspaAuthor Commented:
ok that was strange...

it posts a lot of info about the laptop i'm on now (remote desktoping into the server that the cleaner is running on) and then it posts this at the end...

'%x' is not recognized as an internal or external command,
operable program or batch file.
70 was unexpected at this time.

for obvious reasons i don't want to post everything that it put up, but if you need it, i can "blur" out the private data...
0
 
SteveGTRCommented:
I applied the changes to your post and tried to fix it up as I could. I saw it was sort of messed up and thought maybe it was the EE HTML parser or something.

Basically my change involves adding this to the end of the dir command performed in the for statements:

2^>NUL

This redirects any errors like no files found to the NUL device.

Here's an example:

for /F "tokens=1-4*" %%a in ('dir /TC /a-d 2^>NUL ^| find "/" ^| findstr /V /I DO_NOT_REMOVE_NtFrs') do call :CHECK "%%e" %%a

Go ahead and apply that to all the dir commands in your batch file.
0
 
tspaAuthor Commented:
i'm sorry, i'm confused and i think i did a bad thing...i pasted that last full script you posted (the one that generated the strange stuff) over my other one and saved it...can you post up the correct script so i can have a clean start, and then post again to verify that EE's html didn't mess it up?

it's quittin time here, so i'm gonna close up shop and pick this up when i get home in an hour, so if it's quittin time for you, feel free to pick this up at your convenience...thanks again so much for your help...i know next to nothing about scripting, but like everything else, i'm learning chunks of all sorts of I.T. left and right as i go!  
0
 
SteveGTRCommented:
I don't have the original file. But, here's a version of the last post I did that appears to be fixed:

@echo off

setlocal enabledelayedexpansion

set fileDir=%~1

REM **Sets default folder to be cleaned if none entered...
if "%fileDir%"=="" set fileDir=d:\tempdrive

call :GETPARTS "%date%

REM ** Here's where to change the days
call :SUBTRACTDAYS 7

set _CutOff=%yy%/%mm%/%dd%

pushd.

cd /d "%fileDir%"

call :PROCESSDIR

for /f "delims=" %%a in ('dir /b /s /ad 2^>NUL') do call :PROCESSSUBDIR "%%a"

set sortKey=10000

if exist _temp.txt del _temp.txt>NUL

for /f "delims=" %%a in ('dir /b /s /ad 2^>NUL') do (echo !sortKey!%%a)>>_temp.txt&set /a sortKey+=1

if exist _temp.txt (
  for /f "delims=" %%a in ('type _temp.txt ^| sort /R') do call :CHECKDIR "%%a"
)

popd

goto :EOF

:CHECKDIR

set dirName=%~1
set dirName=%dirName:~5%

for /f "delims=" %%a in ('dir "%dirName%" ^| findstr /L /C:"0 File(s)"') do echo Removing: %dirName%&rd /Q "%dirName%"

goto :EOF

:PROCESSSUBDIR

pushd.

cd /d %1

call :PROCESSDIR

popd

goto :EOF

:PROCESSDIR

for /F "tokens=1-4*" %%a in ('dir /TC /a-d 2^>NUL ^| find "/" ^| findstr /V /I DO_NOT_REMOVE_NtFrs') do call :CHECK "%%e" %%a
for /F "tokens=1-4*" %%a in ('dir /TC /a-ds 2^>NUL ^| find "/" ^| findstr /V /I DO_NOT_REMOVE_NtFrs') do call :CHECK "%%e" %%a

goto :EOF

:CHECK

call :GETPARTS %2

echo if /i "%yy%/%mm%/%dd%" GTR "%_CutOff%" goto :EOF

REM **This is where the "safety" is.  Remove the echo statement to arm.
echo attrib -s -h %1
echo del /f %1

goto :EOF

:SUBTRACTDAYS

set dayCnt=%1

if "%dayCnt%"=="" set dayCnt=1

REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100

:CHKDAY

if /I %dd% GTR 0 goto DONESUBTRACT

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yy=%yy% - 1

:ADJUSTDAY

if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through

:SET31

set /A dd=31 + %dd%

goto CHKDAY

:SET30

set /A dd=30 + %dd%

goto CHKDAY

:LEAPCHK

set /A tt=%yy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY

:DONESUBTRACT

if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

goto :EOF

:GETPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF
0
 
tspaAuthor Commented:
i'm sorry for the delay in this...i had a wonderful user here delete a year's worth of projects, and have been restoring that...i'm going to try this script today and see what she does.
0
 
tspaAuthor Commented:
ok, using the last full script you posted, as is...generates this...

attrib -s -h "05-05-06 084.jpg"
del /f "05-05-06 084.jpg"
if /i "2006/05/05" GTR "2006/05/04" goto :EOF
attrib -s -h "05-05-06 085.jpg"
del /f "05-05-06 085.jpg"
if /i "2006/05/05" GTR "2006/05/04" goto :EOF
attrib -s -h "05-05-06 086.jpg"
del /f "05-05-06 086.jpg"
if /i "2006/05/05" GTR "2006/05/04" goto :EOF
attrib -s -h "05-05-06 087.jpg"
del /f "05-05-06 087.jpg"
if /i "2006/05/05" GTR "2006/05/04" goto :EOF
attrib -s -h "05-05-06 088.jpg"
del /f "05-05-06 088.jpg"
if /i "2006/05/05" GTR "2006/05/04" goto :EOF
attrib -s -h "Thumbs.db"
del /f "Thumbs.db"
if /i "2006/05/05" GTR "2006/05/04" goto :EOF
attrib -s -h "Thumbs.db"
del /f "Thumbs.db"
Removing: D:\TempDrive\The Gardens
The directory is not empty.
Removing: D:\TempDrive\kidzpix
The directory is not empty.
Removing: D:\TempDrive\JoseA
The directory is not empty.
Removing: D:\TempDrive\Faun
The directory is not empty.
File Not Found

any thoughts?
0
 
SteveGTRCommented:
What files still exist in these directories? What are the attributes?
0
 
tspaAuthor Commented:
gardens, josea, and faun are legitimately holding data (the faun folder wouldn't be if the pix inside actually self deleted, but the others are still within the seven days.), but the kidzpix folder only has thumbs.db in it...thumbs is a hidden (system) file.  There are other folders on the "tempdrive" that aren't mentioned in the output.

what is up with the other though?
if /i "2006/05/05" GTR "2006/05/04" goto :EOF
attrib -s -h "05-05-06 085.jpg"
del /f "05-05-06 085.jpg"
i've never seen this before, are we echoing something we shouldn't?
0
 
SteveGTRCommented:
Looks like I left the debugging statements in the file. Try this replacement for the CHECK routine:

:CHECK

call :GETPARTS %2

if /i "%yy%/%mm%/%dd%" GTR "%_CutOff%" goto :EOF

REM **This is where the "safety" is.  Remove the echo statement to arm.
attrib -s -h %1
del /f %1

goto :EOF
0
 
SteveGTRCommented:
Looking at the processing you'll probably also want to change the line that processes the removal of the directories:

Change this:

for /f "delims=" %%a in ('dir "%dirName%" ^| findstr /L /C:"0 File(s)"') do echo Removing: %dirName%&rd /Q "%dirName%"

To:

for /f "delims=" %%a in ('dir "%dirName%" ^| findstr /L /C:"0 File(s)"') do (
  for /f "delims=" %%b in ('dir /as "%dirName%" ^| findstr /L /C:"0 File(s)"') do (
     for /f "delims=" %%c in ('dir /ah "%dirName%" ^| findstr /L /C:"0 File(s)"') do echo Removing: %dirName%&rd /Q "%dirName%"
  )
)
0
 
tspaAuthor Commented:
OK, i am a little curious, what do the last two ")" do?  Is that a typo?

for /f "delims=" %%a in ('dir "%dirName%" ^| findstr /L /C:"0 File(s)"') do (
  for /f "delims=" %%b in ('dir /as "%dirName%" ^| findstr /L /C:"0 File(s)"') do (
     for /f "delims=" %%c in ('dir /ah "%dirName%" ^| findstr /L /C:"0 File(s)"') do echo Removing: %dirName%&rd /Q "%dirName%"
  )   <----
)   <----
0
 
SteveGTRCommented:
They are for the prior to open parens.

for /f "delims=" %%a in ('dir "%dirName%" ^| findstr /L /C:"0 File(s)"') do ( <----
  for /f "delims=" %%b in ('dir /as "%dirName%" ^| findstr /L /C:"0 File(s)"') do ( <----
     for /f "delims=" %%c in ('dir /ah "%dirName%" ^| findstr /L /C:"0 File(s)"') do echo Removing: %dirName%&rd /Q "%dirName%"
  )
)
0
 
tspaAuthor Commented:
AHHH!

there they are...trying now...what exactly is this doing differently?
0
 
SteveGTRCommented:
Well we found out we had to do extra dir commands for the system files and I applied the same to the remove directory processing. Basically this boils down to don't remove the directory unless there are no regular files (first for), no system files (second for), and no hidden files (third for).
0
 
tspaAuthor Commented:
gotcha...i can see that now...i ran it like that again, and got 3 "File not Founds" but that may be expected, since the folder has already been "cleaned out" for the day...but i didn't get any errors on dirs that weren't empty, thought that actually didn't bother me before (i'm not going to be awake looking at the script when it runs...i'll see what it does tonight and we should be good to go.  
0
 
SteveGTRCommented:
You can do the following to get rid of file not found messages:

for /f "delims=" %%a in ('dir "%dirName%" 2^>NUL ^| findstr /L /C:"0 File(s)"') do ( <----
  for /f "delims=" %%b in ('dir /as "%dirName%" 2^>NUL ^| findstr /L /C:"0 File(s)"') do ( <----
     for /f "delims=" %%c in ('dir /ah "%dirName%" 2^>NUL ^| findstr /L /C:"0 File(s)"') do echo Removing: %dirName%&rd /Q "%dirName%"
  )
)
0
 
tspaAuthor Commented:
minus the arrows right?
0
 
SteveGTRCommented:
Yes
0
 
tspaAuthor Commented:
Ok, i get no file not founds, but it's not deleting empty folders at all now.  

I'm adding points, cuz i know this is taxing...i thought it was gonna be a simple fix...one day i'll learn my lesson.
0
 
SteveGTRCommented:
Please post the batch file you are using now.
0
 
tspaAuthor Commented:
@echo off

setlocal enabledelayedexpansion

set fileDir=%~1

REM **Sets default folder to be cleaned if none entered...
if "%fileDir%"=="" set fileDir=d:\tempdrive

call :GETPARTS "%date%

REM ** Here's where to change the days
call :SUBTRACTDAYS 7

set _CutOff=%yy%/%mm%/%dd%

pushd.

cd /d "%fileDir%"

call :PROCESSDIR

for /f "delims=" %%a in ('dir /b /s /ad 2^>NUL') do call :PROCESSSUBDIR "%%a"

set sortKey=10000

if exist _temp.txt del _temp.txt>NUL

for /f "delims=" %%a in ('dir /b /s /ad 2^>NUL') do (echo !sortKey!%%a)>>_temp.txt&set /a sortKey+=1

if exist _temp.txt (
  for /f "delims=" %%a in ('type _temp.txt ^| sort /R') do call :CHECKDIR "%%a"
)

popd

goto :EOF

:CHECKDIR

set dirName=%~1
set dirName=%dirName:~5%

for /f "delims=" %%a in ('dir "%dirName%" 2^>NUL ^| findstr /L /C:"0 File(s)"') do (
  for /f "delims=" %%b in ('dir /as "%dirName%" 2^>NUL ^| findstr /L /C:"0 File(s)"') do (
     for /f "delims=" %%c in ('dir /ah "%dirName%" 2^>NUL ^| findstr /L /C:"0 File(s)"') do echo Removing: %dirName%&rd /Q "%dirName%"
  )
)

goto :EOF

:PROCESSSUBDIR

pushd.

cd /d %1

call :PROCESSDIR

popd

goto :EOF

:PROCESSDIR

for /F "tokens=1-4*" %%a in ('dir /TC /a-d 2^>NUL ^| find "/" ^| findstr /V /I DO_NOT_REMOVE_NtFrs') do call :CHECK "%%e" %%a
for /F "tokens=1-4*" %%a in ('dir /TC /a-ds 2^>NUL ^| find "/" ^| findstr /V /I DO_NOT_REMOVE_NtFrs') do call :CHECK "%%e" %%a

goto :EOF

:CHECK

call :GETPARTS %2

if /i "%yy%/%mm%/%dd%" GTR "%_CutOff%" goto :EOF

REM **This is where the "safety" is.  Remove the echo statement to arm.
attrib -s -h %1
del /f %1

goto :EOF

:SUBTRACTDAYS

set dayCnt=%1

if "%dayCnt%"=="" set dayCnt=1

REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100

:CHKDAY

if /I %dd% GTR 0 goto DONESUBTRACT

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yy=%yy% - 1

:ADJUSTDAY

if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through

:SET31

set /A dd=31 + %dd%

goto CHKDAY

:SET30

set /A dd=30 + %dd%

goto CHKDAY

:LEAPCHK

set /A tt=%yy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY

:DONESUBTRACT

if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

goto :EOF

:GETPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF
0
 
SteveGTRCommented:
Looks like this should fix that:

for /f "delims=" %%a in ('dir "%dirName%" 2^>NUL ^| findstr /L /C:"0 File(s)"') do (
  for /f "delims=" %%b in ('dir /as "%dirName%" 2^>^&1 ^| findstr /L /C:"File Not Found"') do (
     for /f "delims=" %%c in ('dir /ah "%dirName%" 2^>^&1 ^| findstr /L /C:"File Not Found"') do echo Removing: %dirName%&rd /Q "%dirName%"
  )
)
0
 
tspaAuthor Commented:
That time gottit...i almost freaked, cuz there was one folder holding a thumbs.db, but the i realized that the thumbs file was generated today...thank you so much for helping me with this.

I'm accepting your previous post because that made the whole thing work, and i will paste the entire script just below here for everyone's reference.  

Thanks again!  I'm sure i'll have another script to come up with in the near future...

HERE IS THE FINAL VERSION OF THE SCRIPT THAT WORKS...I HAVE PUT IN THE SAFETY ON THE DELETE COMMANDS, REMOVE THE ECHO TO "ARM"

@echo off

setlocal enabledelayedexpansion

set fileDir=%~1

REM **Sets default folder to be cleaned if none entered...
if "%fileDir%"=="" set fileDir=d:\tempdrive

call :GETPARTS "%date%

REM ** Here's where to change the days
call :SUBTRACTDAYS 7

set _CutOff=%yy%/%mm%/%dd%

pushd.

cd /d "%fileDir%"

call :PROCESSDIR

for /f "delims=" %%a in ('dir /b /s /ad 2^>NUL') do call :PROCESSSUBDIR "%%a"

set sortKey=10000

if exist _temp.txt del _temp.txt>NUL

for /f "delims=" %%a in ('dir /b /s /ad 2^>NUL') do (echo !sortKey!%%a)>>_temp.txt&set /a sortKey+=1

if exist _temp.txt (
  for /f "delims=" %%a in ('type _temp.txt ^| sort /R') do call :CHECKDIR "%%a"
)

popd

goto :EOF

:CHECKDIR

set dirName=%~1
set dirName=%dirName:~5%

for /f "delims=" %%a in ('dir "%dirName%" 2^>NUL ^| findstr /L /C:"0 File(s)"') do (
  for /f "delims=" %%b in ('dir /as "%dirName%" 2^>^&1 ^| findstr /L /C:"File Not Found"') do (
     for /f "delims=" %%c in ('dir /ah "%dirName%" 2^>^&1 ^| findstr /L /C:"File Not Found"') do echo Removing: %dirName%&rd /Q "%dirName%"
  )
)

goto :EOF

:PROCESSSUBDIR

pushd.

cd /d %1

call :PROCESSDIR

popd

goto :EOF

:PROCESSDIR

for /F "tokens=1-4*" %%a in ('dir /TC /a-d 2^>NUL ^| find "/" ^| findstr /V /I DO_NOT_REMOVE_NtFrs') do call :CHECK "%%e" %%a
for /F "tokens=1-4*" %%a in ('dir /TC /a-ds 2^>NUL ^| find "/" ^| findstr /V /I DO_NOT_REMOVE_NtFrs') do call :CHECK "%%e" %%a

goto :EOF

:CHECK

call :GETPARTS %2

if /i "%yy%/%mm%/%dd%" GTR "%_CutOff%" goto :EOF

REM **This is where the "safety" is.  Remove the echo statement to arm.
attrib -s -h %1
echo del /f %1

goto :EOF

:SUBTRACTDAYS

set dayCnt=%1

if "%dayCnt%"=="" set dayCnt=1

REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100

:CHKDAY

if /I %dd% GTR 0 goto DONESUBTRACT

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yy=%yy% - 1

:ADJUSTDAY

if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through

:SET31

set /A dd=31 + %dd%

goto CHKDAY

:SET30

set /A dd=30 + %dd%

goto CHKDAY

:LEAPCHK

set /A tt=%yy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY

:DONESUBTRACT

if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

goto :EOF

:GETPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF
0
 
tspaAuthor Commented:
WHAT?! THERE'S NO A++ ON THE GRADING SCALE...
0
 
SteveGTRCommented:
Thanks :)
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 22
  • 20
Tackle projects and never again get stuck behind a technical roadblock.
Join Now