Sign in with
Sign up | Sign in
Your question
Closed

CMD folder wildcard

Last response: in Windows 7
Share
February 10, 2013 1:31:31 PM

:hello: 

I am making a .bat file and I want it to:
"copy C:/Users/(WILDCARD HERE)/AppData/LordEpicalsFiles/Info.txt" "C:/My_Files/"

The problem is where the (WILDCARD HERE) is, is there a way that I can make the .bat file work with any user?

More about : cmd folder wildcard

a b $ Windows 7
February 10, 2013 1:40:41 PM

See if copy C:\Users\*\AppData\LordEpicalsFiles\Info.txt C:\My_Files works
February 10, 2013 1:51:44 PM

I have already tried that, I think that the * only works for files such as *.txt
Related resources
February 10, 2013 1:52:51 PM

You can't do what you are trying to do by just using a wildcard. You'll have to use a for loop. Here is an example:
  1. @echo off
  2. for /f "usebackq" %%m in (`dir /b c:\users`) do (
  3. copy C:\Users\%%m\AppData\LordEpicalsFiles\Info.txt" "C:\My_Files\"
  4. )


Note however that if C:\users\<username>\appdata\LordEpicalsFiles\Info.txt exists in more than one user's folder, then the copy in C:\My_Files will get overwritten. If you want distinct copies for each user, change the copy line to this:
  1. copy C:\Users\%%m\AppData\LordEpicalsFiles\Info.txt" "C:\My_Files\%%m_Info.txt"



Use the above at your own risk. It is untested.

--Russel
February 10, 2013 2:06:35 PM

Rusabus;

It didn't work, here are the results:

@echo off
for /f "usebackq" %%m in ('dir /b c:/users') do (
%%m was unexpected at this time.
copy "C:/Users/%%m/Appdata/LordEpicalsFiles/Info.txt" "C:/My_Files/"
The filename, directory name, or volume label syntax is incorrect.
February 10, 2013 5:42:10 PM

LordEpical said:
Rusabus;

It didn't work, here are the results:

@echo off
for /f "usebackq" %%m in ('dir /b c:/users') do (
%%m was unexpected at this time.
copy "C:/Users/%%m/Appdata/LordEpicalsFiles/Info.txt" "C:/My_Files/"
The filename, directory name, or volume label syntax is incorrect.



A couple of things:

1. Did you copy and paste from my code sections into your bat file? Your reply has modified the code. For example, in my original code I've got single back quotes (`) but you've got single straight quotes ('). You have to use the single back quotes (hence hte "usebackq" option). In my code I've got back slashes (\) but you've got forward slashes (/).

2. Also, my original code did have a typo. Here is a revision:
  1. @echo off
  2. for /f "usebackq" %%m in (`dir /b c:\users`) do (
  3. copy C:\Users\%%m\AppData\LordEpicalsFiles\Info.txt" "C:\My_Files"
  4. )


Or

  1. @echo off
  2. for /f "usebackq" %%m in (`dir /b c:\users`) do (
  3. copy C:\Users\%%m\AppData\LordEpicalsFiles\Info.txt" "C:\My_Files\%%m_Info.txt"
  4. )


It is also worth noting that if the source file doesn't actually exist, you'll get the same error message you report.

--Russel
February 11, 2013 3:29:12 PM

I copied and pasted it last time, I just didn't copy and paste the cmd output.

Here is what it says this time:

C:\Users\LordEpical ADMIN>@echo off
for /f "usebackq" %%m in (`dir /b c:\users`) do (
%%m was unexpected at this time.
copy C:\Users\%%m\AppData\LordEpicalsFiles\Info.txt" "C:\My_Files\%%m_Info.t
xt"
C:\Users\%%m\AppData\LordEpicalsFiles\Info.txt C:\My_Files\%%m_Info.txt
The filename, directory name, or volume label syntax is incorrect.
0 file(s) copied.
)

Also I do have a source file, and I even made the output folder, same results.
February 11, 2013 11:13:07 PM

Hi,

It looks like you are copying and pasting the commands directly onto the command line rather than into your .bat file and then running your .bat file. The syntax of the for command changes depending on if it run from the command line or a .bat/.cmd file. If you want to run it from the command line directly, change the double percent sines to single percent sines. You might also want a single-line version of the command, like this:

  1. for /f "usebackq" %m in (`dir /b c:\users`) do copy C:\Users\%m\AppData\LordEpicalsFiles\Info.txt" "C:\My_Files"


Best regards,

--Russel

February 12, 2013 2:08:05 PM

It is starting to kind of work now, on the: C:\Users\%m\AppData\LordEpicalsFiles\Info.txt" I put a quotation mark before it so it is like: "C:\Users\%m\AppData\LordEpicalsFiles\Info.txt" ,
I had forgot that after Appdata there is Roaming, so it is now: "C:\Users\%m\AppData\Roaming\LordEpicalsFiles\Info.txt",
but now the only problem is it doesn't work on user names that have a space in them (so my user LordEpical ADMIN didn't work, all I can see is that it must have replaced LordEpical ADMIN with just LordEpical.)

Thanks for your help so far.
February 12, 2013 6:56:53 PM

Also this is the code so far: for /f "usebackq" %m in (`dir /b c:\users`) do copy "C:\Users\%m\AppData\Roaming\LordEpicalsFiles\Info.txt" "C:\My_Files/%m_Info.txt"

Best solution

February 12, 2013 7:54:59 PM
Share

LordEpical said:
It is starting to kind of work now, on the: C:\Users\%m\AppData\LordEpicalsFiles\Info.txt" I put a quotation mark before it so it is like: "C:\Users\%m\AppData\LordEpicalsFiles\Info.txt" ,
I had forgot that after Appdata there is Roaming, so it is now: "C:\Users\%m\AppData\Roaming\LordEpicalsFiles\Info.txt",
but now the only problem is it doesn't work on user names that have a space in them (so my user LordEpical ADMIN didn't work, all I can see is that it must have replaced LordEpical ADMIN with just LordEpical.)

Thanks for your help so far.


Good catch on the missing quote. We'll have to make a quick change to the for command to get it working with spaces. The problem is that the for command sees spaces as a delimiter, so LordEpical becomes %m and ADMIN becomes %n, but we can't write a command that uses %m if there is no space and %m%n if there is one, so instead we disable using any delimiters. Here is an update:

  1. for /f "usebackq delims=" %m in (`dir /b c:\users`) do copy "C:\Users\%m\AppData\Roaming\LordEpicalsFiles\Info.txt" "C:\My_Files"


You may also want to change this because as I have noted earlier, C:\My_Files\info.txt is going to get overwritten. Something like this may be more appropriate:

  1. for /f "usebackq delims=" %m in (`dir /b c:\users`) do copy "C:\Users\%m\AppData\Roaming\LordEpicalsFiles\Info.txt" "C:\My_Files\%m_Info.txt"


Also, as I noted earlier, you'll need double percent sines if you are going to use this in a .bat file or .cmd file.

Best regards

--Russel
February 13, 2013 10:36:10 AM

It worked, thanks.
February 13, 2013 10:36:25 AM

Best answer selected by LordEpical.
a b $ Windows 7
February 14, 2013 8:18:03 PM

This topic has been closed by Brett928S2
!