Sdel - speedy deletion utility

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
batchcc
Posts: 137
Joined: 17 Aug 2015 06:05
Contact:

Sdel - speedy deletion utility

#1 Post by batchcc » 06 Nov 2015 14:26

Moderator comment: This code deletes files but not securely, as the file will still exist untouched on the drive and can be unerased with any tool for the purpose.

http://www.howtogeek.com/234683/why-you ... o-instead/

sdel.bat Most recent version

Code: Select all

@echo off
title SDEL - speedy deltetion
:again
cls
echo  SDEL - Speedy delete program
echo.
echo    F  -  Delete only one file
echo    D  -  Delete all files in this directory (folder)
echo    E  -  Exit this batch
echo.
set /p o=Please select a option:
if /i %o%==F goto file
if /i %o%==D goto folder
if /i %o%==E goto x
goto again

rem Delete only one file
:file
set /p f=Enter path of file including extension:
If exist %f% goto del else goto f && echo path invalid
:del
set file=%f%
FOR /f %%i IN ("%file%") DO (

set filepath=%%~pi

)
cd /d %filepath%
type nul> %f%
ren %f% sdel%random%.sdel
del *.sdel
goto x

rem Delete all files in this directory (folder)
:folder
set /p d=enter path of folder to delete:

cd /d %d%
for %%a in (*.*) do type nul > %%a
for %%a in (*.*) do ren sdel%random%.sdel %%a
for %%a in (*.*) do del *.sdel %%a
goto :x

rem Exit this batch
:x
echo coded by Batchcc
:: Special thanks to Trebor68 for helping.
:: http://www.dostips.com/forum/viewtopic.php?p=43862#p43862
:: this file may be redistributed but do NOT remove the credits.
echo deletion complete...
pause
Last edited by batchcc on 20 Dec 2015 14:18, edited 6 times in total.

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: type nul and ren *.*

#2 Post by trebor68 » 06 Nov 2015 22:29

In a batch file some logical errors are present.
With the SET command input is required. But what happens if you do not "d" or "f" typing or no file should be deleted?
If you have the part "f" processed immediately follows the part "d". This has been certainly not intended by you.
Please look at my code. My batch file displays only text.

If you run the batch file, then you see the desired commands.

Code: Select all

@echo off
:again
cls
echo  SDEL - Secure delete program
echo.
echo    F  -  Delete only one file
echo    D  -  Delete all files in this directory (folder)
echo    E  -  Exit this batch
echo.
set /p o=Please select a option:
if /i %o%==F goto file
if %o%==d goto folder
if %o%==D goto folder
if /i %o%==E goto exitbatch
goto again

rem Delete only one file
:file
echo.
echo   #  Commands - for deleting one file
echo  type nul^> file.ext
echo.
pause
goto :eof

rem Delete all files in this directory (folder)
:folder
echo.
echo   #  Commands - for deleting all files in a folder
echo  for %%%%a in ^(*.*^) do type nul^> %%%%a
echo.
pause
goto :eof

rem Exit this batch
:exitbatch
echo.
echo Exit this batch without deleting files.
pause
goto :eof

batchcc
Posts: 137
Joined: 17 Aug 2015 06:05
Contact:

Re: type nul and ren *.*

#3 Post by batchcc » 07 Nov 2015 14:55

Thank you trebor but the section for deleting an entire folder doesn't work on my computer any ideas how to fix this and yes I removed the echo's.

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: type nul and ren *.*

#4 Post by trebor68 » 07 Nov 2015 16:27

Code for one file:

Code: Select all

type nul > file.ext


Code for all files in a folder:

Code: Select all

for %%a in (*.*) do type nul > %%a

batchcc
Posts: 137
Joined: 17 Aug 2015 06:05
Contact:

Re: type nul and ren *.*

#5 Post by batchcc » 08 Nov 2015 13:41

Thank you trebor I have one more question is it possible to turn this into a self compiled hybrid as seen here viewtopic.php?p=35221#p35221
So it can be run from the command line with a syntax like this

Code: Select all

Sedel /d or /f %filePath%\file.ext 

batchcc
Posts: 137
Joined: 17 Aug 2015 06:05
Contact:

Re: Sdel secure speedy deletion utility

#6 Post by batchcc » 11 Nov 2015 11:06

