BigRedEO
BigRedEO

Reputation: 837

"No such file or directory" - but it does exist - Windows 7

I am updating some old Windows 7 PCs (they will be upgraded to Windows 10 by the end of the year) via batch script (I only have access via SSH) and am running into a problem I have not had before - in fact, commands like this one have worked fine before, but suddenly not. This one stymies me.

I am in this directory (so I know it exists):

C:\HALS 2000\win7>ls -l
total 2908
-rwx------+ 1 Administrators None  880877 Jan 21 08:17 upgrade.8.54.zip
-rwx------+ 1 Administrators None  934641 Jan 21 08:17 upgrade.8.60.zip
-rwx------+ 1 Administrators None 1153914 Jan 16 12:27 upgrade.8.61.zip

And I use this command:

if exist c:\"HALS 2000"\win7\*.* rm c:\"HALS 2000"\win7\*.*

but I get

rm: cannot remove 'c:\\HALS 2000\\win7\\*.*': No such file or directory

WTF? I've used this command many batch updates before.

So I tested this command while remotely logged into one of the PCs:

if exist \"HALS 2000"\win7\*.* rm \"HALS 2000"\win7\*.* 

And I get:

rm: cannot remove '\\HALS 2000win7*.*': No such file or directory

What am I missing here?

Upvotes: 0

Views: 2141

Answers (1)

ottomeister
ottomeister

Reputation: 5808

This is happening because wildcarded filenames are handled differently on Unix and Windows.

In Unix, wildcarded arguments (arguments containing * and ?) are expanded by the shell (the command-line handler) and the results of the expansion are substituted for the wildcard in the list of arguments that are passed to the program. If you had run that rm command on a Unix system then the arguments that rm received would have been the list of names upgrade.8.54.zip, upgrade.8.60.zip and upgrade.8.61.zip. rm then takes each argument in turn and tries to delete it.

In Windows, wildcarded arguments are not expanded by the command-line handler. The wildcard is passed unmodified as an argument into the program, and it's up to the program itself to find the filenames that match the wildcard.

Because rm is originally a Unix program, it is written to expect that the shell will take care of wildcards. rm is not written to perform wildcard expansion internally. When it is invoked on Windows as rm c:\HALS 2000\win7\*.* and is passed the unexpanded argument c:\HALS 2000\win7\*.* it tries to delete a file whose name is exactly, literally, c:\HALS 2000\win7\*.*. No such file exists, so the delete attempt fails and rm reports an error.

del is a Windows command, so it was written with the expectation that it might be asked to handle the expansion of wildcard arguments. When it is given the argument c:\HALS 2000\win7\*.* it finds the files whose names match that wildcard and deletes them.

Upvotes: 1

Related Questions