Friday, February 20, 2015

windows - Batch program to move shortcut file


I'm writing a short batch program to move a few shortcuts.


The problem is that Windows returns, "Can not find the file specified".


I imagine this is because either it's considered a system file (think user account controls petty level "system") or that it's hidden even though you don't have to dir /a to display it in a directory listing.


What do I need to change here? The file names/paths are all correct as I've directly copied them from Windows Explorer.


cd C:\ProgramData\Microsoft\Windows\Start Menu\Programs\System
mkdir Settings
cd C:\ProgramData\Microsoft\Windows\Start Menu
dir
move "Default Programs.lnk" "C:\ProgramData\Microsoft\Windows\Start Menu\System\Settings"
pause

Answer



The problem lies in the following line of code:


move "Default Programs.lnk" "C:\ProgramData\Microsoft\Windows\Start Menu\System\Settings"

As pointed out by @Scott, the destination path is wrong. In particular:


Start Menu\System

That part should read:


Start Menu\Programs\System

Remarks


Even if you were able to solve the problem, there are other things worth mentioning.


Change the current folder


cd C:\ProgramData\Microsoft\Windows\Start Menu\Programs\System

The above commands wouldn't work as you probably expect in case you are on a drive which isn't C::


D:\>cd C:\ProgramData\Microsoft\Windows\Start Menu\Programs\System
D:\>mkdir Settings

In the example above, the Settings folder would be created in the root of the D: drive. To avoid this, use the /d parameter:


cd /d C:\ProgramData\Microsoft\Windows\Start Menu\Programs\System

The cd command doesn't treat spaces a delimiters when command extensions are enabled (by default, they are). It's a good idea to use quotes anyway:


cd /d "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\System"

The folder might not be stored on the C: drive at all, though. Rather then hard-coding the full path, you can use the %ProgramData% environment variable which was introduced with Windows Vista:


cd /d "%programdata%\Microsoft\Windows\Start Menu\Programs\System"

In earlier operating systems you would have used %AllUsersProfile% instead. Variable names are not case-sensitive.


Create folders


mkdir Settings

To save typing you can use md instead:


md Settings

Unlike the cd command, mkdir and md treat spaces as delimiters. Let's say you run this command:


md Some settings

In this case, two folders are created: Some and settings. If you intend to create a single folder called Some settings you need to use quotes. For consistency, I recommend always using quotes; even when they're not mandatory:


md "Settings"

You can also specify a full path, like this:


md "%programdata%\Microsoft\Windows\Start Menu\Programs\System\Settings"

The System and Settings subfolders don't usually exist, and both would be created in one go.


Further reading


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...