I tried to make a code that can run as a hybrid from cmd but it doesn't work its modeled off this self compiled hybrid as seen here viewtopic.php?p=35221#p35221 does anyone know how to fix this ? thanks


Code: Select all

@if (@X)==(@Y) @end /* JScript comment
@echo off
setlocal

for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d  /o:-n "%SystemRoot%\Microsoft.NET\Framework\*jsc.exe"') do (
   set "jsc=%%v"
)

if not exist "%~n0.exe" (
   "%jsc%" /nologo /out:"%~n0.exe" "%~dpsfnx0"
)

%~n0.exe %*

endlocal & exit /b %errorlevel%

*/

import System;

var arguments:String[] = Environment.GetCommandLineArgs();

var newLine = false;
var output = "";
var foregroundColor = Console.ForegroundColor;
var backgroundColor = Console.BackgroundColor;
var evaluate = false;
var currentBackground=Console.BackgroundColor;
var currentForeground=Console.ForegroundColor;


//http://stackoverflow.com/a/24294348/388389
var jsEscapes = {
  'n': '\n',
  'r': '\r',
  't': '\t',
  'f': '\f',
  'v': '\v',
  'b': '\b'
};

function decodeJsEscape(_, hex0, hex1, octal, other) {
  var hex = hex0 || hex1;
  if (hex) { return String.fromCharCode(parseInt(hex, 16)); }
  if (octal) { return String.fromCharCode(parseInt(octal, 8)); }
  return jsEscapes[other] || other;
}

function decodeJsString(s) {
  return s.replace(
      // Matches an escape sequence with UTF-16 in group 1, single byte hex in group 2,
      // octal in group 3, and arbitrary other single-character escapes in group 4.
      /\\(?:u([0-9A-Fa-f]{4})|x([0-9A-Fa-f]{2})|([0-3][0-7]{0,2}|[4-7][0-7]?)|(.))/g,
      decodeJsEscape);
}


function printHelp( ) {
   print( arguments[0] + "  [path] [file name + extension] -d or -f" );
   print( " " ); 
   print( "   -d             Deletes all files in a directory " );
   print( "   -f             Deletes one specifies file ");
   print( "" );
 
   

if ( arguments.length == 1 || arguments[1].toLowerCase() == "-help" || arguments[1].toLowerCase() == "-help"   ) {
   printHelp();
   Environment.Exit(0);
}

   if ( arguments[arg].toLowerCase() == "-f" ) {
      evaluate=true;
   }
   
   if ( arguments[arg].toLowerCase() == "-d" ) {
      output=arguments[arg+1];
   }
   

ShadowThief
Expert
Posts: 974
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: type nul and ren *.*

#7 Post by ShadowThief » 11 Nov 2015 14:37

batchcc wrote:Thank you trebor I have one more question is it possible to turn this into a self compiled hybrid as seen here viewtopic.php?p=35221#p35221
So it can be run from the command line with a syntax like this

Code: Select all

Sedel /d or /f %filePath%\file.ext 

You don't need to make the script a hybrid to run it like that; just put it in one of the folders of your system path.

penpen
Expert
Posts: 1846
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Sdel secure speedy deletion utility

#8 Post by penpen » 11 Nov 2015 19:00

batchcc wrote:does anyone know how to fix this ?
You haven't closed the method body of function "printHelp" (missing '}' character), and
the variable "arg" within the last two "if condition" is undefined, so
this might help you (untested):

Code: Select all

@if (@X)==(@Y) @end /* JScript comment
@echo off
setlocal

for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d  /o:-n "%SystemRoot%\Microsoft.NET\Framework\*jsc.exe"') do (
   set "jsc=%%v"
)

if not exist "%~n0.exe" (
   "%jsc%" /nologo /out:"%~n0.exe" "%~dpsfnx0"
)

%~n0.exe %*

endlocal & exit /b %errorlevel%

*/

import System;

var arguments:String[] = Environment.GetCommandLineArgs();

var newLine = false;
var output = "";
var foregroundColor = Console.ForegroundColor;
var backgroundColor = Console.BackgroundColor;
var evaluate = false;
var currentBackground=Console.BackgroundColor;
var currentForeground=Console.ForegroundColor;


