Batch file deleting itself issue

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
stroked
Posts: 20
Joined: 29 Apr 2013 21:48

Batch file deleting itself issue

#1 Post by stroked » 10 May 2013 22:56

when run without commandline, AOK, but when run with, alls not well. the routine halts with a batch not found error,
and the batch is, you guessed it, MIA.
how it gets deleted when you'd expect it to have "access denied" attribs while running is anyones guess!
you can test it by running with no %1 for one routine [works], and any %1 for tother.
i must have a missing parenthesis, in a nest, but cant see it.

Code: Select all

@cls
@ECHO Off
COLOR 0e
   if "%~1" == "" goto ProcessNoCmdline
   if not "%~1" == "" goto ProcessCmdline

:ProcessNoCmdLine
IF not EXIST NFO2DIR2.TXT (
     ECHO Ensure your've read nfo2dir2.txt before you run, Press ^^C if you want to abort.
   echo        Because WARNING: This will irreversibly, alter disk structure!!!
   ping localhost -n 5 >nul
 ) else (
 START /max notepad NFO2DIR2.TXT
)

if not exist *.nfo (
        ECHO      It appears you haven't run vid. export yet! Aborting with NO changes.
        ping localhost -n 5 >nul
        exit /B
   )

echo.
echo Stacked video files MAY be present, run with
echo   commandline next, continuing anyway. .
ping localhost -n 5 >nul

for %%a in (*.nfo) do md "%%~na" & move /y "%%~na.*" "%%~na" & move /y "%%~na-fanart.*" "%%~na" & move /y "%

%~na-poster.*" "%%~na" & move /y "%%~na-thumb.*" "%%~na" & move /y "%%~na-landscape.*" "%%~na" 2 >nul

echo.               
echo Please wait. . DON'T INTERRUPT!!
ping localhost -n 5 >nul

if EXIST extrathumbs (
 rd /s /q extrathumbs
)

if EXIST extrafanart (
 rd /s /q extrafanart   
 echo.
 echo extrafanart and extrathumbs dir. deleted
   ) ELSE (
   echo.
   echo extrafanart or extrathumbs dir. not found. . .

:end
cls
echo.
echo The following is a listing of files not yet processed
echo It doesn't show your newly created dir. structure!. . .
echo.
dir /a-d-h
ping localhost -n 5 >nul

echo.                     
ECHO          +------------------------------+
ECHO          :XBMC NFO TO DIR Creator  V2.50:
ECHO          : 2012,2013, by the 'Strokeman':
ECHO          +------------------------------+
ECHO.
echo Operation Completed Successfully, you will now have to update Library, After
echo that, please run artwork downloader, then enable extrafanart in chosen skin.
echo.
echo      Rogue or orphan stacked files?. . .Run nfo2dir2 with commandline.
ping localhost -n 5 >nul
goto :eof

:ProcessCmdline
   @Echo OFF
   Color 0b

   REM Part of the file name, will be replaced with %~1 to take input from cmd
   SET "InFilePartName=%~1"
   SET "MaxFiles=%~2"            // Default Max Files in sequence is 10

   REM Check for user input
   IF "%~1" == "" Goto :Error
   IF "%~2" == "" SET "MaxFiles=10"            // Default max files will be set to 10 if no input was found

   Rem The Following is executed when there is input from user
   SET "PartName=%InFilePartName:~0,-1%"         // This is the name without the last number
   SET "PartNum1=%InFilePartName:~-1%"         // Remove This Line if The parts always start with number 1
   For /L %%A In ( 1 1 %MaxFiles% ) Do (
       IF Exist "*%PartName%%%A*" (
   CALL :ProcessFiles "%PartName%" "%%A"
)
   )
)

REM After It finish it will Display the following and exit
echo.           
echo            +-----------------------------------------+
Echo             Rogue 'n' orphan stacked file mover v1.50
Echo              A NFO2DIR2 companion, 2013, 'Strokeman' 
Echo            +-----------------------------------------+
Echo.
Echo COMPLETED..Directory(s) created and all related files (IF ANY), moved...
GOTO :EOF

Exit
:ProcessFiles
SET "InputName=%~1"
SET "InputNum=%~2"
SETLOCAL EnableDelayedExpansion
For /F "delims=" %%A In (' DIR /B /A:-D "*%InputName%%InputNum%.*" ') Do (
   IF Exist "%%A" (
      SET "FName=%%~nA"
      SET "FNameExt=%%~nxA"
      SET "FullFName=%%~dpnxA"
        For /F "tokens=1 delims=-.#(" %%z In ( "!FName!" ) Do SET "DirName=%%z"
   IF NOT EXIST "!DirName!" MD "!DirName!" >NUL
    IF EXIST "!FullFName!" MOVE "!FullFName!" "%%~dpA!DirName!\" >NUL
   for %%b in (fanart poster thumb landscape) do (
   IF EXIST "%%~dpA!DirName!*-%%b.*" MOVE "%%~dpA!DirName!-*-%%b.*" "%%~dpA!DirName!\" >NUL
)
         ) Else (
         Echo No files found that match. .
         ping localhost -n 5 >nul
   )
)
gOTO :EOF

