17

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.)

10
  • 4
    If you are creating the \temp directory, why not just rename/move the %1 directory to \temp? Or is that just the simplified case?
    – Ken
    Feb 22, 2012 at 23:27
  • That is indeed a simplified case. Simply renaming would not work.
    – Synetech
    Feb 23, 2012 at 1:15
  • Why is stripping an attribute not an option? That would be the proper (and intended) way to do it.
    – Rook
    Feb 23, 2012 at 1:39
  • > 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
    Feb 23, 2012 at 1:47
  • Forgive my ignorance, but would something like jamesewelch.com/2008/05/01/… work?
    – Dr Kitty
    Feb 25, 2012 at 22:07

5 Answers 5

7

I just accomplished this using:

xcopy /H hidden_file destination_file
del /AH hidden_file
5
  • 1
    The 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. Jun 25, 2013 at 15:23
  • 3
    Scott 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
    Jun 25, 2013 at 17:34
  • 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
    Jun 26, 2013 at 19:36
  • 1
    From 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, …).” Jul 3, 2013 at 0:02
  • 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
    Jul 10, 2013 at 22:33
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

7
  • 1
    Not 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
    Mar 23, 2012 at 1:00
  • 2
    Not 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
    Mar 23, 2012 at 16:00
  • 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
    Mar 23, 2012 at 17:13
  • 4
    @Synetech, You just have to qualify the file name: mv .\--1.txt newdirectory
    – dangph
    Dec 15, 2012 at 14:09
  • 4
    Unix 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
    Oct 25, 2013 at 13:33
4

Powershell v2:

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

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

2
  • 1
    I’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
    Mar 24, 2012 at 3:21
  • Even easier PowerShell solution: mv .oldfile .newfile Dec 3, 2014 at 21:12
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
2
  • 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
    Jul 10, 2013 at 22:42
  • 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
    Jul 11, 2013 at 19:58
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.
1
  • 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
    Mar 12, 2014 at 4:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .