1

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
Roberto
  • 21
  • 6
  • You would need to provide sample files, exact directory structure, exact place within the directory structure which is the current directory when executing your command, and exact command-line used, in order for us to be able to reproduce the problem and find out what's wrong. – Mike Nakis Feb 29 '20 at 02:58
  • Does this make more sense? `For /F "Delims=" %%F In ('Dir /B/S/A:-DR *.nef 2^>NUL') Do Echo %%F` – Compo Feb 29 '20 at 11:05
  • @MikeNakis I tried this while the markReadonlyWith2stars.cmd script was placed either in C:\ or in G:\Users\username\Desktop while the processed folder was placed basically anywhere (on a SDHC card, on T:\, or within other folders). I usually use this script by dragging and dropping the folder onto the script file. But it makes no difference to calling it via cmd. – Roberto Mar 01 '20 at 16:41
  • @Compo Just tested that. It doesn't solve the problem, but it also works like my script and is shorter. – Roberto Mar 01 '20 at 16:55
  • @Roberto, I posted it as a comment, not an answer! What mine does, is ONLY process read only `.nef` files. Your's processes every `.nef` file, then tries to determine whether it's read only. Please also make sure that you've read the help information for [tag:robocopy], `robocopy /?`, especially the `/IA:R` `/XA:R` `/A+:R`, and `/A-:R` options. I cannot currently help you further, because you have not explained your specific issue, in a way that I understand. – Compo Mar 01 '20 at 17:59
  • the `!var!` notation has nothing to do with "non-English Windows", but with [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) – Stephan Mar 01 '20 at 18:33

1 Answers1

0

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.

Roberto
  • 21
  • 6