//http://stackoverflow.com/a/24294348/388389
var jsEscapes = {
  'n': '\n',
  'r': '\r',
  't': '\t',
  'f': '\f',
  'v': '\v',
  'b': '\b'
};

function decodeJsEscape(_, hex0, hex1, octal, other) {
  var hex = hex0 || hex1;
  if (hex) { return String.fromCharCode(parseInt(hex, 16)); }
  if (octal) { return String.fromCharCode(parseInt(octal, 8)); }
  return jsEscapes[other] || other;
}

function decodeJsString(s) {
  return s.replace(
      // Matches an escape sequence with UTF-16 in group 1, single byte hex in group 2,
      // octal in group 3, and arbitrary other single-character escapes in group 4.
      /\\(?:u([0-9A-Fa-f]{4})|x([0-9A-Fa-f]{2})|([0-3][0-7]{0,2}|[4-7][0-7]?)|(.))/g,
      decodeJsEscape);
}


function printHelp( ) {
   print( arguments[0] + "  [path] [file name + extension] -d or -f" );
   print( " " ); 
   print( "   -d             Deletes all files in a directory " );
   print( "   -f             Deletes one specifies file ");
   print( "" );
}
   

if ( arguments.length == 1 || arguments[1].toLowerCase() == "-help" || arguments[1].toLowerCase() == "-help"   ) {
   printHelp();
   Environment.Exit(0);
}


for (var arg = 0; arg < arguments.length; ++arg) {
   if ( arguments[arg].toLowerCase() == "-f" ) {
      evaluate=true;
   }
   
   if ( arguments[arg].toLowerCase() == "-d" ) {
      output=arguments[arg+1];
   }
}
Note: I might have miss an error.
It is better you open a command shell ("cmd.exe)" and execute your batch there;
the compiler then should give you error messages.


penpen

batchcc
Posts: 137
Joined: 17 Aug 2015 06:05
Contact:

Re: Sdel secure speedy deletion utility

#9 Post by batchcc » 12 Nov 2015 15:56

Thank you pen pen so much :) I am sorry for any mistakes I made but I am not very good with other programming languages
I will tell you if it works but my pc isnt working right now thanks again.

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Sdel secure speedy deletion utility

#10 Post by trebor68 » 12 Nov 2015 19:02

I have the code adjusted so that this batch file can be started with parameters.
Before the echo commands are removed, the security query should be installed.
It should also be checked, what happens when a placeholder '*' and '?' in the file name or folder name are available. It when the placeholders are excluded would be best.

Code: Select all

@echo off
setlocal EnableExtensions
if "%1"=="" goto :help
if "%~2"=="" goto :help

set test=%~2
set test=%test:**=%
set test=%test:?=%
if /i not "%test%"=="%~2" (echo ERROR - Wildcards are not allowed.) & goto :eof

if /i "%1"=="/F" goto :file
if /i "%1"=="/D" goto :folder

:help
echo.
echo  SDEL - Secure delete program
echo.
echo    SDEL /F file.ext
echo    SDEL /D folder
echo.
echo    /F       Delete only one file
echo    /D       Delete all files in this directory (folder)
echo.
echo   file.ext  filename with extension
echo   folder    foldername
echo.
goto :eof

rem Delete only one file
:file
if not exist %2 (echo ERROR - File is not exist.) & (echo File: %2) & goto :eof
echo.
echo File: %~f2
echo.
echo   #  Commands - Query whether the file should be deleted.
echo.
echo   #  Commands - for deleting one file
echo  type nul^> file.ext
echo.
goto :eof

rem Delete all files in this directory (folder)
:folder
if not exist %2\nul (echo ERROR - Folder is not exist.) & (echo Folder: %2) & goto :eof
:: set foldername=%~f2
echo.
echo Folder: %~f2
echo Files : %~f2\*.*
echo.
echo   #  Commands - Query whether the files in the folder should be deleted.
echo.
echo   #  Commands - for deleting all files in a folder
echo  for %%%%a in ^(*.*^) do type nul^> %%%%a
echo.
goto :eof

batchcc
Posts: 137
Joined: 17 Aug 2015 06:05
Contact:

Re: Sdel secure speedy deletion utility

#11 Post by batchcc » 13 Nov 2015 14:01

sorry penpen its not working

Code: Select all

