Why is a folder not deleted with RMDIR in batch file as last command? - batch-file

I have the following code in a .bat file:
#echo off
xcopy /Y /S %CD%\Code\Release C:\Users\%USERNAME%\Desktop\ShareIt /I
cls
cd C:\Users\%USERNAME%\Desktop\ShareIt\
call "Trabalho AEDA.exe"
xcopy /Y /S C:\Users\%USERNAME%\Desktop\ShareIt\FICHEIROS\ %CD%\Code\Release\FICHEIROS\
RMDIR /S /Q C:\Users\%USERNAME%\Desktop\ShareIt
That copies a folder to a location, runs the .exe from it and then it overwrites the original files in my folder and has do delete the ones initially copied.
The folder I copy to the user desktop has other folder inside, and the .exe.
At the final line of the .bat, it deletes everything in the folder, but the folder is kept in the Desktop folder. I want to delete it, too. I tried several instructions, but without success.
EDIT: That was the issue, thanks guys.

ShareIt folder isn't deleted probably because you are in the folder.
So, adding cd .. before RMDIR /S /Q C:\Users\%USERNAME%\Desktop\ShareIt solves it.

The main problem with deleting ShareIt folder is already answered by the other answers.
It is not possible on Windows to delete a folder which is the current working directory of any running process or in which a file is opened by an application with a file lock preventing the deletion of the opened file.
In this case the ShareIt folder is the current working directory of the command process executing the batch file because the batch file explicitly sets this directory as working directory. The solution is making any other directory the working directory for the command process.
But this is not the only potential problem of the few command lines of this batch file. There are some more.
%CD% could expand to a path which contains perhaps one or more spaces or other characters like &()[]{}^=;!'+,`~ which require enclosing complete folder path in double quotes. This list of characters is output on running in a command prompt window cmd /? in last paragraph on last output help page.
Also %CD% expands to a folder path usually not ending with a backslash, except the current directory is the root directory of a drive. So %CD%\Code\Release could for example expand to C:\\Code\Release with two backslashes in path. However, this is no real problem as Windows kernel corrects the path automatically on whatever file system access function is used internally by xcopy for copying the files and folders.
Parameter /I lets xcopy interpret the target string as folder path even if folder path is not ending with a backslash, but only if multiple files are copied like when source path is a folder path like it is here obviously the case. Otherwise on copying just a single file /I is ignored by xcopy. Best for a folder path as target is specifying the target folder path with a backslash as then xcopy interprets target always as folder path. It is easier to use here paths relative to current directory and omit %CD% completely.
C:\Users\%USERNAME% is not good as the user's profile directory can be also on a different drive than C: and can be in a different directory than Users, for example on Windows XP. The path C:\Users\%USERNAME% is the default for the user's profile directory since Windows Vista. But every user has the freedom to change it also on Windows Vista and later Windows versions. There is the environment variable USERPROFILE defined by Windows which contains full path to active user's profile directory. See Wikipedia article about Windows Environment Variables for more details about predefined Windows environment variables.
And of course the user's profile directory path could contain one or more spaces or other characters which again require double quotes around complete folder path or file name, for example if the user account name contains a space character. So again double quotes should be used wherever %USERPROFILE% or %USERNAME% is used in a folder or file name with path.
Command CD without parameter /D changes the current directory only on current drive if the new current directory exists at all on current drive. So with current directory on starting the batch file being on a different drive than drive C:, changing the current directory with used code would not work at all.
The command CALL should not be necessary at all in case of Trabalho AEDA.exe is really an executable as the file extension indicates. CALL can be used also for an executable, but it is designed primary for running a subroutine or calling another batch file from within a batch file.
The last two lines do not really make sense because the current directory was changed by the batch file to ShareIt folder in desktop folder of current user. Therefore %CD%\Code\Release\FICHEIROS\ expands to
C:\Users\%USERNAME%\Desktop\ShareIt\Code\Release\FICHEIROS\
as target path for xcopy and the last line deletes the entire folder
C:\Users\%USERNAME%\Desktop\ShareIt
That is obviously not the intention. xcopy in last but one line should copy the files to a subdirectory in initial current directory.
I suggest to use this batch code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
%SystemRoot%\System32\xcopy.exe Code\Release "%USERPROFILE%\Desktop\ShareIt\" /C /Q /R /S /Y >nul
pushd "%USERPROFILE%\Desktop\ShareIt"
if not errorlevel 1 "Trabalho AEDA.exe" & popd
%SystemRoot%\System32\xcopy.exe "%USERPROFILE%\Desktop\ShareIt\FICHEIROS" Code\Release\FICHEIROS\ /C /Q /R /S /Y >nul
setlocal EnableDelayedExpansion
if "!CD:%USERPROFILE%\Desktop\ShareIt=!" == "!CD!" RMDIR /S /Q "!USERPROFILE!\Desktop\ShareIt"
endlocal
endlocal
The last IF conditions needs most likely an additional explanation for what is its purpose.
!CD:%USERPROFILE%\Desktop\ShareIt=! results in searching case-insensitive in current directory path string for all occurrences of the path string to ShareIt directory in desktop directory of current user and replacing all found occurrences by an empty string. So it depends on what is the current directory on what happens here.
In case of current directory is %USERPROFILE%\Desktop\ShareIt, the string left of comparison operator == becomes "". In case of current directory is a subdirectory of %USERPROFILE%\Desktop\ShareIt, the string left of == becomes a relative path to this directory enclosed in double quotes. In all other cases the string left of == is not modified at all and expands to path of current directory enclosed in double quotes like the string right of the comparison operator.
So the IF condition checks if the current directory is NOT the ShareIt directory or a subdirectory of this directory in the desktop directory of current user. Only in this case it is safe to delete the entire ShareIt directory.
Note: The IF condition does not work correct on %USERPRFOLE% expands to a folder path string containing one or more ! because of enabled delayed variable expansion.
For understanding all other used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
cd /?
echo /?
endlocal /?
if /?
popd /?
pushd /?
set /?
setlocal /?
xcopy /?
There should be read also the Microsoft documentation about Using command redirection operators for an explanation of >nul.

