简体   繁体   中英

batch file to set attrib +r on all .docx files created or modified in the last days

Good afternoon,

I'm trying to utilise a batch script to modify the attributes of all .docx files in a given folder to +R when a field is changed within an access database.

getting the script to run is the easy bit.

I currently set attrib +R on *.docx - which is fine as there are only around 300 files in the folder, however this will grow very, very rapidly.

I've considered having new files created in a different directory, then use robocopy to move them on an event in access, but the event will have to be when creating the hyperlink to the document - the file cannot be moved at that stage.

Is there a way to efficiently set +R on directories with a large number of files, the majority of which will already be +R?

Either by checking modified / created date not greater than say 7 days, or checking the files that already have +R?

There are several ways to face it via forfiles , dir , robocopy , ... but all of them have the same problem: to determine what files needs to be processed, all of them need to be checked.

Using dir to search the needed files and attrib to change its attributes

pushd "x:\where\files\are" && (
    for /f "delims=" %%a in ('dir /b /a-r-d *.docx') attrib +r "%%a"
    popd
)

Using robocopy to do all the work

pushd "x:\where\files\are" && (
    robocopy . . /is /XA:R /A+:R /COPY:A
    popd
)

In both cases only files without the read only attribute will be processed. The robocopy command should be faster as only one executable is started while the for / dir solution needs to execute attrib for each found file, but robocopy is only directly included in the OS since Vista (for XP or 2003 it can be downloaded from Microsoft)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM