-1

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?

hellopybits
  • 1
  • 1
  • 1
  • try with `cd "C:/Users/%USERNAME%/Desktop"` – npocmaka Mar 11 '22 at 02:00
  • Nope, Same error. – hellopybits Mar 11 '22 at 02:25
  • 4
    What about `CD /D "%UserProfile%\Desktop"`, `CD /D "%SystemDrive%\Users\%UserName%\Desktop"`, or, if you have to… `CD /D "C:\Users\%UserName%\Desktop"`. _Please note that Windows uses backward slashes for file path separators, so please use them instead of Unix/URI separators._ – Compo Mar 11 '22 at 02:51
  • 2
    I suspect that the batch file has been saved with unicode or some other format. Please make sure that it is saved in ANSI format using a text editor, not a word-processor. Are you cut-and-pasting the code, or have you retyped it into the question? If you use insert `dir "C:\Users\%USERNAME%\Desktop"`, do you get a directory listing? – Magoo Mar 11 '22 at 02:55
  • Does `C:\Users\%USERNAME%\Desktop` even exist, or have you put user directories on a different drive letter? – SomethingDark Mar 11 '22 at 03:10
  • 1. Do not double click on the batch file. Open first a [command prompt](https://www.howtogeek.com/235101/) and run the batch file from within the command prompt window by typing its fully qualified file name enclosed in `"` if that is necessary. Use the file name completion feature of `cmd.exe` with the key TAB as explained by help of the Windows Command Processor output on running `cmd /?` in the command prompt window. 2. [Debug your batch file](https://stackoverflow.com/a/42448601/3074564) to find out which command line really results in the display of this error message. – Mofi Mar 11 '22 at 07:17
  • 3. Read the Microsoft documentation about [Naming Files, Paths, and Namespaces](https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file) and use in future ``\`` as directory separator and not `/` as used on Linux/Mac. That is important as the automatic correction by Windows I/O functions do not always result in the correct behavior. 4. Run `C:\Windows\System32\reg.exe query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"` and look on the output registry values, especially the value with name `Desktop`. Is `%USERPROFILE%\Desktop` the value as by default? – Mofi Mar 11 '22 at 07:23
  • 5. Run `C:\Windows\System32\reg.exe query "HKCU\Software\Microsoft\Command Processor"`. Is there a registry value with name `AutoRun` output which by default does not exist and therefore should not be in the list of output registry values? Yes, then look on the value to get knowledge which software package corrupted your Windows command processor environment with the `AutoRun` registry value and run next `C:\Windows\System32\reg.exe delete "HKCU\Software\Microsoft\Command Processor" /v AutoRun /f` to delete the `AutoRun` registry value which does not exist by default. – Mofi Mar 11 '22 at 07:26

2 Answers2

0

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

Fat P
  • 341
  • 2
  • 10
0

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.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • 1
    Certainly not the case in a `cd` command. – Magoo Mar 11 '22 at 12:33
  • @sweepingattack1 Please open a command prompt window, run `cd /?` and read the little usage help with `CHDIR command does not treat spaces as delimiters, ...`. So spaces in an unquoted folder path are no problems on using the command __CD__. But a file/folder name without/with path is better always enclosed in `"` even on using command __CD__ because of the folder path could contain `&` which is interpreted as unconditional AND operator by `cmd.exe` before executing command __CD__ or as conditional AND operator on folder path containing `&&`. But your answer is definitely not correct. – Mofi Mar 11 '22 at 18:24