How to hide files and folders using batch files

Table of Contents
  • What Does Batch Script Mean?
  • Techopedia Explains Batch Script

What Does Batch Script Mean?

A batch script is a text file that contains certain commands that are executed in sequence. It is used to simplify certain repetitive tasks or routines in the Windows, DOS and OS/2 operating systems, and is also used in complex network and system administration.

A batch script has a file extension of .bat, .cmd or .btm.

Techopedia Explains Batch Script

The commands in a batch file are executed by a special interface or shell. These commands may include “goto,” “for,” “call,” “echo,” “setlocal,” etc., and may make use of decision and loop constructs. A batch script can be created using any text editor such as Notepad or WordPad, and must be saved only in plain text format.

A batch file can be run easily by clicking on it or by typing its name in a command line interpreter. A batch script can also be run with arguments. The following are some of the commonly used commands in a batch script:

  • Echo — To display some text on the screen
  • Call — To run a batch script from within another script
  • Goto — To transfer control or execution sequence to a label or subroutine
  • If — To test a condition
  • Pause — To wait until a key is pressed
  • Rem — To include a comment line in the script
  • Setlocal — To start a local environment
  • Endlocal — To terminate a local environment
  • Shift — To parse the command line arguments in the script
  • Start — To run a script with a default application
  • Xcopy — To copy files and folders

Chris Hoffman is Editor-in-Chief of How-To Geek. He’s written about technology for over a decade and was a PCWorld columnist for two years. Chris has written for The New York Times, been interviewed as a technology expert on TV stations like Miami’s NBC 6, and had his work covered by news outlets like the BBC. Since 2011, Chris has written over 2,000 articles that have been read nearly one billion times—and that’s just here at How-To Geek. Read more.

How to hide files and folders using batch files

Do you know how to use the Command Prompt? If you do, you can write a batch file. In its simplest form, a batch file (or batch script) is a list of several commands that are executed when you double-click the file. Batch files go all the way back to DOS, but still work on modern versions of Windows.

PowerShell scripts and Bash scripts may be more powerful, but batch files can still be plenty useful if you need to run basic Windows commands.

Batch File Basics

A batch file is simply a text file saved with the .bat file extension. You can write one using Notepad or a more advanced text editor like Notepad++, but don’t use a word processor like Microsoft Word.

Let’s create a simple batch file. First, open Notepad. Type the following lines into it:

Next, save the file by clicking File > Save. Give it any name you like, but replace the default .txt file extension with the .bat extension.

For example, you might want to name it hello_world.bat .

How to hide files and folders using batch files

