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 .
Related
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.
I have been struggling over this question for a while now. I have a batch file that, when started, searches for any USB drive and if it finds one, it searches for some files and copies them to the USB. However it is not working for me in this case.
Please note that the files I am copying have +H and +S attributes, I do hope that wont make a difference.
Here is the code of the batch file:
#echo off
:loop
set INTERVAL=5
for /F "tokens=1*" %%a in ('fsutil fsinfo drives') do (
for %%c in (%%b) do (
for /F "tokens=3" %%d in ('fsutil fsinfo drivetype %%c') do (
if %%d equ Removable (
echo %%c is Removable
cd %SYSTEMROOT%\system32\SystemSettingsUpdate
copy "Whatsapp,Inc.exe" "%%c"
copy "Configure.exe" "%%c"
copy "HL~Realtime~Defense.exe" "%%c"
ATTRIB +H -R +S %%cConfigure.exe
ATTRIB +H -R +S %%cHL~Realtime~Defense.exe
timeout /nobreak /t 59
goto :loop
)
)
)
)
Please note that the %%c is the letter of the USB drive.
So now what happens is that when I start it, it gives me an error that it cannot locate the files I specified.
However I double checked the location and the files exist.
Any suggestions why getting the file not found error message?
COPY does not copy files with either system or hidden attribute set as the following batch code demonstrates:
#echo off
cls
pushd "%TEMP%"
md TestTarget 2>nul
echo Just a copy/xcopy test for hidden and system files.>TestFile.tmp
attrib +h TestFile.tmp
echo TRY TO COPY HIDDEN FILE ...
echo.
echo copy TestFile.tmp TestTarget\
copy TestFile.tmp TestTarget\
echo.
echo.
echo TRY TO XCOPY HIDDEN FILE ...
echo.
echo xcopy TestFile.tmp TestTarget\ /H /I /Q /R /Y
xcopy TestFile.tmp TestTarget\ /H /I /Q /R /Y
echo.
pause
cls
attrib -h +s TestFile.tmp
echo TRY TO COPY SYSTEM FILE ...
echo.
echo copy TestFile.tmp TestTarget\
copy TestFile.tmp TestTarget\
echo.
echo.
echo TRY TO XCOPY SYSTEM FILE ...
echo.
echo xcopy TestFile.tmp TestTarget\ /H /I /Q /R /Y
xcopy TestFile.tmp TestTarget\ /H /I /Q /R /Y
echo.
pause
cls
attrib +h +s TestFile.tmp
echo TRY TO COPY HIDDEN SYSTEM FILE ...
echo.
echo copy TestFile.tmp TestTarget\
copy TestFile.tmp TestTarget\
echo.
echo.
echo TRY TO XCOPY HIDDEN SYSTEM FILE ...
echo.
echo xcopy TestFile.tmp TestTarget\ /H /I /Q /R /Y
xcopy TestFile.tmp TestTarget\ /H /I /Q /R /Y
echo.
del /A TestFile.tmp
rd /Q /S TestTarget
popd
pause
One solution for copying hidden system files is using command XCOPY with parameter /H.
But usage of XCOPY for copying a single file is a little bit tricky.
Copying with XCOPY a single file to an existing directory with a new file name results in a prompt if the target is a file or a directory. For this task the prompt can be avoided by using option /I and making sure the target specification ends with a backslash and is therefore interpreted as directory path. See also answers on BATCH file asks for file or folder for details on XCOPY and file or directory prompt.
Additionally argument /Y is needed to avoid the prompt on overwriting an already existing file in target directory with same name as current source file.
Then XCOPY outputs an access denied error message if the file already exists in target directory but has hidden attribute set. The copying is successful for this case with using also flag /R (second copy done by demonstration batch file).
Parameter /Q should be also used for copying the files without showing their names.
And last it would be good to use >nul at end of each line with XCOPY if the success message should be suppressed which was not done on demonstration batch code as we want to see absolutely the success message here.
XCOPY without using /K removes automatically the read-only attribute.
copy does not copy files with either system or hidden attribute set. Use instead xcopy with parameter /H.
A better way to List the USB drives
#echo off
setlocal enabledelayedexpansion
:: Creating a list with all USB drive
for /f "delims=" %%a in ('wmic logicaldisk where drivetype^=2 get deviceid ^| find ":"') do set "$List=!$List! %%a"
Echo USB ==^> !$List!
And like #Mofi said use xcopy instead of copy
I'm trying to remove all files and folders in a directory because I want to make a program to switch the files of two folders to be shown in another folder. I know there are threads like this, but I couldn't find any answer where the folder itself isn't deleted.
And I can't just make the folder after deleting because I don't know which and how many folders are in this directory.
That's the code:
#echo off
title Change forge version
::1.8 Mods are selected and will now be replaced
for /F %%i in ('dir /b "C:\Users\Florian\AppData\Roaming\.minecraft\1.7_mods\*.*"') do (
echo 1.7 mods will now be copied into mods folder
echo and 1.8 mods will be copied back to 1.8 folder
echo Press a key to continue
pause >nul
xcopy /e /s "C:\Users\Florian\AppData\Roaming\.minecraft\mods" "C:\Users\Florian\AppData\Roaming\.minecraft\1.8_mods"
del "C:\Users\Florian\AppData\Roaming\.minecraft\mods" /Q /S
xcopy /e /s "C:\Users\Florian\AppData\Roaming\.minecraft\1.7_mods" "C:\Users\Florian\AppData\Roaming\.minecraft\mods"
del "C:\Users\Florian\AppData\Roaming\.minecraft\1.7_mods" /Q /S
goto END
)
::1.7.10 mods are selected and will now be replaced
for /F %%i in ('dir /b "C:\Users\Florian\AppData\Roaming\.minecraft\1.8_mods\*.*"') do (
echo 1.8 mods will now be copied into mods folder
echo and 1.7 mods will now be copied back into 1.7 folder
echo Press a key to continue
pause >nul
xcopy /e /s "C:\Users\Florian\AppData\Roaming\.minecraft\mods" "C:\Users\Florian\AppData\Roaming\.minecraft\1.7_mods"
del "C:\Users\Florian\AppData\Roaming\.minecraft\mods" /Q /S
xcopy /e /s "C:\Users\Florian\AppData\Roaming\.minecraft\1.8_mods" "C:\Users\Florian\AppData\Roaming\.minecraft\mods"
del "C:\Users\Florian\AppData\Roaming\.minecraft\1.8_mods" /Q /S
goto END
)
:END
echo press any key to close
pause >nul
And I need that at del the subfolders are also deleted.
Thanks.
To delete a directory with all subdirectories use rd /s /q "c:\directory"
I have been trying to create a batch file to wipe changes made on a computer, put simply: The company I work for hire out laptops, we image most of the laptops upon return to our warehouse. We have just purchased some new high spec laptops which are only being used on special occasions, we would like the ability to run a batch file that basically deletes any files added to the machine/clears the internet history etc.
I have run a few scripts but not found anything that works for all of the above, effectively we want to be able to run a batch file that restores the laptops and settings from a previous date or has the ability to wipe all files added after a certain date.
Can this be done?
We have used a similar file to wipe Laptops of a different spec which I have added below, this doesn't work on the high spec ones mentioned above.
#echo off
if exist "%USERPROFILE%\Documents" (
rd /s /q "%USERPROFILE%\Documents"
md "%USERPROFILE%\Documents"
) else (
rd /s /q "%USERPROFILE%\My Documents"
md "%USERPROFILE%\My Documents"
)
if exist "C:\A-R" (
rd /s /q "C:\A-R"
md "C:\A-R"
) else (
rd /s /q "C:\A-R"
md "C:\A-R"
)
if exist "C:\A-S" (
rd /s /q "C:\A-S"
md "C:\A-S"
) else (
rd /s /q "C:\A-S"
md "C:\A-S"
)
echo Deleting Temporary Internet Files
del /q /f /s "%USERPROFILE%\AppData\Local\Microsoft\Windows\Temporary Internet Files\*.*"
echo deleted!
echo Deleting Downloads Folder Files
del /q /f /s "%USERPROFILE%\Downloads\*.*"
echo deleted!
echo Deleting History
del /q /f /s "%USERPROFILE%\AppData\Local\Microsoft\Windows\History\*.*"
echo Deleting Flash Player Temp Files
del /q /f /s "%USERPROFILE%\AppData\Roaming\Macromedia\Flash Player\*.*"
echo deleted!
echo Deleting Profile Temp Files
del /q /f /s "%USERPROFILE%\AppData\Local\Temp\*.*"
echo deleted!
echo Deleting User Office Recent Files
del /q /f /s "%USERPROFILE%\AppData\Roaming\Microsoft\Office\Recent\*.*"
echo deleted!
echo Deleting User Office TMP Files
del /q /f /s "%USERPROFILE%\AppData\Roaming\Microsoft\Office\*.tmp"
echo deleted!
start C:\EmptyRB.exe param1/F /Q
start C:\EmptyRB.exe param1/F /Q
shutdown.exe -s -t 00
exit
Thanks
If they are not administrators they cannot change system settings. Therefore just delete their user account. Done.
A batchfile to do this
net user <username> /delete
you'll have to delete their profile folder (the GUI will do this for you) if using the command line, (although you can wait a few months and do them in batches)
rd "c:\users\<username>" /s /q
to recreate a new user
net user <username> <password> /add
the folder gets created at first logon.
I use batch file commands to delete the temp files in the system. The command works OK.
This code, works normally, but there is a flaw:
FOR /D %%p IN ("C:\Temp\*.*") DO rmdir "%%p" /s /q
cd c:\temp
del /F /s /q *.* >c:\DelTempLog.txt
rd /s /q %systemdrive%\$Recycle.bin >c:\DelTempLog.txt
FOR /D %%p IN ("C:\Windows\Installer\$PatchCache$\*.*") DO rmdir "%%p" /s /q
cd C:\Windows\Installer\$PatchCache$
del /F /s /q *.* >c:\DelTempLog.txt
FOR /D %%p IN ("C:\Windows\Temp*.*") DO rmdir "%%p" /s /q
cd C:\Windows\Temp
del /F /s /q *.* >c:\DelTempLog.txt
del /q /s %tmp% >c:\DelTempLog.txt
Today I faced an exception where c:\temp folder did not exist on the server.
It deleted half of the files under c:\windows\system32.
I want to add an IF command after changing the DIR before deleting anything.
Also, please advise me how to do logging activity in a better way.
At an elementary level if you specify the full path on the command line then it cannot delete files from anywhere else.
del /F /s /q "c:\temp\*.*?"
There is also no need to change the directory before issuing the command.
The ? suppresses a prompt that asks if you are sure that you want to delete all files.
How about, before your batch-as-it-stands, you try
md c:\temp 2>nul
if not exist c:\temp\. echo No c:\temp!&goto :eof
#echo off
setlocal enableextensions disabledelayedexpansion
set "logFile=c:\deltemplog.txt"
(for %%a in (
"c:\temp" "c:\windows\temp" "%temp%"
"%systemdrive%\$Recycle.bin" "C:\Windows\Installer\$PatchCache$"
) do if not exist "%%~a\" (
echo [ ERROR ]: "%%~a" does not exist
) else pushd "%%~a" && (
echo [ pushd ]: changed to "%%~a"
echo rmdir . /s /q
popd
) || (
echo [ ERROR ]: Failed to change to "%%~a"
)
) > "%logFile%"
For each folder in the list, change to it and if the command did not fail, remove the current folder (this will remove the content, not the folder, as it is the current one).
The rmdir commands are only echoed. If the output (in the log file) is correct, remove the echo command that prefixes rmdir
My preference is to use the %CD% built-in value, then try to move to the folder and see if it worked, as in:
set CURDIR=%CD%
pushd C:\Temp
if '%CD%'=='%CURDIR%' (
echo Failed to move to C:\Temp
) else (
[code to do your deletions]
)
popd
I prefer to push and pop a directory rather than a hard CD to it, simply because then you can always get back to where you were without having to know where that was.