1

I'm having a little troubles with my script in .bat. My task is to write a script that will check a file for a few things. I've already defined some of those things, but now I'm stuck. My problem is that I don't know how to define a condition that says: If the file is hidden or read-only, delete this attribute and write some info about the change to the file (some text).

And then I'm having a second problem and that is that the script has always to write something to the file, but when I try to write something to the file (while the script is running) and then I save it, there is always just the thing the script has to write in it. Would somebody please give me some advice? I'm a novice. Thanks a lot for all the responses.

here is the script itself:

@echo off
title file-checking script
set file="file.txt"
set maxbytesize=1
type NUL > file.txt
pause
:loop
if exist file.txt @echo ok> file.txt
if not exist file.txt type NUL > file.txt
FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA
if %size% LSS %maxbytesize% (
    echo.File is under %maxbytesize% byte
) ELSE (
    del file.txt
)
timeout/t 2 
goto loop

2 Answers 2

1

read HELP FOR and you will notice that checking the attributes is similar to checking the file size

 set ATTRS=%%~aA
Sign up to request clarification or add additional context in comments.

1 Comment

I think you meant help for.
1

Use >> instead of > to append data to an existing file, preserving existing content. The > redirection will overwrite any existing content.

You can use the following to test if a file is hidden (after you have proven that it exists):

dir /b /ah file.txt >nul 2>nul && (
  echo file is hidden
) || (
  echo file is not hidden
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.