1.

Solve : Help with code to delete file when not in use?

Answer»

I was helped recently with my question regarding opening a *.ts file in my video conversion project, which worked well.

I now want to check to see if the file is finished converting, and then delete the original, and found this code (Salmon Trout):


@echo off
echo Testing file %~nx1
if not exist "%~dpnx1" echo File does not exist! & GOTO end
if exist "%~dp1\dummy.file" del "%~dp1\dummy.file"
ren "%~dpnx1" dummy.file
If %ERRORLEVEL% gtr 0 (
echo file in use
) else (
echo file not in use
ren "%~dp1\dummy.file" "%~nx1"
)
:end


This looks as if it's perfect for me, but I don't understand how to use it (%~nx1
?).

I'll be checking this file:
J:\Documents and Settings\All Users\Documents\My Videos\wintv\*.avi
and then will use something like this to delete it when it's "not in use"(?):
@echo off
cd C:\Documents and Settings\All Users\Documents\My Videos\
DEL *.ts


Can SOMEONE tell me how to implement the above code to achieve this? Any notes on why you changed what would be helpful, but not necessary if you don't have time for it, but I'd like to understand what you did if possible.try this:
Code: [Select]@echo off
for /f "delims=" %%A in ('dir /b /a-d ^| find ".avi"') do call :check "%%A"
pause>nul
exit

:check
echo Testing file %~nx1
if not exist "%~dpnx1" echo File does not exist! & goto end
if exist "%~dp1\dummy.file" del "%~dp1\dummy.file"
ren "%~dpnx1" dummy.file
If %Errorlevel% gtr 0 (
echo file [%~n1] in use
) else (
echo file [%~n1] not in use
ren "%~dp1\dummy.file" "%~nx1"
call :del "%~1"
)
:end
goto :eof

:del
@echo off
echo del "C:\Documents and Settings\All Users\Documents\My Videos\%~n1.ts"
goto :eof



Discussion

No Comment Found