Eldar B.
Eldar B.

Reputation: 1327

Batch IF doesn't work

I made a simple BATCH file for notes, it just deletes a folder, makes a new one, and puts in a file called note.txt and then edits it... for some reason, it doesn't do ANYTHING at all! not even removing the folder, here's the code:

goto start
:abort
        ECHO Aborted!
        PAUSE
        GOTO start

:replace
        del /q "D:\Users\Eldar\Desktop\Note Folder"
        mkdir "Note Folder"
        @echo>"D:\Users\Eldar\Desktop\Note Folder\note.txt"
        @echo %note%> "D:\Users\Eldar\Desktop\Note Folder\note.txt"
        pause

:start
    @ECHO OFF
    color e
    cls
    echo Put the note in here:
    set /p LastNote=<"D:\Users\Eldar\Desktop\Note Folder\note.txt"
    SET /P note=
    cls
    echo Note has been saved!
    echo also, the last note you saved was: "%LastNote%".
    echo so are you sure you want to replace this note with a new one (Y/N)?
    SET /P agree=
    IF "%agree%"=="y" (
         ECHO goto replace
    ) ELSE (
        GOTO abort
    )

Upvotes: 0

Views: 492

Answers (2)

Compo
Compo

Reputation: 38579

There appears to be no reason to remove the Note Folder directory or even to delete the note.txt file.

How does it perform if you rewrite it like this?

@Echo Off
Color 0E

Set "LastNote="
If Exist "%UserProfile%\Desktop\Note Folder\note.txt" (
    Set/P LastNote=<"%UserProfile%\Desktop\Note Folder\note.txt"
)
If Not Defined LastNote GoTo replace

:ask
    ClS
    Echo=The last note you saved was:
    Echo=%LastNote%
    Echo=
    Choice /M "Would you like to replace this note with a new one"
    If "%ErrorLevel%"=="1" GoTo replace
    Echo=
    Echo=Aborted!
    Timeout -1
    GoTo ask

:replace
    ClS
    Set/P "note=Put the note in here: "
    If "%note%"=="" GoTo ask
    Echo=Note has been saved!
    If Not Exist "%UserProfile%\Desktop\Note Folder\" (
        MD "%UserProfile%\Desktop\Note Folder"
    )
    (Echo=%note%)>"%UserProfile%\Desktop\Note Folder\note.txt"
    Timeout -1
    GoTo ask

For the benefit of the OP, here is your version of your script with the major flaws removed and with unnecessary lines kept in.

@goto start
:abort
    ECHO Aborted!
    PAUSE
    GOTO start

:replace
    rmdir "D:\Users\Eldar\Desktop\Note Folder"
    mkdir "D:\Users\Eldar\Desktop\Note Folder"
    echo.>"D:\Users\Eldar\Desktop\Note Folder\note.txt"
    >"D:\Users\Eldar\Desktop\Note Folder\note.txt" echo %note%
    pause

:start
    @ECHO OFF
    color e
    cls
    echo Put the note in here:
    set /p LastNote=<"D:\Users\Eldar\Desktop\Note Folder\note.txt"
    SET /P note=
    cls
    echo Note has been saved!
    echo also, the last note you saved was: "%LastNote%".
    echo so are you sure you want to replace this note with a new one (Y/N)?
    SET /P agree=
    IF "%agree%"=="y" (
        goto replace
    ) ELSE (
        GOTO abort
    )

Upvotes: 0

Uladzimir Palekh
Uladzimir Palekh

Reputation: 1871

There are few errors in your script:

  1. del can delete only empty folders. Use rmdir /s /q folder_name to remove folder with files
  2. Remove ECHO in ECHO goto replace
  3. You are using full paths everywhere except mkdir. Is it correct?

My working version:

@echo off

goto start

:abort
    echo Aborted!
    pause
    goto start

:replace
    rmdir /s /q ".\TestF"
    mkdir "TestF"
    echo>".\TestF\note.txt"
    echo %note%> ".\TestF\note.txt"
    pause

:start
    color e
    cls
    echo Put the note in here:
    set /p LastNote=<".\TestF\note.txt"
    set /p note=
    cls
    echo Note has been saved!
    echo also, the last note you saved was: "%LastNote%".
    echo so are you sure you want to replace this note with a new one (Y/N)?
    set /p agree=
    if "%agree%"=="y" (
        goto replace
    ) else (
        goto abort
    )

Upvotes: 1

Related Questions