dimanche 29 mars 2020

Isolate an IF statement?

I would like to make sure that the "||" statement block is not applicable for this if statement :

if exist "%BackupDest%\%%~nI\%Desktop%\filename*.doc" (
ren "%BackupDest%\%%~nI\%Desktop%\filename*.doc" "newname*.doc"
)

Is there a way to isolate it from the rest of the code without having to interfere with the "||" statement block and without having to add a 2nd "||" statement block ?

I tried to add the if statement right after :

) || (
echo       Access refused ^(check permissions^)
)

But it executes the "||" statement block if any error occurs. Same thing if I remove the IF statement and leave the REN command only.

Here is the whole code :

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set BackupDest=D:\backup

for /f "tokens=*" %%I in ('dir /a:d-h /b "%HomeDrive%\users\*" ^| findstr /x /i /l /v /g:"%~dp0exclude_users.txt"') do if exist "%BackupDest%\%%I\" (

    echo -----------------------------------------
    echo      Processing user : %%I
    echo -----------------------------------------
    echo

    :: checking write permissions
    copy /y nul "%BackupDest%\%%I\.writable" >nul 2>&1 &&
    (
        del "%BackupDest%\%%I\.writable"

        xcopy "%%I\Desktop" "%BackupDest%\%%I\Desktop\" /e /i /y
        xcopy "%%I\Documents" "%BackupDest%\%%I\Documents\" /e /i /y

        :: renaming some files
        if exist "%BackupDest%\%%~nI\%Desktop%\filename*.doc" (
            ren "%BackupDest%\%%~nI\%Desktop%\filename*.doc" "newname*.doc"
        )

    ) || (
        echo       Access refused ^(check permissions^)
    )
)

pause
exit

Any help would be very helpful.

Thanks.

Aucun commentaire:

Enregistrer un commentaire