saladass PEPE
saladass PEPE

Reputation: 11

CMD error filename/directory error

I used to program batch files for work but I quit since a long time, now I'm back on the job and there seemed to be a bit of a problem. I try to edit txt files using CMD commands on a batch file:

e.g. echo hello >> *.txt

the thing is I want to add the text to all the txt files in that directory and I remember the * represented all the files in that directory with the same extension unless it's used as *.* then it includes all the files, but now all it does is just writes this error on cmd:

The filename, directory name, or volume label syntax is incorrect.

Can anyone can give a little help?

Upvotes: 1

Views: 901

Answers (1)

Bitcoin Murderous Maniac
Bitcoin Murderous Maniac

Reputation: 1488

You can use a FOR /F loop with a DIR command to iterate the full paths and pass those over to the redirection append >> to echo to the text files accordingly.

Example FOR Loop

Be sure to change the value of the Folder= variable to be the directory you need to append to the files with the ECHO command.

Confirmed working batch script example

@ECHO ON
SET Folder=C:\MyFolder
CD /D "%Folder%"
FOR /F "TOKENS=*" %%A IN ('DIR /B /A-D "%Folder%\*.txt"') DO ECHO HELLO>>%%~fA
PAUSE
EXIT

Further Resources

  • DIR
  • FOR /F

    In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string
    

Upvotes: 1

Related Questions