H:\sdel>sdel H:\sdel\1 -d

Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at JScript 0.Global Code()
   at JScript Main.Main(String[] )

H:\sdel>



penpen
Expert
Posts: 1846
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Sdel secure speedy deletion utility

#12 Post by penpen » 13 Nov 2015 16:40

As said, i only fixed the (syntax) errors i saw.

So especially i didn't search for logical errors in your algorithm, because
i'm unsure, what this algorithm is intended to do in detail;
in addition i assumed it is unfinished for now
=> there might be no logical errors but only some missing code.

Probably these lines causes the error (if i have to guess, i would say to replace the "+" with a "-" character):

Code: Select all

   if ( arguments[arg].toLowerCase() == "-d" ) {
      output=arguments[arg+1];
   }


Sidenote:
The help information and your invocation example both contradict to your algorithm.
This better could be seen, when translating your "help info" into MS notation (see Command Line Syntax Key):

Code: Select all

:: help info (ambiguous, but probably the following)
:: sdel [path] [file name + extension] -d or -f
sdel <Path> {-d|<FileName> -f}

:: your example
:: sdel H:\sdel\1 -d
sdel <Path> -d

:: algorithm (currently ambiguous; param after "-d"-switch must be present)
:: most probably:
sdel {-d <Path>|<Path> <FileName> -f}

:: but these are (theoretically) possible, too
sdel <Path> {-d <FileName>|<FileName> -f}
sdel {-d <Path> <FileName>|<Path> <FileName> -f}
:: ...
sdel <Path> -{d|f} <FileName>
: ...
sdel -{d|f} <Path> <FileName>


penpen

batchcc
Posts: 137
Joined: 17 Aug 2015 06:05
Contact:

Re: Sdel secure speedy deletion utility

#13 Post by batchcc » 16 Dec 2015 14:17

Can anyone help fix the "d" option it isnt working and i'm not sure why here is the updated code

Code: Select all

@echo off
title SDEL - secure, speedy deltetion
:again
cls
echo  SDEL - Secure delete program
echo.
echo    F  -  Delete only one file
echo    D  -  Delete all files in this directory (folder)
echo    E  -  Exit this batch
echo.
set /p o=Please select a option:
if /i %o%==F goto file
if /i %o%==D goto folder
if /i %o%==E goto x
goto again

rem Delete only one file
:file
set /p f=Enter path of file including extension:
If exist %f% goto del else goto f && echo path invalid
:del
set file=%f%
FOR /f %%i IN ("%file%") DO (

set filepath=%%~pi

)
cd /d %filepath%
type nul> %f%
ren %f% sdel%random%.sdel
del *.sdel
goto x

rem Delete all files in this directory (folder)
:folder
set /p d=enter path of folder to delete:

cd /d %d%
for %%a in (*.*) do type nul > %%a
for %%a in (*.*) do ren sdel%random%.sdel %%a
for %%a in (*.*) do del *.sdel %%a
goto :x

rem Exit this batch
:x
echo coded by Batchcc
:: Special thanks to Trebor68 for helping.
:: http://www.dostips.com/forum/viewtopic.php?p=43862#p43862
:: this file may be redistributed but do NOT remove the credits.
echo deletion complete...
pause

thanks

Squashman
Expert
Posts: 4180
Joined: 23 Dec 2011 13:59

Re: Sdel secure speedy deletion utility

#14 Post by Squashman » 16 Dec 2015 15:09

Code: Select all

cd /d %d%
for %%a in (*.*) do type nul > %%a
for %%a in (*.*) do ren sdel%random%.sdel %%a
for %%a in (*.*) do del *.sdel %%a

You should really get into the habit of using "Quotes" around your file names and paths. Don't you have the rename backwards. You want to rename the original name to a random name. Not sure why you would then try to delete the original directory. You already renamed it. You should use the RMDIR command to delete it.

batchcc
Posts: 137
Joined: 17 Aug 2015 06:05
Contact:

Re: Sdel secure speedy deletion utility

#15 Post by batchcc » 16 Dec 2015 15:12

like this? and it still doesn't work.

Code: Select all

cd /d %d%
for %%a in (*.*) do type nul > %%a
for %%a in (*.*) do ren "sdel%random%.sdel" %%a
for %%a in (*.*) do del *.sdel %%a

Post Reply