Moving hidden files/folders with the command-line or batch-file

15

4

Question

Does anyone know of a way to move files and folders that have the hidden, system, or read-only attribute set from the command-line or a batch file? (No, stripping the attributes first is not an option since there is no practical way to know which attributes were set in order to re-set them after the move.)


(Failed) Attempts

  1. Using the basic move command does not work with items with the hidden or system attribute set and for some reason, it does not have switches to specify attributes like the dir and del commands do.

  2. I tried using a utility I wrote that uses the shell’s file operation function, but that requires using start /w to prevent the batch file from running on ahead, and it complains about long-filename support for some reason.

  3. I tried using robocopy, but it first copies the files and then deletes the originals instead of simply moving the source (which results in a frustrating delay, even with the excessive output redirected to nul).

(Surprisingly it seems that few people have ever needed to move hidden files from the command-line. All I could find was this one person who abandoned the attempt.)

Synetech

Posted 2012-02-22T22:51:20.407

Reputation: 63 242

4If you are creating the \temp directory, why not just rename/move the %1 directory to \temp? Or is that just the simplified case? – Ken – 2012-02-22T23:27:49.830

That is indeed a simplified case. Simply renaming would not work. – Synetech – 2012-02-23T01:15:28.900

Why is stripping an attribute not an option? That would be the proper (and intended) way to do it. – Rook – 2012-02-23T01:39:28.580

> Why is stripping an attribute not an option? @Idigas, how exactly would you know which attributes were set on each and every item so that they can correctly be re-set? > That would be the proper (and intended) way to do it That doesn’t sound right, at all. – Synetech – 2012-02-23T01:47:19.377

Forgive my ignorance, but would something like http://www.jamesewelch.com/2008/05/01/how-to-write-a-dos-batch-file-to-loop-through-files/ work?

– Dr Kitty – 2012-02-25T22:07:15.660

@TheElectricMuffin, probably not. I already use for /d to move all of the directories in the source to the target. However, using for *may* work for files too. I’ll run some tests in a second to confirm, but I suspect that the for and move commands can both see through hidden and system attributes. If that’s the case, then you will have done it! I’ll be right back… – Synetech – 2012-02-25T22:12:11.220

