Home > Software engineering >  Batch file throws "The system cannot find the path specified" on a valid path
Batch file throws "The system cannot find the path specified" on a valid path

Time:03-11

I am new to batch and while i made a script, the following error occurs: The system cannot find the path specified. Here is the code for the file:

title HelloBatch
cd C:/Users/"%USERNAME%"/Desktop
break > test.txt

I tried doing %USERNAME% without the quotatiion marks, NOPE, it still shows error.

Another note is that my username containe spaces. is that why it displays the error? if yes, how can i solve it?

CodePudding user response:

What happen when you run cd C:/Users/"%USERNAME%" in command prompt? If it change directory correctly, try cd Desktop .

Windows can sometimes remap the Desktop to somewhere else. For example, OneDrive can make the Desktop C:\Users\%USERNAME%\OneDrive\Desktop . You can check the actual path from Explorer by opening any directory on the Desktop.

enter image description here

CodePudding user response:

*After you read and went through Mofis comments - I would advise against the usage of C:\Users\%USERNAME%\Desktop and would rather advise to use %USERPROFILE%\Desktop as you not only save yourself time and thought, less is more, but also make it less static as through this it is relativ and not absolute: when using a specific username it couldnt work on another system. If for some reason your desktop is not in your user folder you can set your actual desktop path through mofis answer here. However it has to be noted that you will need registry access as you maybe don't have it at your workplace. That should be considered

CodePudding user response:

It should be "%USERNAME%" since when your username has spaces (example: "some username") then if you use %USERNAME% it will think that your username is "some" and not "some username", to fix that you need to add "%USERNAME%" with the quotation marks.

  • Related