Closed Solved

CMD folder wildcard

: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?
13 answers Last reply Best Answer
More about folder wildcard
  1. See if copy C:\Users\*\AppData\LordEpicalsFiles\Info.txt C:\My_Files works
  2. I have already tried that, I think that the * only works for files such as *.txt
  3. 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:
    [cpp]
    @echo off
    for /f "usebackq" %%m in (`dir /b c:\users`) do (
    copy C:\Users\%%m\AppData\LordEpicalsFiles\Info.txt" "C:\My_Files\"
    )
    [/cpp]

    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:
    [cpp]
    copy C:\Users\%%m\AppData\LordEpicalsFiles\Info.txt" "C:\My_Files\%%m_Info.txt"
    [/cpp]


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

    --Russel
  4. 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.
  5. Anonymous 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:
    [cpp]
    @echo off
    for /f "usebackq" %%m in (`dir /b c:\users`) do (
    copy C:\Users\%%m\AppData\LordEpicalsFiles\Info.txt" "C:\My_Files"
    )
    [/cpp]

    Or

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

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

    --Russel
  6. 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.
  7. 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:

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

    Best regards,

    --Russel
  8. 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.
  9. 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"
  10. Best answer
    Anonymous 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:

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

    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:

    [cpp]
    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"
    [/cpp]

    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
  11. It worked, thanks.
  12. Best answer selected by LordEpical.
  13. This topic has been closed by Brett928S2
Ask a new question

Read More

Command Prompt Windows 7