@TheElectricMuffin bad news. It didn’t work. Neither for, nor move can see hidden files. Worse, neither has any attribute options like del or dir. :-( – Synetech – 2012-02-25T22:13:54.870

In fact, even using for /f "delim=s" %i in ('dir /a /b "..."') do move %i ... won’t work. The simplest case just won’t; for example, if c:\foobar.txt is hidden (or system…), then move c:\foobar.txt c:\blah won’t work because move cannot understand hidden files, even when they are explicitly named. – Synetech – 2012-05-20T01:37:01.573

Regarding what you say of robocopy. I wonder if MOVE also copies then deletes source. it is safer to do that. I suppose the way to test would be to repeatedly do DIR on both. The unix mv command apparently does copy and remove. If things are so slow, I suggest getting an SSD drive if you haven't already. i think it made a slow atom boot windows 7 faster than an intel core i5! moving/cut and pasting files, is v quick within the same file system because it just removes and creates a new link(according to mv) and i've heard similar things for windows hence can be so fast. – barlop – 2012-12-15T11:45:37.303

@barlop, an SSD isn’t really a fix because links require using NTFS instead of FAT32, and moreover, it doesn’t really improve things all that much when it comes to moving a couple of thousand tiny files instead of a few large ones. – Synetech – 2012-12-15T21:23:45.170

Answers

6

I've been able to successfully move read-only, hidden files using MV.EXE from the GNU for Win32 utilities. The target file retains the read-only, hidden attributes.

You'll have to download the entire package:

http://gnuwin32.sourceforge.net/downlinks/coreutils-bin-zip.php

RobW

Posted 2012-02-22T22:51:20.407

Reputation: 386

1Not bad. That works, unfortunately it dies when dealing with files that start with --; it thinks that it is an argument instead of a filename. – Synetech – 2012-03-23T01:00:27.667

2Not bad? Thats it? It solves your request. I don't see anything about the '--filename' requirement, or I would have tested for it. Apologies, but hidden requirements are poor sport. – RobW – 2012-03-23T16:00:31.357

Yes, not bad. It does only the basic operation I asked about. If it cannot handle valid Windows filenames, then it is not very useful now is it? Handling filenames correctly is not a “hidden” requirement. – Synetech – 2012-03-23T17:13:57.040

Well, that's an ethical stretch. "...move files and folders that have the hidden, system, or read-only attribute set...". Nothing basic about this question. You said "...surprisingly few...". If you're asking for time in exchange for 'solved' points, for a problem you know is not straightforward, holding back on requirements isn't very fair. I too don't mind rejecting a solution which does not fit the description, but at least I'll try to include the obscure gotcha's in advance. – RobW – 2012-03-23T23:08:24.560

What’s obscure about the tool being able to handle valid filenames? It’s not my fault the port has a bug. – Synetech – 2012-03-24T03:21:59.030

4@Synetech, You just have to qualify the file name: mv .\--1.txt newdirectory – dangph – 2012-12-15T14:09:34.203

@dangph, that’s a good idea; thanks for the tip. – Synetech – 2012-12-15T21:29:05.223

4Unix utilities in general use --foo to denote command-line arguments. In order to support file names starting with --, most of them accept a special argument -- to denote the end of the command-line arguments and the beginning of the file names. I'm not sure whether mv is one of these commands, but if it is, then you should be able to: mv -- --weirdFileName.gotcha betterFileName.txt – Ben – 2013-10-25T13:33:26.117

5

I just accomplished this using:

xcopy /H hidden_file destination_file
del /AH hidden_file

Ben

Posted 2012-02-22T22:51:20.407

Reputation: 2 050

1The question asks how to move files.  The OP knows how to copy and delete, and doesn’t want to do that.  Also, he says that he already knows how to delete hidden files. – Scott – 2013-06-25T15:23:46.943

2Scott is correct; if the source and destination volumes are the same, the last thing you want to do is to “move” a file by copying it first. At best, it is pointless with a single small file, but with a large file, or many small files (let alone many large files), it is horrible (high CPU and disk load). This is made all the worse if you are moving a whole directory tree. The file-system already supports a quick and easy move by changing directory entry pointers; the problem is with the OS interface which prevents doing so with hidden files. – Synetech – 2013-06-25T17:34:37.413

Fair enough, it isn't a real move. And for big moves it can give a lot of overhead. But the OP never actually said a copy+delete was necessarily a bad thing, and regardless, this is one of the top hits in web searches for how to move a hidden file from the command line, and it worked for what I had to do. I imagine people just looking for how to move a few hidden files might use it as well instead of installing GNU tools or PowerShell.

Very annoying that the "move" command simply can't do this. – Ben – 2013-06-26T19:36:32.747

1From the question: “ (Failed) Attempts          …          3. I tried using robocopy, but it first copies the files and then deletes the originals instead of simply moving the source (which results in a frustrating delay, …).” – Scott – 2013-07-03T00:02:21.040

Fair enough...I guess I read that as "I don't want to use robocopy" instead of "I don't want a copy+delete operation." But I see your point. – Ben – 2013-07-10T22:33:32.750

3

Powershell v2:

move-item -force   '--1.txt' newdirectory

also moves hidden files with hidden and system attributes set. Will work with files beginning with '--'.

RobW

Posted 2012-02-22T22:51:20.407

Reputation: 386

Even easier PowerShell solution: mv .oldfile .newfile – Jay Sullivan – 2014-12-03T21:12:18.637

1I’m sure that would work, but PowerShell is excruciatingly slow compared to the normal command-prompt. And no, that is not a “hidden” requirement, I clearly indicated my dislike of the delay of Robocopy in the question. (Please don’t tell me that it works fast on your system because that does not help me in any way. On my system it takes several seconds simply to load, and even just pressing Tab to complete a filename takes >1 second which is order of magnitude slower than in cmd. So unless you’re willing to buy me a new system, then PS is not going to help.) – Synetech – 2012-03-24T03:21:17.190

0

How about this?

C:\Temp>attrib hidden.txt
A   H        C:\temp\hidden.txt

C:\Temp>mklink /H renamed.txt hidden.txt
Hardlink created for renamed.txt <<===>> hidden.txt

C:\Temp>del /AH hidden.txt

C:\Temp>attrib renamed.txt
A   H        C:\temp\renamed.txt

Ben

Posted 2012-02-22T22:51:20.407

Reputation: 2 050

Obviously this only works within the constraints of where hardlinks are allowed. I think this means the destination of the file must be on the same partition of the same physical disk. But if this constraint is not met, I'm pretty sure a copy+delete would be required anyway, under the hood. – Ben – 2013-07-10T22:42:40.453

Interesting idea. Of course it is too limited (I only use NTFS for the Windows 7 system drive and FAT32 for everything else. Also, I usually use Windows XP.) It also seems to work inconsistently with different combinations of attributes. For example, if you set the read-only attribute, it won’t let you delete the “new” file the first time you try to delete it, instead it strips the attribute. ಠ_ఠ And of course, it doesn’t work for folders. – Synetech – 2013-07-11T19:58:31.477

0

We've had PowerShell and the mv command. For completeness, therefore, here's how to do it with JP Software's TCC/LE, whose move and ren commands both have a /a: option that is common to many of its commands that scan directories for files to operate upon:

[C:\Users\JdeBP]touch /c --wibble--
11/03/2014 20:29:18.868  C:\Users\JdeBP\--wibble--

[C:\Users\JdeBP]attrib +h +s .\--wibble--
___A___________ -> _HSA___________  C:\Users\JdeBP\--wibble--

[C:\Users\JdeBP]move /a:hs --wibble-- --wobble--
C:\Users\JdeBP\--wibble-- -> C:\Users\JdeBP\--wobble--
     1 file moved

[C:\Users\JdeBP]ren /a:hs --wobble-- --wurble--
C:\Users\JdeBP\--wobble-- -> C:\Users\JdeBP\--wurble--
     1 file renamed

C:\Users\JdeBP]dir /a:hs /t /m /k --wurble--
11/03/2014  20:29               0  _HSA___________  --wurble--

[C:\Users\JdeBP]

Further reading

  • JP Software. MOVE. Take Command / TCC Help.
  • JP Software. REN. Take Command / TCC Help.

JdeBP

Posted 2012-02-22T22:51:20.407

Reputation: 23 855

I for one am not about to buy a commercial software to do what I should be doable with a small, free program (and I may very well end up having to write it myself as usual). Regardless, you hard-coded the attributes which is no good because you cannot predict what attributes a folder may have. Also, by touching it, you lost its original timestamp. – Synetech – 2014-03-12T04:02:27.123