Wednesday, April 17, 2013

Windows 7 Won't Delete or Rename Files with Long Pathnames

I tried to delete some files today, but Windows 7 coughed up an error message:
"Sorry, I'm stupid. I don't know how to delete files with a long filename. Why don't you erase me and replace me with Linux?"
I may have applied my editorial powers on the error message above, but the gist of it was that the pathname was too long for Windows 7's NTFS file system. Windows 7 refused to permit any deletion of the files, no matter which variant of del or rmdir I tried. As a last resort, I tried renaming the files to conform to Windows 7's inexplicable rule. Windows 7 refused to let me rename them. I googled for help, because Windows 7 is no help. Based upon suggestions on a microsoft forum, for about half an hour I tried variations of the rm, rmdir, del, and even the cacls commands, to no avail.

This is one of the worst bugs I have ever encountered in Windows, and it was not present in Windows XP, because the files with the too-long pathnames came from a Windows XP computer! Windows has just gotten buggier, not better. The only thing Microsoft thinks about is how to enhance profits. The end user experience is the last thing on its mind.

But I've got a new feather in my cap since 2012. I'm a Linux veteran. I wouldn't call myself a guru other than in jest, but I know my way around a Linux system and can create and execute shell scripts, upgrade the kernel, and modify system parameters without too much difficulty, and I'm learning new things all the time. Let me say this to the Windows diehards: Linux is very useful, and you should learn it because it will help you manage your Windows system. Today is a case in point. I had a funny feeling that the Windows bug would not exist in Linux. Linux can read a Windows drive without any difficulty. Of course, Windows cannot read Linux drives, because of its severe limitations in intellectual capacity.

I insert a CD of Gparted, the Linux distro with a funny name, and boot my system from the CD, accepting all the defaults along the way. I love Gparted. Its mission in life is to repartition drives, and it can handle just about any type of drive. I have used it for its intended purpose many a time, but today I wish to use it for an unintended purpose. The command line is what I want. A Linux command prompt is a powerful thing, let me tell you, about a hundred times more powerful than a Windows command prompt.

By default, Gparted boots into a minimalist graphical environment and loads the flagship program, Gparted, which after a few moments reveals the pathnames of the attached drives. This is important information that I will use to compose the commands below. I wait until Gparted has gathered its information, which takes about a minute, and then click on a different program, the Terminal icon, which gives the command prompt.

Do not fear the Linux command prompt. It is your friend. It will let you do what you want to do, unlike Windows. What I want to do is kill the files that Windows won't kill because Windows is stupid. The first step is to mount the Windows drive into the Linux file hierarchy. I prepare for this by creating a directory that will be used to mount the NTFS drive:

sudo mkdir /mnt/windoze
Then I take the information observed from the Gparted window, which reveals the name of the drive, /dev/sdsomething-or-other, and compose a command resembling this:
sudo mount /dev/sda2 /mnt/windoze
Note that /dev/sda2 isn't necessarily going to be the case on your computer, and /mnt/windoze is simply a name I made up, although it is quite proper to mount drives in the /mnt directory.

What's left is to navigate to the directory with the bad file and kill it.
cd /mnt/windoze
Note that ls is the rough equivalent of dir in the Windows command line. I enter several more cd commands. Once I find the directory or the files that I want to delete, I use a very powerful command, rm, which I suggest examining prior to using. To get some help on a Linux command, one enters the command followed by --help. Try
rm --help
The command that finally gets rid of the bad filenames, which exist in a folder called "documentaries", is
sudo rm documentaries --recursive
And that's that! No more immortal files lingering on my Windows filesystem. I clicked on Exit, and Gparted let me reboot the system back into Windows, where I confirmed that the files were indeed gone.


Greg Zeng said...

Linux full-time here, but data, backup & archives are on NTFS-3G partitions. Linux creates 'bad' filenames that W7 cannor read. So in my Linux distros, I re-name these to being W7 friendly, if needed.

I avoid CLI as best I can. Current 2013) version allows file renames, but not (yet) recursively.

Neng Silvi said...

I have download Long Path Tool and it is the best solution of your problem.

igor said...

With Windows, one never knows whether the next download one finds on the Internet will contain a virus or malware. So downloading is a gamble with Windows. However, it is true that many people spend a lot of time downloading additional software in order to overcome all the bugs in Windows.

I prefer to do everything I can in Linux, where no downloading is necessary. Linux puts the user back in control, whereas Windows takes the control away. Windows sometimes knows what it is doing and sometimes doesn't. When it doesn't, there isn't much the user can do about it other than play Download Roulette.

garry dam said...

Another software alternative that works for this kind of problem is "LongPathTool" for error messages: Cannot read from source file or disk, there has been a sharing violation, cannot delete file or folder, the file name you specified is not valid or too long, the source or destination file may be in use and many other file managing errors.

igor said...

How much do they pay to insert plugs about their software on blogs like this one?

techlorebyigor is my personal journal for ideas & opinions