batch-filecmdfile-attributes

Why is my batch script hiding processed files/folders?


Can anyone here give me a hint, why my batch script marks successfully processed folders as system, hidden, non-archive? Furthermore I cannot even remove the "hidden" attribute via Explorer (probably because of the systemfolder attribute).

The script is meant to process one folder (passed to it as a parameter), looking for raw-photo files (.nef files in my case) that are marked read-only. For every read-only photo the script copies a specified file to the processed folder and renames that copy according to the photo filename.

The folder attribute mess is caused by robocopy. (Without that command there is no problem.) But it doesn't have to touch the folder at all. It only copies one file to that folder. The error only occurs, if at least one file in the folder was marked read-only and gets a sidecar file.

I already tried to move the script from system drive to desktop and start it from there. It made no difference.

(To avoid confusion: I am on a non-English Windows 10, so I used !var! instead of %var%. Hell it took some time to find that trick...)

echo off
setlocal ENABLEDELAYEDEXPANSION
chcp 65001

IF "%~1" == "" (
    GOTO myWarning
) ELSE (
    IF EXIST "%~1" (
        GOTO myFuction
    ) ELSE (
        GOTO myWarning
    )
)

GOTO myFuction
:myWarning
echo Ordner-Pfad muss angegeben werden!
pause
GOTO:eof

:myFuction
echo Bearbeite %1

cd "%1"

for /r %%f in (*.nef) do (
    set fileattr=%%~af
    set readonlyattr=!fileattr:~1,1!
    :: check if current file is read-only
    IF !readonlyattr!==r (
        :: create XMP-Sidecar file for read-only photos
        echo %%f
        robocopy "C:" "%1" "metadata-2stars.xmp"
        rename "metadata-2stars.xmp" "%%~nf.xmp"
    )
)

GOTO:eof

Solution

  • Sorry, after I narrowed the problem down to robocopy I found the solution. It seems to be a known bug in robocopy, e.g. described here:

    https://blog.coeo.com/how-to-prevent-robocopy-from-hiding-your-files-and-how-to-fix-it-when-it-does

    The solution/hotfix is simply to tell robocopy not to mark the destination as system hidden by adding /A-:SH at the end of the command. So with robocopy "C:" "%1" "metadata-2stars.xmp" /A-:SH everything works as expected.