cmd.exe strange behavior. Not recognizing desktop anymore

0

I was trying to add a tracert command to a .bat file.

At the cmd.exe I was writing:

tracert 111.111.111.111 > desktop\tracert.txt

and it ran ok. I've created a bat file like this:

::@echo off
Cd C:\
cls
echo %DATE%
echo %TIME%
set datetimef=%date:~-4%_%date:~3,2%_%date:~0,2%__%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %datetimef%
::2013_04_20__13_01_47
::tracert 111.111.111.111 > desktop\Tracert_IP1_%datetimef%.txt
tracert 111.111.111.111 > desktop\tracert.txt

The idea is to add the data to filename, I also would like to write it inside the tracert.txt, but I dont know how.

Code result:

C:\>echo 20/04/2013
20/04/2013

C:\>echo 14:18:12,44
14:18:12,44

C:\>set datetimef=2013_04_20__14_18_12

C:\>echo 2013_04_20__14_18_12
2013_04_20__14_18_12

**C:\>tracert 111.111.111.111  1>desktop\tracert.txt**
**The system cannot find the path specified**

Why is it adding "1" before the ">" and removing the space between ">" and "desktop"?

Anyway, I tried to run the tracert command again directly at the cmd but now it is giving me the same error message as the bat file: The system cannot find the path specified

Edit 1: Well, the problem with the path not find is because I've changed the cmd path using "cd" command, than the desktop folder was just not found! I'm embarrassed! Sorry guys.. Thanks for the replays about the "1"s and disapearing withe spaces.

Pedro77

Posted 2013-04-20T17:31:05.330

Reputation: 1 367

1To remove all ambiguity why not specify the full path, with quotes if required? – Karan – 2013-04-20T17:34:00.780

I'd probably use %USERPROFILE%\Desktop – unknownprotocol – 2013-04-20T19:35:11.133

You are right! Dumb problem.. sorry – Pedro77 – 2013-04-21T21:54:28.367

Answers

1

The place you know as desktop is a subfolder of your user profile that has the same name. If your batch file is stored on your desktop then simply remove the desktop\ part of the output. When you run cmd its base directory is your root user profile directory which has your desktop folder below that. If you create a batch file and run it then the base directory that cmd works in is that directory and not your user profile.

If you want your batch file to always output to your desktop irrespective of where it is stored then the cmd environment has a variable USERPROFILE that points to your profile directory (C:\Users\yourUserName) and can be combined with \desktop to output files to your desktop.

Where you have

tracert 111.111.111.111 > desktop\tracert.txt

Replace it with

tracert 111.111.111.111 > %USERPROFILE%\desktop\tracert.txt

Mokubai

Posted 2013-04-20T17:31:05.330

Reputation: 64 434

Yes, that's what I'd say. Alternatively, one can also create an environment variable for this user's desktop folder... so it can be accessed via for ex: %DESKTOP% – unknownprotocol – 2013-04-20T19:37:33.250

3

"Desktop" in this case is relative path. You should change to right folder ("cd" command) or put the whole path to output file in the .bat file.

pbies

Posted 2013-04-20T17:31:05.330

Reputation: 1 633

If the path contains spaces - you should specify the whole path surrounded by "". – pbies – 2013-04-20T20:29:23.480

1

1> is STDOUT, as opposed to STDERR, which is 2>

I think the space is just a syntactical issue, cmd.exe doesn't like the whitespace, so it changes it.

See Microsoft's Command redirection page for more info.

As to the not found error, checking permissions in that directory would be my first suggestion.

PS: Not a win user anymore, but is 'desktop' a valid way of accessing the desktop, or is it a directory name?

mcalex

Posted 2013-04-20T17:31:05.330

Reputation: 2 315

I realizy that I was very dumb! Lol... When you start cmd it starts by default at the Users\UserName folder! I changed the cmd path usind "cd".. LOL! sorry! – Pedro77 – 2013-04-21T21:53:41.847