Link to home
Start Free TrialLog in
Avatar of Asatoma Sadgamaya
Asatoma SadgamayaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

MS Dos Command to password protect a file

Hi,

Please find the MS Dos command below. I am trying password protect a file. I am looking for a MS Dos command to do this (I need to include this command in a bat file)

@echo off
attrib +r -a -s -h "C:\Rep\MonthlyReports\abcd.xlsx"

Please let me know.

Thank you
A
Avatar of David Favor
David Favor
Flag of United States of America image

There no built in DOS command for file encryption.

You can password protect entire folders...

attrib local +h +s

Open in new window


Or use zip to encrypt a file...

zip -p pass123 file.zip your-file-path

Open in new window


Or openssl to encrypt a file...

openssl aes-256-cbc -d -in file -out file.encrypted

Open in new window

The first line is nonsense, that just hides the folder (making it a "system" hidden folder). No password involved.
But yes, there is nothing built-in you can use to password protect files or folders, you'll always need to use external tools - if you need a solution not bound to exactly one machine and user (NTFS encryption).

What's the primary purpose for protection?
Correct, to me the first approach is nonsense.

Only mentioned for completeness.

The best options will be zip for files which can actually be compressed for space savings + openssl for audio/video files which gain little or nothing by compression.
Avatar of Asatoma Sadgamaya

ASKER

Hi,

What I need is a Dos command which can make an excel workbook read only mode. If I copy this file and paste it in other folder, this file should exist in read only mode.
Also, I want a dos command to make the above excel workbook into Normal mode.
Thanks
A
ASKER CERTIFIED SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Likely best if you explain what you're trying to accomplish, as the more I read over your comments... some detail seems missing to understand what you're trying to accomplish.
Thank you for your code below to set a file read only. your code work for me because when I try to copy paste the file, still retains the read only mode. thats exactly I am looking for.

attrib +R filename

Open in new window

May I know the similar code to make the file back to normal mode please?
attrib -r filename
Avatar of Bill Prew
Bill Prew

Given that this an Excel file you want to write protect, you'd be much better off using the built in capability of Excel for that.  You can also assign a password as well if you wanted.  Details below:



»bp
Hi,
When i apply
attrib +R filename

Open in new window

on an excel file it converts it to read only mode. then I copy the file and paste manually, it retains the read only mode. But when I apply below code to migrate the excel file to another location, below code coverts the excel file into normal mode.ie the read only mode does not available. Please have a look. I like to see the read only mode on the file I exported using the code below.


@echo off
setlocal
echo Start Copy

set SOURCE_DIR=C:\Reports\MonthlyReports
set DEST_DIRS="Y:\","S:\ Reports\"
set FILENAMES_TO_COPY="Report_New.xlsx"

pushd "%SOURCE_DIR%"

for %%F in (%FILENAMES_TO_COPY%) do (

    for %%D in (%DEST_DIRS%) do (
        echo file "%%~F" to "%%~D"
        xcopy /Y "%%~F" "%%~D"
    )
)
popd
echo. Done.
exit /b

Open in new window


Thank you
If you use ROBOCOPY instead of XCOPY it will copy the attributes.

robocopy | Microsoft Docs


»bp
No version of DOS that I am aware of ships with any type of file encryption capability. Hence, there will be no "DOS" command line to do that.  You require a third party application.
Hi Bill, I have changed Xcopy to Robocopy on the above code. Nothing has happened.

Error: Invalid parameter after robocopy "/Y"

Thank you
A
You need to change the syntax to
robocopy .  "%%~D" "%%~F"

Open in new window

In fact, this should be sufficient
@echo off
setlocal
echo Start Copy

set SOURCE_DIR=C:\Reports\MonthlyReports
set DEST_DIRS="Y:\","S:\ Reports\"
set FILENAMES_TO_COPY="Report_New.xlsx"

for %%D in (%DEST_DIRS%) do robocopy %SOURCE_DIR%  "%%~D" %FILENAMES_TO_COPY /njs /njh

echo. Done.
exit /b

Open in new window

@Asatoma, likely best to close out this question, as there are several answers to your original question.

Then open another question about how to do file copy operations + preserve file attributes.
Thanks