Bat file - remove and copy - batch-file

I want to remove all directory content (subdirectories and files, but not the main directory).
After that I want to copy all content from other directory to that directory.
How could I do this?
This code doesn't work
cd C:\directory1
rmdir /s/q
pause
xcopy C:\directory2\directory22 C:\directory1 /s /e

rmdir needs a directory to delete, so this will work, but you will get an error message:
cd C:\directory1
rmdir . /s /q
pause
xcopy C:\directory2\directory22 C:\directory1 /s /e
But you can also do this:
cd C:\
rmdir C:\directory1 /s /q
md C:\directory1
pause
xcopy C:\directory2\directory22 C:\directory1 /s /e

Related

xcopy not copying directories. unsure why

#echo on
cls
set "root"="%CD%"
cd bin\cemu*
xcopy /q .\mlc01\* "%root%\temp\mlc01\" /e /i /y
xcopy /q .\hfiomlc01\* "%root%\temp\hfiomlc01\" /e /i /y
cd ..
rmdir /s /q cemu_1.7.3d
rmdir /s /q cemu_1.7.4d
cd ..
cd bin\cemu*
xcopy /q "%root%\temp\" .\mlc01\* /e /i /y
xcopy /q "%root%\temp\" .\hfiomlc01\* /e /i /y
cd "%root%"
pause
everything works fine with changing directories and everything but xcopy isn't copying directories or anything (I used this same command in my other project to and it works fine but here it wont)
I tried /s /t /e and all that stuff and I still cant get it going
Try changing:
set "root"="%CD%"
to:
set "root=%CD%"
Due to the quirks that exist in cmd.exe's quoting (cmd.exe has so many quirks) the first variation doesn't do what you want it it to do - it creates an environment variable named root" (yes, the environment variable name has a trailing double-quote character).

del /s /q "file location" isn't working

del /S /Q "C:\TEST\TESTFOLDER\"
isn't actually deleting TESTFOLDER.
Del command is used for deleting files. If you want to delete folder, you should use rmdir with /s /q switches to force delete the folder and all it's files without prompt
rmdir /S /Q "C:\TEST\TESTFOLDER\"

check the existence of the folder and delete the temp files

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.

If statement batch file

I am trying to do a batch file (I have windows 7) to do search for a folder and if it is existed so copy it under name folder backup and delete the folder and then again create new folder and make a copy of another folder to the new one. But the thing that every time it create the folder backup even though the folder that I searched for it at the beginning is not existed.
Any suggestions?
#echo off
dir /b C:\ >> "C.txt"
findstr /m "Folder" C.txt
del C.txt
if NOT %errorlevel%==0 (
goto :continue
) else (
mkdir C:\FolderBackUp
xcopy /s /e "C:\Folder" C:\FolderBackUp
rmdir /s /q "C:\Folder"
)
:continue
mkdir "C:\Folder"
xcopy /s /e "c:\Folder1\Folder2" C:\Folder
goto:eof
one suggestion:
#ECHO OFF &SETLOCAL
IF EXIST "C:\folder\" (
mkdir "C:\FolderBackUp"
xcopy /s /e "C:\folder\" "C:\FolderBackUp\"
rmdir /s /q "C:\Folder"
)
mkdir "C:\Folder"
xcopy /s /e "c:\Folder1\Folder2" "C:\Folder\"
goto:eof

Batch file to perform start, run, %TEMP% and delete all

