jeudi 16 avril 2020

Why are those conditions statement problematic?

I'm making a batch script that checks whether the current user has the right to write to a local folder by creating a file inside it. IF NOT, it displays an error message. IF so, it deletes the file and creates a subfolder.

@echo off

set folder=C:\test

rem directory check
copy /y nul "%folder%\.writable" >nul 2>&1 || (echo %username% can't access %folder%) && (

del "%folder%\.writable" 2>nul
md "%folder%\test2"
)

pause
exit

The problem is that the script still performs the deletion of the .writable file and the creation of the "test2" folder even if it couldn't create the file.

By replacing >null 2>&1 with >null 2>null = same problem

By reversing || and &&, if it can create the file, it displays the echo command, without going any further, otherwise if it can't create it, it doesn't display the echo command and nevertheless executes the lines below (del, md).

How to solve this while keeping the copy command ?

Thanks.

Aucun commentaire:

Enregistrer un commentaire