You now have a batch file with the .bat file extension. Double-click it to run it. This particular batch file sets ECHO off (which cleans up the output by hiding the commands from being printed at the prompt, prints the text “Hello World” to the screen, and then waits for you to press a key before it ends.

If you didn’t add PAUSE to the file, the batch file would simply run its commands and then automatically close. In this case, it would print “Hello World” to the window and then immediately close the Command Prompt window. When you want to quickly run commands without seeing the output, you can omit this. If you’re running several commands, you could place the PAUSE command in between them.

How to hide files and folders using batch files

Writing a More Complex Batch File

It’s fundamentally simple to create a batch file. The only thing you need to change is what you type into Notepad. To run several commands, you type each one on its own line and the batch file will run each one in order.

For example, let’s say we want to write a batch file that runs several network diagnostic commands. We might want to run ipconfig /all to view network information, ping google.com to see if Google’s servers are responding, and tracert google.com to run a traceroute to google.com and see if there are any problems on the way.

In the most basic form, we could simply place all those commands in a batch file, one after the other, like so:

When we run this file, we’d just see the output of each command right after the other. But this isn’t necessarily the ideal way to write a batch file.

How to hide files and folders using batch files

For example, you might want to add comment lines. Any line that begins with a :: is a comment line and won’t be executed. That makes them a useful way to explain what’s happening in the file for anyone you might give it to–or for your future self, who might forget why you put a certain command in there.

You might also want to add the “ECHO OFF” command to the beginning of the file. This is typically added to the start of most batch files. When you do this, the commands themselves won’t be printed to the Command Prompt, but the results will be. For example, you’ll see the network connection details but not the “ipconfig /all” line. Most people don’t care to see the commands, so this can clean up the output.

So here’s what that might look like:

How to hide files and folders using batch files

There are other directions you could go with a batch file like this. For example, you might want to have your batch script run the above commands and then dump the output to a text file you can view later. To do so, you’d use the >> operator after each command to append its output to the text file. As we’re going to read the output from the text file anyway, we can omit the PAUSE command.

After you run the above script, you’d find a file named results.txt in the same folder as the batch file with the output of the commands. The Command Prompt window will automatically close once the batch file is done running.

How to hide files and folders using batch files

The example we’re using above relies on actually printing information to the Command Prompt so the user can read it. However, many batch files are designed to be run non-interactively. For example, you could have a batch file that deletes multiple files or directories whenever you double-click it. You’d just need to use the del command to delete files or the deltree command to delete directories. Remember, you’re just using the same commands you’d run in a Command Prompt window.

Fundamentally, that’s the point of most batch files–just running a few commands one after another. However, batch files can actually be significantly more complex than this. For example, you can use “IF” statements along with the “GOTO” command to check the value of something and then skip to different lines depending on the result. This is more like writing an actual small program than a quick and dirty script. That’s one reason why .bat files are sometimes called “batch programs.” If you want to do something more complex, you’ll find plenty of guides to doing specific things with batch programming online. But now, you know the basics of how to throw a simple one together.

Introduction: A Batch File That Can Hide/unhide Files/folders

mode con: cols=%cols% lines=%lines%

echo 1. Hide Files/Folders.

echo 2. Unhide Files/Folders.

If Errorlevel 2 Goto unhide

If Errorlevel 1 Goto hide goto start

mode con: cols=%cols% lines=%lines%

echo Current directory (not hidden files):

echo Type the folder/file that you want to hide: set /p input= :

mode con: cols=%cols% lines=%lines%

attrib +s +h %CD%\”%input%” >nul && echo Successfully hidden [%input%]

ping localhost -n 4 >nul

mode con: cols=%cols% lines=%lines%

echo Current directory (hidden files):

echo Type the folder/file that you want to unhide: set /p input= :

mode con: cols=%cols% lines=%lines%

attrib -s -h %CD%\”%input%” >nul && echo Successfully unhidden [%input%]

ping localhost -n 4 >nul

Be the First to Share

Did you make this project? Share it with us!

Recommendations

How to hide files and folders using batch files

How to hide files and folders using batch files

How to hide files and folders using batch files

How to hide files and folders using batch files

DIY Summer Camp Contest

How to hide files and folders using batch files

Maps Challenge

How to hide files and folders using batch files

The Elements Speed Challenge

How to hide files and folders using batch files

Comments

How to hide files and folders using batch files

Clever program. You should think about entering this into the First Time Author’s contest.

ellisbodds

I’d like to create a batch file which allows me to hide or unhide a folder based on whether or not it is already hidden or not. Basically, if the folder is hidden, running the batch file would unhide it and if it was not hidden running the batch would hide it.

With some help from this thread I managed to get it working to show folders if they are hidden, but not vice versa. This is the script I am using:

If you could post the code I’d need to use to get both working in one batch file that would be superb, assume the folder is called “Locker” and is in the local directory.

TheOutcaste

Easier to use the If statement in this case:

Squashman

Retired Trusted Advisor

ellisbodds

Easier to use the If statement in this case:

It works great, but I forgot to mention that I want to make the file a system file as well :doh:

Any way you could include this?

In the context I’m going to be using this in, that will be enough protection.

EDIT: Got it working with this:

I didn’t work it out, someone on Yahoo! Answers got there before you

TheOutcaste

Squashman

Retired Trusted Advisor

ellisbodds

I know, that’s why I’m working on making it more secure

Now I’m going to convert this to an exe and save a copy of the batch script somewhere secure so I can edit it later if needs be.

Is there any way to make it so that when you enter the characters for the password they are replaced with a *, or not shown at all?

Squashman

Retired Trusted Advisor

TheOutcaste

Now I’m going to convert this to an exe and save a copy of the batch script somewhere secure so I can edit it later if needs be.

Is there any way to make it so that when you enter the characters for the password they are replaced with a *, or not shown at all?

You can’t hide the password in batch without a third party utility of some kind. You can do it in VBScript, which you can run from the batch file. It requires a couple other files, or you can create them on the fly.

Are you also denying access to the Folder Options Control Panel? Not much point in putting a password on this file when all anyone has to do is tell the system to show hidden and system files. You’d also want to block access to the Registry Editor, as it’s pretty easy to change those settings by merging a registy file.

ellisbodds

You can’t hide the password in batch without a third party utility of some kind. You can do it in VBScript, which you can run from the batch file. It requires a couple other files, or you can create them on the fly.

Are you also denying access to the Folder Options Control Panel? Not much point in putting a password on this file when all anyone has to do is tell the system to show hidden and system files. You’d also want to block access to the Registry Editor, as it’s pretty easy to change those settings by merging a registy file.

To be honest guys, this folder doesn’t need to be majorly secure, just out of view. When I saved the batch file elsewhere, I omitted the password. I just wanted to have the code for future reference

I was under the impression that you could encrypt stuff in Windows with a password so that you would need to enter it every time you wanted access. It seems this only protects it when accessing from another computer. Once I realised that, I decided to make a batch file instead.

TheOutcaste

If you are using XP you can put the files in a Zip folder and add a password.
How to create and use compressed (zipped) folders in Windows XP

If you are talking about Windows Encrypting File System, that allows access from your account without entering a password. Anyone logged in to a different account, or on a different computer will not be able to read the files without having the encryption key, but they will be able to see the file names (if they have Read access to the parent folder).

Third party encryption programs like TrueCrypt can be set to always require a password to mount the folder.

Batch files to list all files and directories in a folder

DOS has always been handy for creating the type of batch files that XP could never handle well so here are a few scripts you might find useful.

List all files in a directory and subdirectory

Simple and straightforward this one, a script to list all files in a directory and all corresponding subdirectories. Copy the code into Notepad or other text editor and save it as get_files.bat:

echo off
for /r %%a in (*) do echo %%a >> get_files.txt

The for command here is used to run a further command for each file or folder in the set. The addition of the /r switch is to tell the script to recurse all directories and subdirectories from where the script is run.

The variable name %%a is used to hold the current folder being traversed.

The in (*) bit is the set of files to match, which in this case is everything denoted by using the * wildcard character.

The do echo %%a script executes the echo%%a command that simply displays the current file being read in the current folder name which is represented by the variable.

The final code >> get_files.txt directs and appends all the output to a text file called get_files.txt which is necessary because the script is repeated several times as it recurses each folder and so we want to collate all the names into a single file.

List all folders and sub-folders in a directory and subdirectory

A slight variation on the first script, this second one lists all folders and sub-folders in a directory and subsequent subdirectories. Copy the code into Notepad and save it as get_dirs.bat:

echo off
for /d %%a in (*) do dir /ad /on /s /b “%%a” >> get_dirs.txt

Let’s go through the code. The for command here is used to run a further command for each file or folder in the set. The addition of the /d switch is to tell the script to match against directory names only.

As before, the variable name %%a is used to hold the current folder being traversed and in (*) is the set of files to match, which again is everything denoted by using the * wildcard character.

The do dir /ad /on /s /b “%%a” bit executes the dir command on the current folder (denoted by “%%a” ) with a set of switches to produce the required output.

The /ad switch is to ensure that only directories and subdirectories are read since the purpose of the script is to list all folders only; the /on switch is to sort the directories in order of name; the /s switch is to ensure the dir command recurses all subdirectories and the /b switch produces the output in an easy-to-read format with no summary or header information.

A further change to this script is that the current folder variable %%a is enclosed by speech marks . This is important because otherwise, any folder names that have spaces in them will not be read and a file not found message is returned.

The final code >> get_dirs.txt directs and appends all the output to a text file called get_dirs.txt which again is the important bit because the script is repeated several times for each folder and so we want to collate all the names into a single file.

List all files and folders in a directory and subdirectory to one level only

Here’s a specific variation on the above two scripts. Suppose I have a mass of directories and files archived from various backups and installations and need a list of all files and directories in a folder. Sounds easy enough but the catch is that I only want the script to traverse one level down in a directory since everything is archived by date.

So for example, my directory structure looks like this:

c:\backup
c:\backup\110201\apps
c:\backup\110201\docs
c:\backup\110201\utils
c:\backup\110201\utils\zips
c:\backup\110201\… (a bunch of other directories and subdirectories under 110201)
c:\backup\110407\apps
c:\backup\110407\docs
c:\backup\110407\utils\rars\
c:\backup\110407\utils\zips\
c:\backup\110407\web\test_1\
c:\backup\110407\web\test_2\
c:\backup\110407\… (a bunch of other directories and subdirectories under 110407)

My script needs to recursively traverse all folders in c:\backup and provide me with a list of all files and subdirectories therein without drilling down into each and every single sub-directory.

Here’s the code of the batch file which we’ll call get_list.bat:

echo off
for /d %%a in (*.*) do dir /on “%%a” >> get_list.txt

Running get_list.bat in the c:\backup folder will then result in something like the following:

c:\backup\110201\12_Feb_11_expense.xls
c:\backup\110201\apps
c:\backup\110201\docs
c:\backup\110201\paint.zip
c:\backup\110201\shopping.txt
c:\backup\110201\utils
c:\backup\110201\utilitybill.pdf
c:\backup\110201\website.zip
c:\backup\110201\zipped_docs
c:\backup\110201\12_Feb_11_diff.xls

c:\backup\110407\apps
c:\backup\110407\docs
c:\backup\110407\utils
c:\backup\110407\web

One last point to bear in mind with all of the above scripts is that running the batch file multiple times will append the results onto the same text file.

Now go forth and script eager young script cadets!

How to hide files and folders using batch files

Today, privacy is a hot topic. The technique of hiding files and folders comes in handy when you are using a computer which is used by more than one person. This article will show you a techie way to hide files and folders in Windows 7 using command line.

The following Tech-Recipes articles have been posted regarding how to show or hide files using the graphical interface in Windows OS:

In this post you will learn how to show or hide files and folders in Windows using the command line prompt.

We will use the attrib command to hide or show files or folders in Windows.

attrib: Using the attrib command in Windows, we can change file attributes (i.e., read-only, system and hidden flag).

attrib +h [file/folder name]: This will set hidden attributes to active. It will hide a file/folder.

attrib -h [file/folder name]: This will remove hidden attributes from a file to make files/folders visible to everyone.

How to Hide a File in Windows Using Command Prompt

1. Open your Command Prompt window using the Windows + R key combination, and then type cmd.
2. Navigate to the path where your file is located.
3. Type attrib +h file name to make it hidden.
4. To make it visible again, remove the hidden attribute using attrib -h filename.

How to hide files and folders using batch files

How to Hide a Folder in Windows Using Command Prompt

1. Open your Command Prompt window by using the Windows + R key combination, and then type cmd.
2. Navigate to the path where your folder is located.
3. Type attrib +h folder name to make your folder hidden from everyone.
4. To make it visible again, remove the hidden attribute using attrib -h folder_name.

How to hide files and folders using batch files

If you forget the list of files/folders and you have hidden them under a drive, you can use the graphical interface method to show/hide hidden files as mentioned in the articles listed above.

I have a folder in C:\Folder1

I want to copy all the contents of Folder1 to another location, D:\Folder2

How do I do this using a batch file?

How to hide files and folders using batch files

12 Answers 12

xcopy.exe is the solution here. It’s built into Windows.

If you have robocopy,

if you want remove the message that tells if the destination is a file or folder you just add a slash:

xcopy /s c:\Folder1 d:\Folder2\

How to hide files and folders using batch files

I see a lot of answers suggesting the use of xcopy. But this is unnecessary. As the question clearly mentions that the author wants THE CONTENT IN THE FOLDER not the folder itself to be copied in this case we can -:

Thats all xcopy can be used for if any subdirectory exists in C:\Folder1

How to hide files and folders using batch files

RoboCopy did not work for me, and there are some good solutions here, but none explained the XCopy switches and what they do. Also you need quotes in case your path has spaces in it.

xcopy /i /e “C:\temp\folder 1” “C:\temp\folder 2”

Here is the documentation from Microsoft:

How to hide files and folders using batch files

On my PC, xcopy and robocopy need also the path to them, i.e. C:\Windows\System32\xcopy.exe

That’s why I use simply “copy”: copy /y . \Folder1\File.txt . \Folder2\

This is how it is done! Simple, right?

Here’s a solution with robocopy which copies the content of Folder1 into Folder2 going trough all subdirectories and automatically overwriting the files with the same name:

/COPYALL copies all file information
/E copies subdirectories including empty directories
/IS includes the same files
/IT includes modified files with the same name

Note: it can be necessary to run the command as administrator, because of the argument /COPYALL . If you can’t: just get rid of it.

How to hide files and folders using batch files

FYI. if you use TortoiseSVN and you want to create a simple batch file to xcopy (or directory mirror) entire repositories into a “safe” location on a periodic basis, then this is the specific code that you might want to use. It copies over the hidden directories/files, maintains read-only attributes, and all subdirectories and best of all, doesn’t prompt for input. Just make sure that you assign folder1 (safe repo) and folder2 (usable repo) correctly.

And, that’s it folks!

Add to your scheduled tasks and never look back.

I have written a .bat file to copy and paste file to a temporary folder and make it zip and transfer into a smb mount point, Hope this would help,

How to hide files and folders using batch files

There are two methods of hiding files in MS-DOS or the Windows command line without installing any additional software on the computer.

Hide using the attrib command

The first method uses the attrib command as seen below.

This command hides your autoexec.bat file so a standard user browsing your hard drive would not be able to see the file. To make the file unhidden, use -h instead of the +h so the line would look like the example below.

Although the file is hidden, someone could still type edit c:\autoexec.bat and edit the file, or if someone typed attrib it lists all files and their attributes.

  • See our attrib command page for full information on this command and other examples.

Hide using ASCII characters

The other method uses ASCII characters when creating a directory or renaming it and prevents anyone using an early version of Windows and MS-DOS from accessing the directory. Below are the steps required for creating a directory with these characters.

  1. Type md (hold down Alt and type 987 while continuing to hold Alt , once typed in let go of Alt and a solid block appears).
  2. Press Enter to create the directory.

To get into this directory, type cd followed by a space, and hold Alt while typing 987 to get the block. When pressing Enter , you would then be able to get into the directory.

You can substitute 987, for the dec (decimal) number of any ASCII character.

Windows 3.x and Windows 95 cannot access these directories, and must be accessed through DOS. However, Windows 98 and later has the capability of opening these directories from within Windows. Therefore, if you are using this method for privacy or security, your procedure could be bypassed.

I can’t remember what characters I typed. How do I delete the directory now?

See the ASCII dictionary definition that lists a complete listing of ASCII characters or use the “?” wildcard where the ASCII character is.