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
Related
I have a folder That has subfolders as below : I was able to loop and copy the folder and subfolders from test1 to test 3. But I want to extend the functionality by removing the each folder after copying
but am not able to instead is removing all the test 1 to test 3 folder.The test folder is just an example, the folders can be any name.
"C:\BACKUP_FDM_FILES\localbackup\test1"
"C:\BACKUP_FDM_FILES\localbackup\test2"
"C:\BACKUP_FDM_FILES\localbackup\test3"
pushd "C:\BACKUP_FDM_FILES"
for /f "tokens=*" %%G in ('dir /b /s /a:d ') do (
echo Found %%G
XCOPY %%G "C:\FDM\Upload" /e /s /y
if errorlevel 0 (
echo Copy succesffully copied reason Files were copied without error and you can delete the content folder
rd "%%G\*" /s /q
mkdir "%%G"
)
)
popD
pause
If you resort to XCOPY, why not use the "successor" ROBOCOPY without any loop, deleting and re-creating the folder? Like in
robocopy "C:\BACKUP_FDM_FILES\localbackup\" "C:\FDM\Upload\localbackup" /s /e /COPYALL /dcopy:T /move
This will even keep the timestamp on the moved folder, and all attributes on moved files. Added benefit: you can use all the other helpful options of this command at no extra cost.
"C:\BACKUP_FDM_FILES\localbackup\test1"
"C:\BACKUP_FDM_FILES\localbackup\test2"
"C:\BACKUP_FDM_FILES\localbackup\test3"
pushd "C:\BACKUP_FDM_FILES"
for /f "tokens=*" %%G in ('dir /b /s /a:d ') do (
echo Found %%G
XCOPY %%G "C:\FDM\Upload" /e /s /y
if errorlevel 0 (
echo Successfully copied now you can delete folder
popd
rd "%%G\*" /s /q
mkdir "%%G"
)
pause
You are using pushd to go to backup folder so you need to use popd to go back to parent folder.
SS64 - POPD
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 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.
What do I add to my DEL batch file if I want to exclude a folder inside a folder I want to delete?
I have this code to delete my all the contents of the temp folder
DEL /F /Q C:\temp
Now, I want to exclude a folder named imptfolder inside. This shouldn't be deleted whether it exists or not inside the temp folder. How do I do this?
Here's some batch script code that'll do what you're asking.
for /d %%I in (c:\temp\*) do (
if /i not "%%~nxI" equ "imptfolder" rmdir /q /s "%%~I"
)
del /q c:\temp\*
If you're just entering these commands from the console, use single percents rather than double.
cd /d c:\temp
for /d %I in (*) do #(if /i not "%~I" equ "imptfolder" rmdir /q /s "%~I")
del /q *
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