looking for a way to perform the following:
Start > Run > "%TEMP% > Delete everything (skipping any conflicts).
so Far I have
#echo off
start %TEMP%
DEL *.*
I suppose I could use CD to get to the folder, the thing I'm wondering is if there are any instances where it cannot delete an a dialog box comes up, I want to skip these.
Thanks for the help!
Liam
del won't trigger any dialogs or message boxes. You have a few problems, though:
start will just open Explorer which would be useless. You need cd to change the working directory of your batch file (the /D is there so it also works when run from a different drive):
cd /D %temp%
You may want to delete directories as well:
for /d %%D in (*) do rd /s /q "%%D"
You need to skip the question for del and remove read-only files too:
del /f /q *
so you arrive at:
#echo off
cd /D %temp%
for /d %%D in (*) do rd /s /q "%%D"
del /f /q *
The following batch commands are used to delete all your temp, recent and prefetch files on your System.
Save the following code as "Clear.bat" on your local system
*********START CODE************
#ECHO OFF
del /s /f /q %userprofile%\Recent\*.*
del /s /f /q C:\Windows\Prefetch\*.*
del /s /f /q C:\Windows\Temp\*.*
del /s /f /q %USERPROFILE%\appdata\local\temp\*.*
/Below command to Show the folder after deleted files
Explorer %userprofile%\Recent
Explorer C:\Windows\Prefetch
Explorer C:\Windows\Temp
Explorer %USERPROFILE%\appdata\local\temp
*********END CODE************
If you want to remove all the files in the %TEMP% folder you could just do this:
del %TEMP%\*.* /f /s /q
That will remove everything, any file with any extension (*.*) and do the same for all sub-folders (/s), without prompting you for anything (/q), it will just do it, including read only files (/f).
Hope this helps.
#echo off
RD %TEMP%\. /S /Q
::pause
explorer %temp%
This batch can run from anywhere.
RD stands for Remove Directory but this can remove both folders and files which available to delete.
cd C:\Users\%username%\AppData\Local
rmdir /S /Q Temp
del C:\Windows\Prefetch*.* /Q
del C:\Windows\Temp*.* /Q
del C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Recent Items*.* /Q
pause
#echo off
del /s /f /q %windir%\temp\*.*
rd /s /q %windir%\temp
md %windir%\temp
del /s /f /q %windir%\Prefetch\*.*
rd /s /q %windir%\Prefetch
md %windir%\Prefetch
del /s /f /q %windir%\system32\dllcache\*.*
rd /s /q %windir%\system32\dllcache
md %windir%\system32\dllcache
del /s /f /q "%SysteDrive%\Temp"\*.*
rd /s /q "%SysteDrive%\Temp"
md "%SysteDrive%\Temp"
del /s /f /q %temp%\*.*
rd /s /q %temp%
md %temp%
del /s /f /q "%USERPROFILE%\Local Settings\History"\*.*
rd /s /q "%USERPROFILE%\Local Settings\History"
md "%USERPROFILE%\Local Settings\History"
del /s /f /q "%USERPROFILE%\Local Settings\Temporary Internet Files"\*.*
rd /s /q "%USERPROFILE%\Local Settings\Temporary Internet Files"
md "%USERPROFILE%\Local Settings\Temporary Internet Files"
del /s /f /q "%USERPROFILE%\Local Settings\Temp"\*.*
rd /s /q "%USERPROFILE%\Local Settings\Temp"
md "%USERPROFILE%\Local Settings\Temp"
del /s /f /q "%USERPROFILE%\Recent"\*.*
rd /s /q "%USERPROFILE%\Recent"
md "%USERPROFILE%\Recent"
del /s /f /q "%USERPROFILE%\Cookies"\*.*
rd /s /q "%USERPROFILE%\Cookies"
md "%USERPROFILE%\Cookies"
#echo off
del /s /f /q c:\windows\temp\*.*
rd /s /q c:\windows\temp
md c:\windows\temp
del /s /f /q C:\WINDOWS\Prefetch
del /s /f /q %temp%\*.*
rd /s /q %temp%
md %temp%
deltree /y c:\windows\tempor~1
deltree /y c:\windows\temp
deltree /y c:\windows\tmp
deltree /y c:\windows\ff*.tmp
deltree /y c:\windows\history
deltree /y c:\windows\cookies
deltree /y c:\windows\recent
deltree /y c:\windows\spool\printers
del c:\WIN386.SWP
cls
Just use
del /f /q C:\Users\%username%\AppData\Local\temp
And it will work.
Note: It will delete the whole folder however, Windows will remake it as it needs.

Resources