3

Does anybody of a way to access files and directories that have a % in their name (which is valid) from the command-line? Specifically, if there are two %’s and the text between them happens to correspond to an environment variable.

For example, if there is a file called C:\blah\%temp%.txt or a folder called C:\Program Files\%temp%\, none of the following will work because the variable gets expanded:

> dir "c:\blah\%temp%.txt"
> dir "c:\blah\^%temp^%.txt"
> dir "c:\blah\%%temp%%.txt"
> dir "c:\blah\\%temp\%.txt"

> dir "c:\program files\%temp%"
> dir "c:\program files\^%temp^%"
> dir "c:\program files\%%temp%%"
> dir "c:\program files\\%temp\%"


Using wildcards will work, but does not uniquely select the file/folder and may include others:

> dir "c:\blah\?temp?.txt"        (also shows ztempz.temp, 1tempa.txt, etc.)
> dir "c:\program files\?temp?"   (likewise)



(This is frustrating because every now and then—usually when Explorer is restarted for whatever reason—the environment variables stop expanding and some places where they are used end up creating files or directories with the environment variable in it. For example, because I configured Chromium to store its cache in a subdirectory of %temp%, if the variable expands, it is fine, but when it doesn’t, Chromium creates a directory called %temp% under its own directory and stores the cache—which can get large—there. I want to add a line to my temp-/junk-file cleaning script to automatically delete that folder if it exists, but I cannot figure out how to access it from the command-line without resorting to wildcards.)

0

2 Answers 2

12

Something odd happened to me when I tested this.

dir "C:\Program Files\^%temp^%"

For some reason, it appears the quotes interfere with the escaping. If I escape the quotes too, it works.

dir ^"C:\Program Files\^%temp^%^"

It looks ugly, but it works.

If there are no spaces in the path, the quotes are not necessary at all.

3
  • I recommend using: dir c:\progra~1\^%temp^% Also, his question is "How can I escape the %'s in a file or folder" Apr 7, 2012 at 3:49
  • Both methods work. The question was partially why dir "c:\program files\^%temp^%" did not work, and this is the workaround.
    – Bob
    Apr 7, 2012 at 4:07
  • 1
    @Bob, that’s not a workaround, it’s an exact answer. I asked how to escape the %’s and you explained that it does indeed use the carets, but the surrounding quotes interfere so they need to be escaped as well. Problem solved.
    – Synetech
    Apr 7, 2012 at 6:20
0
del c:\a\file^%named^%bob

Proof:

C:\lolnope>edit bob

C:\lolnope>move bob bob^%was^%wub
        1 file(s) moved.


C:\lolnopen>dir "bob%was%wub"
 Volume in drive C is OS
 Volume Serial Number is CC34-08D9

 Directory of C:\lolnope

04/06/2012  08:18 PM                15 bob%was%wub
               1 File(s)             15 bytes



C:\lolnope>del bob^%was^%wub

C:\lolnope>dir bob^%was^%wub
 Volume in drive C is OS
 Volume Serial Number is CC34-08D9

 Directory of C:\lolnope

File Not Found

You can also surround the filename in double quotes. That appears to work with tab completion in CMD.EXE.

Note: I only tested on Windows Vista and Windows 7. This should work for XP as well though.

6
  • I created a %temp%; it had no 8.3 filename in both XP and 7.
    – Bob
    Apr 7, 2012 at 3:43
  • Did you try the other method I listed? If you use the percent symbol do not use quotes. If you use quotes, do not use the percent symbol. Note that you will need to use the 8.3 name if there is a space in the folder name as Bob said in his answer below. Your question is "How can I escape the % sign in file or folder names". Use the caret symbol in front of the % sign. That is the answer. Apr 7, 2012 at 3:49
  • Combining 8.3 names with escaped percentages will work in place of quotes, yes. Also, something like %temp temp% gets the 8.3 name %TEMPT~1. I was actually expecting the 8.3 name to replace percentages with underscores, as it does for truly invalid characters (e.g. \ ). But yes, building the whole path with 8.3 names and avoiding quotes entirely, while escaping appropriately works.
    – Bob
    Apr 7, 2012 at 4:04
  • @Jarrod, the problem with 8.3 names is that they cannot be reliably determined from the LFN, and therefore need to be manually determined. Worse, they can even change! Create two files: ren> "testing 1.txt" and ren> "testing 2.txt". Now do dir /x test*, followed by del "testing 1.txt" and ren> "testing 3.txt". Finally, ren> "testing 1.txt" and dir /x test*. Notice how testing 1.txt has changed from TESTIN~1.TXT to TESTIN~3.TXT. If you don’t manually check and update, you could end up deleting the wrong file; TESTIN~1.TXT is now testing 3.txt instead of testing 1.txt!
    – Synetech
    Apr 7, 2012 at 6:29
  • @Synetech thanks for the info. I say again though, the question was "How do I escape the % symbol in file names" I addressed that question. I will remove the 8.3 info from my original answer because it seems that people are hanging on to that part. Apr 7, 2012 at 16:45

Not the answer you're looking for? Browse other questions tagged .