问题
I make the following directory at the beginning of my BATCH FILE to store temporary content:
"%userprofile%\Downloads\VersionCheckWillDelete"
I wish to remove this folder and all of its contents.
The following removes only the data in the folder but not the folder itself:
RD /S /Q "%userprofile%\Downloads\VersionCheckWillDelete"
EDIT: Turns out that it isn't deleting it because the BATCH file is still using it. I have considered POWERSHELL being still open and trying to kill it but it isn't open. How do I close it in the background. It doesn't need to be running anymore? "The process cannot access the file because it is being used by another process."
THE FOLLOWING IS MY CODE:
@echo off
:version_chk
SET USER_VER=1.1.2-RC
ECHO * * * * * * * * * * * * *
ECHO CHECKING FOR UPDATES...
ECHO * * * * * * * * * * * * *
cd "%userprofile%\Downloads"
mkdir "%userprofile%\Downloads\VersionCheckWillDelete"
cd "%userprofile%\Downloads\VersionCheckWillDelete"
powershell -Command "Invoke-WebRequest https://github.com/KSanders7070/AIRPORT_ALIAS_CREATION/blob/master/Version_Check -OutFile 'version_check.HTML'"
findstr "VERSION-" "version_check.HTML" >> "GitHub_Version.txt"
cd "%userprofile%\Downloads\VersionCheckWillDelete"
for /f "delims=" %%x in (GitHub_Version.txt) do set GH_VER_STRING_ORIGINAL=%%x
for /f "tokens=2 delims=<" %%x in ("%GH_VER_STRING_ORIGINAL%") do set GH_VER_STRING_FIRST_HALF=%%x
SET "GH_VER=%GH_VER_STRING_FIRST_HALF:*VERSION-=%"
if %USER_VER% == %GH_VER% GOTO NORMAL_PROCESS
:UPDATE_AVAIL
CLS
START "" "https://github.com/KSanders7070/AIRPORT_ALIAS_CREATION/releases"
ECHO * * * * * * * * * * * * *
ECHO UPDATE AVAILABLE
ECHO * * * * * * * * * * * * *
ECHO.
ECHO.
ECHO GITHUB VERSION: %GH_VER%
ECHO YOUR VERSION: %USER_VER%
ECHO.
ECHO.
ECHO PLEASE CLOSE THIS BATCH FILE AND GO DOWNLOAD THE MOST RECENT UPDATE AVAILABLE:
ECHO.
ECHO https://github.com/KSanders7070/AIRPORT_ALIAS_CREATION/releases
ECHO.
ECHO.
ECHO OR YOU MAY PRESS ANY KEY TO CONTINUE USING THIS VERSION.
ECHO.
ECHO.
cd "%userprofile%\Downloads"
rd /S /Q "%userprofile%\Downloads\VersionCheckWillDelete\"
PAUSE
:NORMAL_PROCESS
CLS
cd "%userprofile%\Downloads"
rd /S /Q "%userprofile%\Downloads\VersionCheckWillDelete\"
ECHO Your BATCH file is the most up to date version.
PAUSE
回答1:
SOLUTION: Removing the START "URL" line allowed the BATCH to delete the folder. Not sure why it was dependent on Chrome but it solves the issue. So, placing the RD command before the START "" command makes everything work for me.
回答2:
In order to remove directory you need to use this command.
rmdir path
del -f path
回答3:
There was absolutely no reason to change directory to %UserProfile%\Downloads\VersionCheckWillDelete
, in the first instance. Additionally, there's probably no reason to use the Downloads
location when that is one of the purposes of the %Temp%
directory. Regardless of which you choose, you only really need it for the version_check.html
file you're parsing, so why not put it directly into the chosen location, instead of creating a new directory and removing it afterwards. (That way you only need to delete the file if necessary, I would expect that on each run, the file would be overwritten anyhow).
That said, this is probably how I would do what you're doing:
@Echo Off
Set "USER_VER=1.1.2-RC"
Echo * * * * * * * * * * * * *
Echo CHECKING FOR UPDATES...
Echo * * * * * * * * * * * * *
Echo(
If Not Exist "%UserProfile%\Downloads\VersionCheckWillDelete\" MD "%UserProfile%\Downloads\VersionCheckWillDelete"
%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "Invoke-WebRequest -URI \"https://github.com/KSanders7070/AIRPORT_ALIAS_CREATION/blob/master/Version_Check\" -OutFile \"%UserProfile%\Downloads\VersionCheckWillDelete\version_check.html\""
For /F "Tokens=3 Delims=><" %%G In ('%__AppDir__%findstr.exe ">VERSION-[0-9]" "version_check.html"') Do For /F "Tokens=1* Delims=-" %%H In ("%%G") Do Set "GH_VER=%%I"
RD "%UserProfile%\Downloads\VersionCheckWillDelete"
If "%USER_VER%" == "%GH_VER%" Echo Your batch file is the most up to date version.& GoTo Continue
ClS
Echo * * * * * * * * * * * * *
Echo UPDATE AVAILABLE
Echo * * * * * * * * * * * * *
Echo(
Echo GITHUB VERSION: %GH_VER%
Echo YOUR VERSION: %USER_VER%
Echo(
Echo YOU ARE NOT USING THE LATEST VERSION!
Echo(
Echo 1. DOWNLOAD THE LATEST VERSION NOW
Echo 2. CONTINUE USING THIS OUTDATED VERSION
Echo(
%__AppDir__%choice.exe /C 12
If ErrorLevel 2 GoTo Continue
Echo Your Web Browser will now open on the Releases page and this batch file will close.
%__AppDir__%timeout.exe /T 5
Start "" "https://github.com/KSanders7070/AIRPORT_ALIAS_CREATION/releases"
GoTo :EOF
:Continue
Echo(
Echo Continuing . . .
Pause
ClS
来源:https://stackoverflow.com/questions/61803141/batch-file-delete-folder-contents-and-the-parent-folder