1

Ok. I have this batch file that is designed to remove every file and folder from inside a folder that may or may not be present. The basic structure Follows:

@echo off
If EXIST c:\MyDirectory\(
chdir c:\MyDirectory\

echo %CD%
echo %0
echo %~dp0

rem ... This removes everything from the folder....
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
)

The reason for the echoes in I'd like to be able to ensure the batch script has actually changed the path to MyDirectory and is deleting the correct files(we had an incident earlier that I'm kindof in hot water for where the script didn't change paths and deleted everything from one of our docs folders).

The echoes return the either the name of the batch file itself, or the path the batch file was run from,instead of "c:\MyDirectory\". So in my case it's from c:\Testing\ (a dummy directory I created to avoid a second oops).

Is there a way to get the currently active directory from inside a batch script, so that I can verify the directory I'm about to empty??

Rocky
  • 182
  • 1
  • 1
  • 9
  • 1
    No need to echo. Just use the `CD` command with no parameters. You may want to consider using PUSHD and POPD. Or at least using the /D switch with your change directory. – Squashman Jan 12 '16 at 15:31
  • Duplicate of: [Batch file. Delete all files and folders in a directory](http://stackoverflow.com/questions/6836566/). But don't use the accepted answer there. – Bill_Stewart Jan 12 '16 at 16:45

2 Answers2

1

Try this:

@echo off
setlocal EnableDelayedExpansion
If EXIST c:\MyDirectory\(
chdir c:\MyDirectory\

echo !CD!
pause

rem ... This removes everything from the folder....
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
)

Since you're in an if statement you should use setlocal EnableDelayedExpansion and use ! instead of % for variables.

Dennis van Gils
  • 3,487
  • 2
  • 14
  • 35
0

To nuke the contents of a directory, use this shell script (batch file):

@echo off
setlocal enableextensions
if {%1}=={} goto :HELP
if {%1}=={/?} goto :HELP
goto :START

:HELP
echo Usage: %~n0 directory-name
echo.
echo Empties the contents of the specified directory,
echo WITHOUT CONFIRMATION. USE EXTREME CAUTION!
goto :DONE

:START
pushd %1 || goto :DONE
rd /q /s . 2> NUL
popd

:DONE
endlocal
Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62
  • The question is not how to nuke directory contents, it is how to verify that batch script is currently operating inside the desired directory so that only what I want to remove gets removed. -1 for irrelevant answer. – Rocky Jan 14 '16 at 20:05
  • The script does that in this line: `pushd %1 || goto :DONE`. If the `pushd` fails then the `rd` command never runs. – Bill_Stewart Jan 14 '16 at 20:46
  • I don't like the idea of using an exception to control data flow. This is generally considered a very bad practice, isn't it? Additionally, the script I need cannot accept any arguments, which is why it is written the way it is. Also, as I previously stated, the part of my script that actually does the "nuking" works fine, I just needed to be able to verify the working directory, not rewrite the entire script. – Rocky Jan 20 '16 at 14:50
  • "Bad practice" is a judgment call with regards to batch file scripting. As an aside, if your question is about how to find out the current working directory, just use the `%CD%` dynamic variable. – Bill_Stewart Jan 20 '16 at 15:06