Since you execute
cd C:\Users\%USERNAME%\Desktop\ShareIt\
It's the current directory when you execute the now delete the directory command.
Move to another directory, and then try the deletion

Related

Batch file to remove specific subfolders but move everything inside them to the outside first

I am looking for help with a batch script which can help me remove specific subfolders from all directories that are in the same place as the batch file. But instead of deleting everything in the subdirectory, I want to move the contents outside of the subdirectory first.
To elaborate, I receive files which are always nested inside folders with a very specific pattern:
Every request I receive is in a uniquely named folder which follows no specific pattern (the Job Folder).
Inside every Job Folder, there are two things:
A folder denoting the language the file is in (Lang Folder).
An xml file named "Manifest.xml" which contains instructions and metadata.
There is a folder inside every Lang Folder denoting the brand of the file (Brand Folder).
Inside every Brand Folder is a random assortment of subfolders containing JSON files with various degrees of nesting inside different subfolders.
So a typical request would contain the following structure:
Job Folder/Lang Folder/Brand Folder/...
And I want to transform all folders to follow this structure:
Job Folder/Brand Folder/...
You can see an example below of how the folder structure currently looks and how I would like it to look in the end.
Structure of the folders and files before running the batch file:
French_Job1373
French
BrandA
Subfolder
Example.json
Manifest.xml
German_Job1374
German
BrandB
Subfolder1
Subfolder2
Subfolder3
ExampleFile.json
Manifest.xml
Japanese_Job1375
Japanese
BrandC
Subfolder1
Example.json
Manifest.xml
Korean_Job1376
Korean
BrandC
Subfolder1
Subfolder13
ExampleSrc.json
Manifest.xml
Structure of the folders and files as it should be after running the batch file:
French_Job1373
BrandA
Subfolder
Example.json
Manifest.xml
German_Job1374
BrandB
Subfolder1
Subfolder2
Subfolder3
ExampleFile.json
Manifest.xml
Japanese_Job1375
BrandC
Subfolder1
Example.json
Manifest.xml
Korean_Job1376
BrandC
Subfolder1
Subfolder13
ExampleSrc.json
Manifest.xml
I would like to have a batch file which essentially removes the Lang Folder, pulling everything from inside the Lang Folder one level up.
Following should be taken into consideration:
The file Manifest.xml should not be touched. It should remain inside the Job Folder level.
Everything from the Brand Folder level including the Brand Folder itself should be moved one level up (to the same level where the file Manifest.xml is).
The Language Folder (which at this point should be empty) should be deleted.
There is a finite list of possible languages after which the Lang Folder is named. If required to specify all possible languages in the batch file, I would like the option to add new languages to the list in the future.
So far, I have managed to get the batch file below to work as expected, but I need to run as many times as there are Job Folders and I have to place it inside the Language Folder to have it work as expected. If I place it anywhere else, it just deletes the Lang Folder with all of its contents and does not move anything. I am looking to have the batch file check all folders that are next to it and perform the operation as many times as needed.
What I have so far:
#echo off
if -%1==- echo No parameters! You must add %%P parameter! & pause & goto :EOF
cd /d %1
move * ..
for /d %%f in (*) do move %%f ..
cd ..
"%commander_path%\totalcmd.exe" /o /s %1\..
rd %1
FOR /d /r . %%d IN (Russian) DO #IF EXIST "%%d" rd /s /q "%%d"
FOR /d /r . %%d IN (French) DO #IF EXIST "%%d" rd /s /q "%%d"
FOR /d /r . %%d IN (German) DO #IF EXIST "%%d" rd /s /q "%%d"
FOR /d /r . %%d IN (Japanese) DO #IF EXIST "%%d" rd /s /q "%%d"
That is an excellent description of the folder moving task which can be done with a batch file stored in the folder containing all the Job Folders containing the following lines.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ErrorPause="
for /D %%I in ("%~dp0*_Job*") do for /F "eol=| delims=_" %%J in ("%%~nxI") do if exist "%%I\%%J\" (
for /F "eol=| delims=" %%K in ('dir "%%I\%%J" /A /B 2^>nul') do move "%%I\%%J\%%K" "%%I\" 2>nul || (echo Failed to move: "%%I\%%J\%%K"& set "ErrorPause=1")
rd "%%I\%%J" 2>nul || (echo Failed to delete: "%%I\%%J"& set "ErrorPause=1")
)
if defined ErrorPause pause
endlocal
The first FOR loop searches in folder of the batch file referenced with %~dp0 (path ends always with a backslash) for non-hidden subfolders matching the wildcard pattern *_Job*. So assigned to loop variable I are one after the other the full qualified folder names of the Job Folders French_Job1373, German_Job1374, German_Job1374, Korean_Job1376, ...
The second FOR command splits up the current Job Folder name on underscores with everything up to first underscore assigned to loop variable J. That is the name of the Lang Folder in current Job Folder.
The IF condition checks if the Lang Folder in current Job Folder exists at all as otherwise there is nothing to do for the current Job Folder.
The third FOR loop starts one more command process in background with %ComSpec% /c and the command line within ' appended as additional arguments. So there is executed with Windows installed into C:\Windows and batch file path being C:\Temp, for example, for the first Lang Folder of first Job Folder:
C:\Windows\System32\cmd.exe /c dir "C:\Temp\French_Job1373\French" /A /B 2>nul
The command DIR outputs
all names of the folders and files including hidden ones because of option /A (all attributes)
in bare format because of option /B which means just folder/file name without path
in the specified directory.
FOR ignores folders and files with hidden attribute set which is the reason for using the DIR command line executed by a separate command process in the background.
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
FOR with option /F would split up by default all lines captured from handle STDOUT of background command process after started cmd.exe terminated itself into substrings using normal space and horizontal tab as string delimiters, would ignore next the line if the first substring starts with a semicolon being the default end of line character, and would otherwise assign just the first space/tab separated string to specified loop variable K. That behavior would be no problem according to the example for the folders BrandA, BrandB and two times BrandC. But the usage of the option string "eol=| delims=" results in using a vertical bar as end of line character which no folder/file name can contain and the definition of an empty list of string delimiters which disables line splitting behavior. So each folder/file name output by DIR without path is assigned completely to the loop variable K one after the other.
The command MOVE moves the current folder/file in Lang Folder of current Job Folder one level up into the Job Folder. This action is very fast as this is done by just updating the master file table of the file system which is cached by Windows. There is an error message output and the environment variable ErrorPause is defined if an error occurs on moving a folder or file up one level.
The current Lang Folder is removed with command RD after all folders and files in the current Lang Folder are moved up hopefully successfully. The deletion of the folder fails if a folder or file in Lang Folder could not be moved up because of a file in this folder tree is currently opened by an application, or a folder in this folder tree is the current folder of any running process, or a folder/file with same name exists already in Job Folder. An error message is output and the environment variable ErrorPause is defined on deletion of Lang Folder fails because of the folder is not empty.
The command PAUSE halts the batch file execution until a key is pressed if an error occurred. Otherwise the processing of the batch file ends without requiring any further user action.
The batch file can be simply executed once again in case of an error because of a file is opened in an application, or a folder is the current folder of a running process, or the folder/file to move exists already in Job Folder after closing the file in the application or the application itself respectively deletion of the folder/file in Job Folder.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains %~dp0 ... drive and path of argument 0 which is the batch file path.
dir /?
echo /?
endlocal /?
for /?
if /?
move /?
pause /?
rd /?
set /?
setlocal /?
All these commands are internal commands of Windows command processor cmd.exe.
See also: Single line with multiple commands using Windows batch file

