Jack_111
Jack_111

Reputation: 891

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

Upvotes: 1

Views: 82

Answers (1)

Endoro
Endoro

Reputation: 37589

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

Upvotes: 2

Related Questions