:Error
Echo None, or incorrect commandline given. . .exiting with no changes. .
Echo Syntax:clean stackseparator eg. "clean -cd1", "clean disk1"
Echo If orphaned files remain, you may need to manually move.
GOTO :EOF

stroked
Posts: 20
Joined: 29 Apr 2013 21:48

Re: Batch file deleting itself issue

#2 Post by stroked » 11 May 2013 01:17

ic whats going on!
its moving the batch to the nfo2dir2 dir it correctly makes..doh,
how to stop it happening?

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Batch file deleting itself issue

#3 Post by Endoro » 11 May 2013 05:44

Code: Select all

 if not exist *.nfo 

Code: Select all

 IF Exist "*%PartName%%%A*" 

Code: Select all

 IF EXIST "%%~dpA!DirName!*-%%b.*" 

if exist doesn't work with wild cards

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Batch file deleting itself issue

#4 Post by Aacini » 11 May 2013 08:42

The answer to the question: "How a Batch file can delete itself?" is:

Place the DEL command (or any other command that remove the Batch file from its location) in a line that also terminate the execution:

Code: Select all

del myself.bat & exit /B

Another possible way is place the DEL command in the last line of the Batch file and not terminate this line with CR+LF

Antonio

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file deleting itself issue

#5 Post by foxidrive » 11 May 2013 10:29

Endoro wrote:if exist doesn't work with wild cards


Can you clarify that?

It works here.

z:\>if not exist *.png @echo no

z:\>if exist *.png @echo yes
yes

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Batch file deleting itself issue

#6 Post by Endoro » 11 May 2013 11:30

foxidrive wrote:It works here.
Here also. I had it wrong in my memory, please excuse me :oops:

stroked
Posts: 20
Joined: 29 Apr 2013 21:48

Re: Batch file deleting itself issue

#7 Post by stroked » 11 May 2013 17:47

cheated added the line

Code: Select all

attrib +r nfo2dir2.bat

and condition

Code: Select all

if EXIST nfo2dir2 (
 rd /s /q nfo2dir2
)

stroked
Posts: 20
Joined: 29 Apr 2013 21:48

Re: Batch file deleting itself issue

#8 Post by stroked » 11 May 2013 17:51

Aacini wrote:The answer to the question: "How a Batch file can delete itself?" is:

Place the DEL command (or any other command that remove the Batch file from its location) in a line that also terminate the execution:

Code: Select all

del myself.bat & exit /B

Another possible way is place the DEL command in the last line of the Batch file and not terminate this line with CR+LF

Antonio

u misunderstood q, thats a quicker way to achieve what i inadvertently did!

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Batch file deleting itself issue

#9 Post by dbenham » 19 May 2013 10:39

Aacini wrote:Place the DEL command (or any other command that remove the Batch file from its location) in a line that also terminate the execution:

Code: Select all

del myself.bat & exit /B

Another possible way is place the DEL command in the last line of the Batch file and not terminate this line with CR+LF

Both of the above successfully delete the batch script, but I get an ugly "The batch file cannot be found." error message as well.

The trick is to start a hidden second process that actually performs the delete. The following works for me. The EXIT /B is probably not necessary, but I put it in to maximize the probability that the batch script will close before the STARTed process attempts to delete the file.

Code: Select all

start /b "" cmd /c del "%~f0"&exit /b


Dave Benham

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Batch file deleting itself issue

#10 Post by Dos_Probie » 06 Jun 2013 10:55

Thanks for the tip Dave, been using below command but you do get a short flash error as it deletes the batch file, will now start using yours DP

Code: Select all

del /F /Q %0%

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: Batch file deleting itself issue

#11 Post by carlos » 06 Jun 2013 22:08

you can delete the batch script itself with this:

Code: Select all

(
del "%~f0"
)


with the parenthesis you charge the instructions in memory, then executed it.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file deleting itself issue

#12 Post by foxidrive » 06 Jun 2013 22:44

I tried this in Windows 8

Code: Select all

@echo off
echo here
(
del "%~f0"
)
echo there


and had an error message


here
The batch file cannot be found.



But Dave's code works cleanly.

Code: Select all

start /b "" cmd /c del "%~f0"&exit /b

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Batch file deleting itself issue

#13 Post by dbenham » 08 Jun 2013 22:29

Here is another way for a batch file to cleanly delete itself without any visible error.

Create and transfer control to another temp batch file that deletes itself as well as the caller, but don't use CALL, and redirect stderr to nul.

Code: Select all

>"%~f0.bat" echo del "%~f0" "%~f0.bat"
2>nul "%~f0.bat"


Dave Benham

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Batch file deleting itself issue

#14 Post by dbenham » 06 Jul 2015 10:02

I've discovered the best way yet to have a batch script delete itself without generating an error. Assuming your script is at the root CALL level:

Code: Select all

(goto) 2>nul & del "%~f0"

It relies on the fact that (GOTO) 2>NUL behaves like EXIT /B, except it allows concatenation of subsequent commands that execute in the context of the caller 8)

The GOTO behavior is described at viewtopic.php?f=3&t=6491


Dave Benham

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file deleting itself issue

#15 Post by foxidrive » 06 Jul 2015 10:59

Spiffy! As a UK citizen might say.

It works in Win 8.1 just fine.

Post Reply