Test IF file exist, ELSE xcopy these two files

Morning all.
So I've been up hours trying to cobble together -a variety of replies to other posts- into my own code in order to see if I could get something usable. No-go. I'm sufficiently lost in the sauce that I've now got to ask for some help from you.
Background:
OS: Windows 10
I use the program text2folders.exe to create 20-30 new folders on a secondary drive every night.
Primarily, I have a base file "aGallery-dl.bat" that I populate each folder with using an xcopy batch file. Secondarily, from time to time I update the source file "aGallery-dl.bat" using the same xcopy and this overwrites the older target file, populating all folders with the newest "aGallery-dl.bat" (whether they need it or not). All is well.
#echo off
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy /y /d ".\aGallery-dl.bat" "%%a\"
I've recently decided I want to add two new files to each folder and have expanded my xcopy to include these. All is well.
#echo off
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy /y /d ".\aGallery-dl.bat" "%%a\"
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy ".\Folder.jpg" "%%a\"
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy ".\Folder2.jpg" "%%a\"
Folder.jpg
a big red X
Folder2.jpg
a big yellow ! mark
When I choose to run a "aGallery-dl.bat" in a given folder (again, one of 100's), it first deletes Folder.jpg then renames Folder2.jpg to Folder.jpg. This has the effect of the red X being replaced by the yellow ! when viewing the folder in File Explorer parent folder. Secondly, it calls "gallery-dl.exe." I use going from red to yellow to let me know I've run "aGallery-dl.bat" at least once. All is well.
rem #echo off
del .\Folder.jpg
ren .\Folder2.jpg Folder.jpg
FOR /F %%i IN ('cd') DO set FOLDER=%%~nxi
"C:\Program Files (x86)\gallery-dl\gallery-dl.exe" -d "U:\11Web\gallery-dl" --download-archive ".\aGDB.sqlite3" "https://www.deviantart.com/"%FOLDER%"/gallery/all"
del .\Folder.jpg
If "aGallery-dl.bat" completes successfully, it finally deletes the Folder.jpg (currently yellow !), and now the representative contents of the folder (usually DeviantArt .jpg's) are visible.
Problem:
When I have to re-run my original xcopy command to update "aGallery-dl.bat" in ALL FOLDERS, the Folder.jpg and Folder2.jpg will be re-copied to all folders, defeating the purpose of deleting them once via "aGallery-dl.bat." I don't want to have to go back and re-run "aGallery-dl.bat" intermittently across 100's of folders (again, only those that have had aGallery-dl.bat run at least once). I need some type of test, that if "aGallery-dl.bat" is already present in the target folder, DO NOT xcopy Folder.jpg and Folder2.jpg aka vague example, below.
*********************************Some sort of test statement here!!!***********************
:aGallery-dlPresent
GOTO eof
:aGallery-dlNotPresent
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy ".\Folder.jpg" "%%a\"
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy ".\Folder2.jpg" "%%a\"
GOTO eof
:eof
I had found a hopeful candidate test statement in the below (copied in its original form from what/where I read in other post), but am looking for ideas/replacements as I HAVE NO IDEA how to modify/inject/implement the below to work in the above.
If exist \\%DIR%\%Folder%\123456789.wav xcopy \\%DIR%\%Folder%\123456789.wav D:\%New Folder%\ /y
Having XCopy copy a file and not overwrite the previous one if it exists (without prompting)
Note: The below is a vague approximation of what it should all look like (barring having a correct -test statement-).
rem #echo off
*********************************Some sort of test statement here!!!***********************
:aGallery-dlPresent
GOTO eof
:aGallery-dlNotPresent
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy ".\Folder.jpg" "%%a\"
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy ".\Folder2.jpg" "%%a\"
GOTO eof
:eof
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy /y /d ".\aGallery-dl.bat" "%%a\"
The command for copying a file is COPY. It is an internal command of Windows command processor cmd.exe. XCOPY is an eXtended file and directory copying executable in directory %SystemRoot%\System32 which is deprecated since Windows Vista as there is even more powerful ROBOCOPY which is with full qualified file name %SystemRoot%\System32\robocopy.exe.
There is no need to use XCOPY or ROBOCOPY for this simple file copying task. COPY is enough on source files aGallery-dl.bat, Folder.jpgand Folder2.jpg don't have hidden attribute set and the same files in the target directories don't have read-only attribute set.
.\ references the current directory which can be any directory. Windows Explorer sets the directory of the batch file as current directory on double clicking on a batch file. But this is nearly the only method to run a batch file on which the directory of the executed batch file is set automatically as current directory (except the batch file is stored on a network resource accessed using UNC path).
There is %~dp0 to reference the path of the batch file. This path always ends with a backslash which means that no additional backslash is needed on concatenating the batch file path with a file or folder name. The usage of %~dp0 makes it possible to reference files in same directory as the executed batch file independent on which directory is the current directory on execution of the batch file.
The batch file needed for your task is:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
for /D %%I in ("U:\11Web\gallery-dl\deviantart\*") do (
if not exist "%%I\aGallery-dl.bat" (
copy "%~dp0Folder.jpg" "%%I\"
copy "%~dp0Folder2.jpg" "%%I\"
)
copy /Y "%~dp0aGallery-dl.bat" "%%I\"
)
endlocal
A file/folder name must be enclosed in " if containing a space or one of these characters &()[]{}^=;!'+,`~. For that reason all file/folder names are enclosed in this batch file in double quotes although inside the batch files no file/folder name contains a space or one of the characters in the list. It is important to understand on batch file writing how a command line looks like after Windows command processor processed the command line. See following topics:
How does the Windows Command Interpreter (CMD.EXE) parse scripts?
How to debug a batch file?
Windows interprets *.* like just * which means any file or folder name. For that reason it is enough to just write * and omit .*.
Please note that for /D ignores directories with hidden attribute set.
The batch file checks first for each subfolder if it does not contain the batch file aGallery-dl.bat. In this case it copies the two files Folder.jpg and Folder2.jpg from directory of executed batch file to current subdirectory of U:\11Web\gallery-dl\deviantart.
Then the batch file aGallery-dl.bat is copied from directory of executed batch file to to current subdirectory of U:\11Web\gallery-dl\deviantart independent on its existence in the destination directory.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains %~dp0 ... drive and path of argument 0 ... full batch file path.
cmd /? ... outputs the help of Windows command processor executing a batch file.
copy /?
echo /?
endlocal /?
for /?
if /?
setlocal /?
See also the chapters Issue 6 and Issue 7 in this answer why using setlocal EnableExtensions DisableDelayedExpansion and endlocal although not necessary by default and why using I instead of a as loop variable although a would work here, too.

How to delete files matching a certain pattern in their name?

I'm creating a batch file that deletes all Rar$DIa0.??? folders in the %TEMP% directory.
Is this possible, and how to do it?
The ??? is three randomized numbers. And that's where I have trouble with - deleting all folders that have Rar$DIa0. in the name.
for /d is designed for just this type of use. Something like this should work (remove one of the % if you're testing from the command line):
for /d %%i in ("%TEMP%\Rar$DIa0.???") do rd "%TEMP%\%%i"
The /d makes it work on directory names instead of file names.
If you want to make it easier on yourself, change to the %TEMP% folder first:
pushdir
cd /d %TEMP%
for /d %%i in ("Rar$DIa0.???") do rd "%%i"
The ??? makes it only act on folders that have three letters after a .. If your folders don't have just a three letter extension, change .??? to .*. If you've got a typo, and there is no actual . in the foldername, just remove it and use Rar$DIa0??? or Rar$DIa0*
You may want to test it first by changing rd to echo to make sure you get the folders you want before actually deleting them.
For more information about for (pun intended) type for /? from a command prompt.
The command line to use in a batch file for this task is:
#for /D %%I in ("%TEMP%\Rar$DIa0.*") do #rd /Q /S "%%I"
Command FOR with option /D searches in folder defined by environment variable TEMP for subfolders with folder name starting with Rar$DIa0. not having hidden or system attribute set.
The loop variable I holds for each found subfolder matching this folder pattern the name of found folder with full path without double quotes although the path to temp folder very often contains 1 or more spaces.
For that reason just the command RD with the parameters /Q for quiet execution and /S for deleting also all subfolders in the specified folder must be called with referencing the current value of loop variable I enclosed in double quotes.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
for /?
rd /?
By the way: WinRAR deletes the temporary folders usually automatically, except a file is opened from within an archive for viewing/modifying it in another application and WinRAR is closed before the other application is exited with the opened file. In this case WinRAR can't delete the temporary folder with temporarily extracted file because the file is still opened in another application. Of course also command RD can't delete the temporary folder if this folder is still the current directory of another application or a file in this folder is still opened by another application with a read/write access lock.

DELTREE command windows 10 replacement [duplicate]

Say, there is a variable called %pathtofolder%, as it makes it clear it is a full path of a folder.
I want to delete every single file and subfolder in this directory, but not the directory itself.
But, there might be an error like 'this file/folder is already in use'... when that happens, it should just continue and skip that file/folder.
Is there some command for this?
rmdir is my all time favorite command for the job. It works for deleting huge files and folders with subfolders. A backup is not created, so make sure that you have copied your files safely before running this command.
RMDIR "FOLDERNAME" /S /Q
This silently removes the folder and all files and subfolders.
You can use this shell script to clean up the folder and files within C:\Temp source:
del /q "C:\Temp\*"
FOR /D %%p IN ("C:\Temp\*.*") DO rmdir "%%p" /s /q
Create a batch file (say, delete.bat) containing the above command. Go to the location where the delete.bat file is located and then run the command: delete.bat
The simplest solution I can think of is removing the whole directory with
RD /S /Q folderPath
Then creating this directory again:
MD folderPath
This will remove the folders and files and leave the folder behind.
pushd "%pathtofolder%" && (rd /s /q "%pathtofolder%" 2>nul & popd)
#ECHO OFF
SET THEDIR=path-to-folder
Echo Deleting all files from %THEDIR%
DEL "%THEDIR%\*" /F /Q /A
Echo Deleting all folders from %THEDIR%
FOR /F "eol=| delims=" %%I in ('dir "%THEDIR%\*" /AD /B 2^>nul') do rd /Q /S "%THEDIR%\%%I"
#ECHO Folder deleted.
EXIT
...deletes all files and folders underneath the given directory, but not the directory itself.
CD [Your_Folder]
RMDIR /S /Q .
You'll get an error message, tells you that the RMDIR command can't access the current folder, thus it can't delete it.
Update:
From this useful comment (thanks to Moritz Both), you may add && between, so RMDIR won't run if the CD command fails (e.g. mistyped directory name):
CD [Your_Folder] && RMDIR /S /Q .
From Windows Command-Line Reference:
/S: Deletes a directory tree (the specified directory and all its
subdirectories, including all files).
/Q: Specifies quiet mode. Does not prompt for confirmation when
deleting a directory tree. (Note that /q works only if /s is
specified.)
I use Powershell
Remove-Item c:\scripts\* -recurse
It will remove the contents of the folder, not the folder itself.
RD stands for REMOVE Directory.
/S : Delete all files and subfolders
in addition to the folder itself.
Use this to remove an entire folder tree.
/Q : Quiet - do not display YN confirmation
Example :
RD /S /Q C:/folder_path/here
Use Notepad to create a text document and copy/paste this:
rmdir /s/q "%temp%"
mkdir "%temp%"
Select Save As and file name:
delete_temp.bat
Save as type: All files and click the Save button.
It works on any kind of account (administrator or a standard user). Just run it!
I use a temporary variable in this example, but you can use any other! PS: For Windows OS only!
None of the answers as posted on 2018-06-01, with the exception of the single command line posted by foxidrive, really deletes all files and all folders/directories in %PathToFolder%. That's the reason for posting one more answer with a very simple single command line to delete all files and subfolders of a folder as well as a batch file with a more complex solution explaining why all other answers as posted on 2018-06-01 using DEL and FOR with RD failed to clean up a folder completely.
The simple single command line solution which of course can be also used in a batch file:
pushd "%PathToFolder%" 2>nul && ( rd /Q /S "%PathToFolder%" 2>nul & popd )
This command line contains three commands executed one after the other.
The first command PUSHD pushes current directory path on stack and next makes %PathToFolder% the current directory for running command process.
This works also for UNC paths by default because of command extensions are enabled by default and in this case PUSHD creates a temporary drive letter that points to that specified network resource and then changes the current drive and directory, using the newly defined drive letter.
PUSHD outputs following error message to handle STDERR if the specified directory does not exist at all:
The system cannot find the path specified.
This error message is suppressed by redirecting it with 2>nul to device NUL.
The next command RD is executed only if changing current directory for current command process to specified directory was successful, i.e. the specified directory exists at all.
The command RD with the options /Q and /S removes a directory quietly with all subdirectories even if the specified directory contains files or folders with hidden attribute or with read-only attribute set. The system attribute does never prevent deletion of a file or folder.
Not deleted are:
Folders used as the current directory for any running process. The entire folder tree to such a folder cannot be deleted if a folder is used as the current directory for any running process.
Files currently opened by any running process with file access permissions set on file open to prevent deletion of the file while opened by the running application/process. Such an opened file prevents also the deletion of entire folder tree to the opened file.
Files/folders on which the current user has not the required (NTFS) permissions to delete the file/folder which prevents also the deletion of the folder tree to this file/folder.
The first reason for not deleting a folder is used by this command line to delete all files and subfolders of the specified folder, but not the folder itself. The folder is made temporarily the current directory for running command process which prevents the deletion of the folder itself. Of course this results in output of an error message by command RD:
The process cannot access the file because it is being used by another process.
File is the wrong term here as in reality the folder is being used by another process, the current command process which executed command RD. Well, in reality a folder is for the file system a special file with file attribute directory which explains this error message. But I don't want to go too deep into file system management.
This error message, like all other error messages, which could occur because of the three reasons written above, is suppressed by redirecting it with 2>nul from handle STDERR to device NUL.
The third command, POPD, is executed independently of the exit value of command RD.
POPD pops the directory path pushed by PUSHD from the stack and changes the current directory for running the command process to this directory, i.e. restores the initial current directory. POPD deletes the temporary drive letter created by PUSHD in case of a UNC folder path.
Note: POPD can silently fail to restore the initial current directory in case of the initial current directory was a subdirectory of the directory to clean which does not exist anymore. In this special case %PathToFolder% remains the current directory. So it is advisable to run the command line above not from a subdirectory of %PathToFolder%.
One more interesting fact:
I tried the command line also using a UNC path by sharing local directory C:\Temp with share name Temp and using UNC path \\%COMPUTERNAME%\Temp\CleanTest assigned to environment variable PathToFolder on Windows 7. If the current directory on running the command line is a subdirectory of a shared local folder accessed using UNC path, i.e. C:\Temp\CleanTest\Subfolder1, Subfolder1 is deleted by RD, and next POPD fails silently in making C:\Temp\CleanTest\Subfolder1 again the current directory resulting in Z:\CleanTest remaining as the current directory for the running command process. So in this very, very special case the temporary drive letter remains until the current directory is changed for example with cd /D %SystemRoot% to a local directory really existing. Unfortunately POPD does not exit with a value greater 0 if it fails to restore the initial current directory making it impossible to detect this very special error condition using just the exit code of POPD. However, it can be supposed that nobody ever runs into this very special error case as UNC paths are usually not used for accessing local files and folders.
For understanding the used commands even better, open a command prompt window, execute there the following commands, and read the help displayed for each command very carefully.
pushd /?
popd /?
rd /?
Single line with multiple commands using Windows batch file explains the operators && and & used here.
Next let us look on the batch file solution using the command DEL to delete files in %PathToFolder% and FOR and RD to delete the subfolders in %PathToFolder%.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Clean the folder for temporary files if environment variable
rem PathToFolder is not defined already outside this batch file.
if not defined PathToFolder set "PathToFolder=%TEMP%"
rem Remove all double quotes from folder path.
set "PathToFolder=%PathToFolder:"=%"
rem Did the folder path consist only of double quotes?
if not defined PathToFolder goto EndCleanFolder
rem Remove a backslash at end of folder path.
if "%PathToFolder:~-1%" == "\" set "PathToFolder=%PathToFolder:~0,-1%"
rem Did the folder path consist only of a backslash (with one or more double quotes)?
if not defined PathToFolder goto EndCleanFolder
rem Delete all files in specified folder including files with hidden
rem or read-only attribute set, except the files currently opened by
rem a running process which prevents deletion of the file while being
rem opened by the application, or on which the current user has not
rem the required permissions to delete the file.
del /A /F /Q "%PathToFolder%\*" >nul 2>nul
rem Delete all subfolders in specified folder including those with hidden
rem attribute set recursive with all files and subfolders, except folders
rem being the current directory of any running process which prevents the
rem deletion of the folder and all folders above, folders containing a file
rem opened by the application which prevents deletion of the file and the
rem entire folder structure to this file, or on which the current user has
rem not the required permissions to delete a folder or file in folder tree
rem to delete.
for /F "eol=| delims=" %%I in ('dir "%PathToFolder%\*" /AD /B 2^>nul') do rd /Q /S "%PathToFolder%\%%I" 2>nul
:EndCleanFolder
endlocal
The batch file first makes sure that environment variable PathToFolder is really defined with a folder path without double quotes and without a backslash at the end. The backslash at the end would not be a problem, but double quotes in a folder path could be problematic because of the value of PathToFolder is concatenated with other strings during batch file execution.
Important are the two lines:
del /A /F /Q "%PathToFolder%\*" >nul 2>nul
for /F "eol=| delims=" %%I in ('dir "%PathToFolder%\*" /AD /B 2^>nul') do rd /Q /S "%PathToFolder%\%%I" 2>nul
The command DEL is used to delete all files in the specified directory.
The option /A is necessary to process really all files including files with the hidden attribute which DEL would ignore without using option /A.
The option /F is necessary to force deletion of files with the read-only attribute set.
The option /Q is necessary to run a quiet deletion of multiple files without prompting the user if multiple files should be really deleted.
>nul is necessary to redirect the output of the file names written to handle STDOUT to device NUL of which can't be deleted because of a file is currently opened or user has no permission to delete the file.
2>nul is necessary to redirect the error message output for each file which can't be deleted from handle STDERR to device NUL.
The commands FOR and RD are used to remove all subdirectories in specified directory. But for /D is not used because of FOR is ignoring in this case subdirectories with the hidden attribute set. For that reason for /F is used to run the following command line in a separate command process started in the background with %ComSpec% /c:
dir "%PathToFolder%\*" /AD /B 2>nul
DIR outputs in bare format because of /B the directory entries with attribute D, i.e. the names of all subdirectories in specified directory independent on other attributes like the hidden attribute without a path. 2>nul is used to redirect the error message output by DIR on no directory found from handle STDERR to device NUL.
The redirection operator > must be escaped with the caret character, ^, on the FOR command line to be interpreted as a literal character when the Windows command interpreter processes this command line before executing the command FOR which executes the embedded dir command line in a separate command process started in the background.
FOR processes the captured output written to handle STDOUT of a started command process which are the names of the subdirectories without path and never enclosed in double quotes.
FOR with option /F ignores empty lines which don't occur here as DIR with option /B does not output empty lines.
FOR would also ignore lines starting with a semicolon which is the default end of line character. A directory name can start with a semicolon. For that reason eol=| is used to define the vertical bar character as the end-of-line character which no directory or file can have in its name.
FOR would split up the line into substrings using space and horizontal tab as delimiters and would assign only the first space/tab delimited string to specified loop variable I. This splitting behavior is not wanted here because of a directory name can contain one or more spaces. Therefore delims= is used to define an empty list of delimiters to disable the line splitting behavior and get assigned to the loop variable, I, always the complete directory name.
Command FOR runs the command RD for each directory name without a path which is the reason why on the RD command line the folder path must be specified once again which is concatenated with the subfolder name.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
del /?
dir /?
echo /?
endlocal /?
for /?
goto /?
if /?
rd /?
rem /?
set /?
setlocal /?
To delete file:
del PATH_TO_FILE
To delete folder with all files in it:
rmdir /s /q PATH_TO_FOLDER
To delete all files from specific folder (not deleting folder itself) is a little bit complicated. del /s *.* cannot delete folders, but removes files from all subfolder. So two commands are needed:
del /q PATH_TO_FOLDER\*.*
for /d %i in (PATH_TO_FOLDER\*.*) do #rmdir /s /q "%i"
You can do it by using the following command to delete all contents and the parent folder itself:
RMDIR [/S] [/Q] [drive:]path
#ECHO OFF
rem next line removes all files in temp folder
DEL /A /F /Q /S "%temp%\*.*"
rem next line cleans up the folder's content
FOR /D %%p IN ("%temp%\*.*") DO RD "%%p" /S /Q
I tried several of these approaches, but none worked properly.
I found this two-step approach on the site Windows Command Line:
forfiles /P %pathtofolder% /M * /C "cmd /c if #isdir==FALSE del #file"
forfiles /P %pathtofolder% /M * /C "cmd /c if #isdir==TRUE rmdir /S /Q #file"
It worked exactly as I needed and as specified by the OP.
I had following solution that worked for me:
for /R /D %A in (*node_modules*) do rmdir "%A" /S /Q
It removes all node modules folder from current directory and its sub-folders.
This is similar to solutions posted above, but i am still posting this here, just in case someone finds it useful
Use:
del %pathtofolder%\*.* /s /f /q
This deletes all files and subfolders in %pathtofolder%, including read-only files, and does not prompt for confirmation.

Why does 'Run as administrator' change (sometimes) batch file's current directory?

I have a batch file that is in the same directory as the file I want to xcopy. But for some reason the file is not being found.
I thought that current directory was always where the batch file was located.
I run batch file as administrator. This occurs on a Windows 7 64-bit desktop computer.
Batch file:
#ECHO OFF
XCOPY /y "File1.txt" "File2.txt"
PAUSE
Error:
File not found - File1.txt
0 File(s) copied
Which directory is current working directory on starting a batch file with context menu item Run as administrator depends on User Account Control (UAC) setting for the current user.
This can be demonstrated with following small batch file C:\Temp\Test.bat:
#echo Current directory is: %CD%
#pause
With having selected in User Account Control Settings
Default - Notify me only when programs try to make changes to my computer
Don't notify me when I make changes to Windows settings
and using Run as administrator, Windows uses registry key
HKEY_CLASSES_ROOT\batfile\shell\runasuser\command
This registry key does not contain a default string for executing the batch file. Instead there is the string value DelegateExecute with the CLSID {ea72d00e-4960-42fa-ba92-7792a7944c1d}.
The result is opening a dialog window with title User Account Control and text:
Do you want to allow the following program to make changes to this computer?
Program name: Windows Command Processor
Verified publisher: Microsoft Windows
After confirmation by the user, Windows opens temporarily a new user session like when using on command line RunAs.
In this new user session the current working directory is %SystemRoot%\System32 on executing now the command defined in Windows registry with default string of key
HKEY_CLASSES_ROOT\batfile\shell\runas\command
which is:
%SystemRoot%\System32\cmd.exe /C "%1" %*
Therefore a console window is opened with title C:\Windows\System32\cmd.exe and the 2 lines:
Current directory is: C:\Windows\System32
Press any key to continue . . .
After hitting any key, batch execution finishes which results in closing cmd.exe which results in closing the user session.
But with having selected in User Account Control Settings
Never notify me when
Programs try to install software or make changes to my computer
I make changes to Windows settings
the behavior is different as the user has already elevated privileges.
Now Windows uses directly the command
%SystemRoot%\System32\cmd.exe /C "%1" %*
according to default string of key
HKEY_CLASSES_ROOT\batfile\shell\runas\command
in current user session.
The result is opening a console window also with title C:\Windows\System32\cmd.exe, but displayed in window is:
Current directory is: C:\Temp
Press any key to continue . . .
The current working directory of the parent process (Windows Explorer as desktop) is used for executing of the batch file because no switch to a different user session was necessary in this case.
PA has posted already 2 possible solutions in his answer which I replicate here with a small improvement (pushd with directory in double quotes) and with adding a third one.
Change current directory to directory of batch file using pushd and popd:
pushd "%~dp0"
%SystemRoot%\System32\xcopy.exe "File1.txt" "File2.txt" /Y
popd
This works also for UNC paths. Run in a command prompt window pushd /? for an explanation why this also works for UNC paths.
Use directory of batch file in source and destination specifications:
%SystemRoot%\System32\xcopy.exe "%~dp0File1.txt" "%~dp0File2.txt" /Y
Change working directory to directory of batch file using cd:
cd /D "%~dp0"
%SystemRoot%\System32\xcopy.exe "File1.txt" "File2.txt" /Y
This does not work for UNC paths because command interpreter cmd does not support a UNC path as current directory by default, see for example CMD does not support UNC paths as current directories for details.
The error message is very self explanatory. The file file1.txt is not found.
Because the file name does not include an absolute path, the system tries to find it on the current directory. Your current directory does not contain this file.
Your misconception is that the current directory is not the directory that contains the bat file. Those are two unrelated concepts.
You can easily check by adding this two commands in your bat file
echo BAT directory is %~dp0
echo Current directory is %CD%
you can notice they are different, and that there is a subtle difference in the way the last backslash is appended or not.
So, there are esentially two ways to cope with this problem
either change the current directory to match the expected one
pushd %~dp0
XCOPY /y "File1.txt" "File2.txt"
popd
or specify the full path in the command
XCOPY /y "%~dp0File1.txt" "%~dp0File2.txt"
For the sake of completeness and obscurity, I add another workaround, confirmed as working under Windows 8.1 and expected to work elsewhere, as it relies on documented functionality:
You can change the runas command definition keys
HKEY_CLASSES_ROOT\batfile\shell\runas\command and
HKEY_CLASSES_ROOT\cmdfile\shell\runas\command into
%SystemRoot%\System32\cmd.exe /S /C "(for %%G in (%1) do cd /D "%%~dpG") & "%1"" %*
Which results in the bat or cmd file starting in its containing directory when started using the runas verb, respectively the "Run as Administrator" menu entry.
What the additions to the original command exactly do:
cmd /S strips away first and last (double) quote in command string after /C
for %%G in (%1) do enumerates its single entry, the %1 argument,
making it available for expansion as %%G in the loop body; the letter is arbitrary but some may be "reserved"
%%~dpG expands to the drive and path of %%G, the ~ tilde stripping away quotes if present, which is why we add them back explicitly
cd /D changes both the drive and directory to its argument, and finally
& runs the second command "%1" %* regardless of success of the first one.
You can use pushd which will even support UNC paths, but a stray popd would land any script in the system32 directory, not a behavior I would be fond of.
It should be possible to do this for the exefile entry as well, but frankly, I'd rather live with the inconsistency than to attempt this on my system, as any error there could break a lot.
Enjoy defeating the security mechanics of your operating system :)

Resources