I was trying to delete C:\Users\%USERPROFILE%\AppData\Local\Temp
and it shows the error:
The filename, directory name, or volume label syntax is incorrect.
This is the code:
#Echo off
del /s /q "C:\Windows\Temp\*.*"
del /s /q "C:\Windows\Prefetch\*.*"
del /s /q "C:\Users\%USERPROFILE%\AppData\Local\Temp\*.*"
for /d %%p in ("C:\Windows\Prefetch\*.*") do rmdir "%%p" /s /q
for /d %%p in ("C:\Windows\Temp\*.*") do rmdir "%%p" /s /q
for /d %%p in ("C:\Users\%USERPROFILE%\AppData\Local\Temp\*.*") do rmdir "%%p" /s /q
ipconfig /flushdns
pause
This is the output on running the batch file:
The filename, directory name, or volume label syntax is incorrect.
Windows IP Configuration
Successfully flushed the DNS Resolver Cache.
Press any key to continue . . .
The solution is quite simple on using this code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
%SystemRoot%\System32\reg.exe QUERY "HKLM\System\CurrentControlSet\Control\Session Manager" /V PendingFileRenameOperations >nul 2>nul && goto NoDelInfo
pushd "%SystemRoot%\Temp" 2>nul && ( rd /Q /S "%SystemRoot%\Temp" 2>nul & popd )
pushd "%SystemRoot%\Prefetch" 2>nul && ( rd /Q /S "%SystemRoot%\Prefetch" 2>nul & popd )
pushd "%TEMP%" 2>nul && ( rd /Q /S "%TEMP%" 2>nul & popd )
%SystemRoot%\System32\ipconfig.exe /flushdns
goto EndBatch
:NoDelInfo
echo There are pending file rename operations.
echo/
echo Please first restart Windows and then run "%~nx0" once again.
echo/
:EndBatch
endlocal
pause
For a full explanation of the three command lines to clear the three folders see:
How to delete files/subfolders in a specific directory at the command prompt in Windows?
The directories for temporary files should never be cleared on pending file/folder rename operations currently registered done by Windows on next start to complete an installation or an uninstall using perhaps files currently stored in one of the two directories for temporary files.
See also the Wikipedia article about the predefined Windows Environment Variables displayed with their values on running set in a command prompt window.
In general it is better to run as administrator the Disk Cleanup tool of Windows as it can delete much more files no longer needed as this batch file.
As part of my job I need to remove files from computers that are being shipped over seas, this task is done several times a week and can be scripted, however I am terrible with script, I am in no means a coder of any sort.
My issue is that I need to clear out the C:\Users folder whilst keeping the Public and Default folders. I know how to remove a directory and all subfolders however I don't know how to keep just those two folders.
The code I have so far is:
#Echo off
color F
:Choice
Echo.
Echo.
set choice=
set /p choice="Type Y to proceed or N to close the tool and then press ENTER:----"
IF '%Choice%' =='Y' GOTO DELETE
IF '%Choice%' =='y' GOTO DELETE
IF '%Choice%' =='N' GOTO END
IF '%Choice%' =='n' GOTO END
:DELETE
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
Echo.
Echo.
#Echo All Files Removed
GOTO END
:END
echo.
echo.
Echo *****...Closing programme... Please wait...*****
Timeout /t 3
exit
Do you know if this is possible?
The following method, ran As Administrator, may work for you:
#Echo Off
Color 0F
Echo=&Echo=
Choice /N /M "Type Y to proceed or N to close the tool"
If ErrorLevel 2 Exit /B
For /F Tokens^=2^Delims^=^" %%A In ('WMIC Path Win32_UserProfile Where^
"Special!='True'" Assoc /AssocClass:Win32_UserAccount 2^>Nul'
) Do WMIC UserAccount Where "SID='%%A' And LocalAccount='TRUE'" Delete
Echo=&Echo=&Echo All Files Removed&Echo=&Echo=
Echo *****...Closing programme... Please wait...*****
Timeout 3 /NoBreak>Nul
Exit /B
Special!='True' will ignore special accounts, which include Administrator, Public and Default.
I want to open every file that ends with .jar in my Downloads folder. My code doesn't work. What am I doing wrong and how do I achieve my end goal?
Code:
del /s /q "C:\Users\%USERNAME%\AppData\Roaming\.minecraft\session.*"
cls
:choice
cls
set /P c=Open the file? [Y/N]
if /I "%c%" EQU "Y" goto :start
if /I "%c%" EQU "N" goto :choice
goto :choice
:start
start "" "C:\Users\Home Computer\Downloads\????.jar"
del /s /q "C:\Windows\Prefetch\JAVA*.pf"
del /s /q "C:\Users\Home Computer\Recent\*.bat.lnk"
del /s /q "C:\Users\Home Computer\Recent\????.jar.*"
del /s /q "C:\Users\Home Computer\Recent\Downloads.lnk"
pause
Edit: I should also mention that the name of the .jar changes every time I re download it. I can't target it specifically by file name. The file name is always four characters.
Take a look at these two screenshots.
https://gyazo.com/579ebd940bcd74f3a3deeb05db7b337d
https://gyazo.com/ceaffd97bf1f1e81860036810b35fcd7
If the name of the file has same number of characters everytime, then you can use ? for each character instead of *.
Say the filename is 3 characters long, then you can write the command as:
start "" "C:\Users\Home Computer\Downloads\???.jar"
Maybe you can take some references from the link given below:
https://www.howtogeek.com/111859/how-to-batch-rename-files-in-windows-4-ways-to-rename-multiple-files/
try
start "" "C:\Users\Home Computer\Downloads\*.jar"
or
java -jar "C:\Users\Home Computer\Downloads\*.jar"
You could use the Where command to identify the name of the downloaded file. (Of course it will only identify it if you don't have more than one four character .jar file there).
#Echo Off
Del/S/Q "%AppData%\.minecraft\session.*" 2>Nul
:Pick
ClS
Choice /M "Open the file"
If ErrorLevel 3 GoTo :EOF
If ErrorLevel 2 GoTo Pick
If ErrorLevel 1 GoTo Start
GoTo :EOF
:Start
For /F "Delims=" %%A In ('Where "%UserProfile%\Downloads":"????.jar"'
) Do (Set "FullName=%%~fA" & Set "Name=%%~nxA")
Start "" "%FullName%"
Del/Q "%SystemRoot%\Prefetch\JAVA*.pf" 2>Nul
Del/Q "%UserProfile%\Recent\%~nx0.lnk" 2>Nul
Del/Q "%UserProfile%\Recent\%Name%.lnk" 2>Nul
Del "%UserProfile%\Recent\Downloads.lnk" 2>Nul
Timeout -1
You could even shorten it a little by deleting more than one of the shortcuts from the same delete line.The locations you've used for recent are effectively only links to the real location in modern Windows versions so you could replace %UserProfile%\Recent with %AppData%\Microsoft\Windows\Recent.
I'm trying to make a program that will delete some files and perfrom rutine maintance on a computer by just clickin on one file. I'm testing it as I'm going along and realized that it's not deleting the folders. I want to delete everything within the folders but not the folders themselves. Here is my code so far:
#echo off
title SYSTEM Optimiation
echo Deleting Temp Folder
del /q /f "C:\Documents and Settings\%username%\Local Settings\TEMP"
echo.
echo DONE
echo.
echo Deleting Download folder
del /q /f "C:\Documents and Settings\%username%\My Documents\Downloads"
echo.
echo DONE
echo.
echo.
echo Hit any key to exit.
pause >nul
Try using wildcards and the /s switch on del:
del /q /s /f "%userprofile%\My Documents\Downloads\*"
but this will probably leave directories inside intact, but empty. Another option would be the quite explicit:
for /d /r "%userprofile%\My Documents\Downloads" %%x in (*) do rd /s /q "%%x"
for /r "%userprofile%\My Documents\Downloads" %%x in (*) do del /f "%%x"
Here much simpler than above. Current directory will be locked and therefore will not be deleted with others.
cd %Temp% && rmdir /s /q .
We have a batch script that removes files (del) and directories (rd). Does anyone know how to halt (fail) execution of the script if any of these delete statements fail? They could fail if a file/directory is locked by Windows. Thanks.
Update
Statements I'm using:
Del: del *.* /S /Q
RD: FOR /D %%G in (*) DO RD /s /q %%G
For deleting the files, you can first try to ren (rename) the file.
ren will set ERRORLEVEL to 1 if the file is locked. Add quotes around filename.
#echo OFF
:: Delete all files, but exit if a file is locked.
for %%F in (*.*) do (
#echo Deleting %%F
ren "%%F" tmp 2> nul
if ERRORLEVEL 1 (
#echo Cannot delete %%F, it is locked.
exit /b 1
)
del tmp
)
I suspect you may be able to do the same thing for directories, but I can't seem to figure out how to get a directory locked so I can test. The following may work:
:: Remove all directories, but exit if one is locked.
FOR /D %%G in (*) DO (
#echo Removing %%G
ren "%%G" tmpdir 2> nul
if ERRORLEVEL 1 (
#echo Cannot remove %%G, it is locked
exit /b 1
)
RD /s /q tmpdir
)
DEL doesn't return an errorlevel if the file is locked. I just did a test with excel file and I saw a zero (on Windows XP).
It might be better to use IF EXIST for the file to be deleted after you do the delete.
del file.txt
if exist file.txt ECHO "FAIL"
AFTER EDIT
Disclaimer: I have no idea how this performs...
You could do this for the files
DIR /B /S /A-d > c:\filestodelete.txt
del *.* /S /Q
FOR /F %%i in (c:\filestodelete.txt) DO (
IF EXIST %%i ECHO %%i STILL EXISTS
)
then for the directories
DIR /B /S /Ad > c:\directoriestodelete.txt
FOR /D %%G in (*) DO RD /s /q %%G
FOR /F %%i in (c:\directoriestodelete.txt) DO (
IF EXIST %%i ECHO %%i STILL EXISTS
)
EDIT: Right, so del does not set the ERRORLEVEL correctly. See ERRORLEVEL on DEL and Windows delete command can fail silently
PREVIOUS (incorrect) SOLUTION
You should check the errorlevel.
For example:
del file.txt
if errorlevel 1 goto FAIL
:PASS
echo Worked!
goto :END
:FAIL
echo Failed!
exit /B 1
:END
Here's my preferred way to check for errors while deleting files. We redirect the "error output" (standard file "2") to a file (e.g. delCmd.err). Then we use the FOR command as a way to get access to the ~z "file size" operater. If the size of the output file is not 0, then we know that "del" got an error... we display the error with the "type" command and exit the batch file with a non-zero error code:
del unwanted.txt 2> delCmd.err
FOR /F "usebackq" %%A IN ('delCmd.err') DO set size=%%~zA
if not "%size%"=="0" (
echo Error deleting unwanted.txt
type delCmd.err
exit /B 1
)
Here is a shorter version of the result monitoring variant. This is using 2>&1 trick to redirect stderr to stdout and for /f to check for any output.
#echo off
setlocal
set error=0
for /f %%i in ('del notepad2.exe 2^>^&1') do set error=1
echo %error%
Just for your information. The DEL command does return an error code if a serious error occurs, however it's behavior is way beyond our intuition that people would simply believe that the error code doesn't work at all.
This is what I've tested in DEL command in Windows 7:
Successful deletion of all files: 0 (of course)
Some files deleted, some files missing: 0 (intuition expects 1)
Deletion failure due to no permission or a read-only medium: 0 (intuition expects ≥ 1)
Non-existent drive or drive not ready (such as no CD on a CD-ROM drive): 1 (yes, you get it, but I will expect a higher error code)
Invalid path: 1 (I will expect a higher error code, too)
And, if you specify a list of files to DEL command, where at least one of the files fit the last two kinds of error mentioned above, then none of the files in the list will be deleted at all.
There is a script that will work like a charm.
It tests deletion failure by testing the existence of the supposedly deleted file/directory.
The output will be an existing c:\mydirectory with completely empty content.
Any error breaks the process and is reported.
The only downside is that directories are removed recursively, because there is no simple way to start deleting directories starting with the most "sub" directories.
#echo OFF
set path_to_clean=c:\mydirectory
#echo -Deleting Files
dir /A-D /S /B %path_to_clean%\* > fileslist.txt
for /F %%F in (fileslist.txt) do (
#echo Deleting %%F
del %%F 2> nul
if exist %%F (
#echo Cannot delete %%F, it is locked.
goto errorhandling
)
)
del fileslist.txt
#echo -Deleting Files done
#echo -Deleting Directories
dir /AD /B %path_to_clean%\* > directorieslist.txt
for /F %%D in (directorieslist.txt) do (
#echo Deleting %path_to_clean%\%%D
rmdir /S /Q %path_to_clean%\%%D 2> nul
if exist %path_to_clean%\%%D (
#echo Cannot delete %path_to_clean%\%%D. This folder or one of its sub-directories is locked.
goto errorhandling
)
)
del directorieslist.txt
#echo -Deleting Directories done
:errorhandling
rem some code here
These seem to work for me:
RD:
FOR /D %d IN (*) DO ( RD /S /Q "%d" & IF EXIST "%d" EXIT /B 1 )
DEL:
FOR %f IN (*.*) DO ( DEL /Q "%f" & IF EXIST "%f" EXIT /B 1 )
No idea about the performance though.
You can add flag /F to DEL for a "force delete" mode.
For rd and rmdir you can do
rd "some directory" || rem
this will set the errorlevel correctly - see
Hi This should work as well
#echo off
for /f %%i in ('del notepad2.exe 2^>^&1') do set ERRORLEVEL=1
IF %ERRORLEVEL%=1 then exit