1

I'm trying to scan files in directorys for text inside, but whenever I come across a file that has the ' - Copy' added to the end of it from windows, the program will not read it. I've tried using quotes within the name being passed, but no dice.

FOR /R %%F in (*.CDP) do (
    for /f "tokens=*" %%a in (%%~nxF) do (

I've been using this code and have no issues with the typical files Im seeing. however if its something like dummy_file - Copy, I will get an error from the program saying 'System cannot find the file dummy_file.' period included. If I use

FOR /R %%F in (*.CDP) do (
        for /f "tokens=*" %%a in ("%%~nxF") do (

Then the second for loop gets skipped, and the program proceeds on. I thought this would made the loop take it as a string literal, but apparently the for loop has its own way of reading things.

Is it possible to accept files in this loop that have a - Copy in them? Will I be able to use dummy_file - Copy.cdp here?

4
  • 1
    Try replacing "tokens=*" with "delims=". Commented May 11, 2015 at 15:32
  • rojo: Produces the same error Commented May 11, 2015 at 15:44
  • What's wrong with for /R %%F in (*.cdp) do ( something with "%%~nxF" )? The only benefit to your inner for /f would be if you intend to massage and tokenize the parts of each file name, or if you intended to process the contents of each file (in which case you should enclose "%%~nxF" in quotation marks and add the usebackq option). I'm wondering if some other part of your code you haven't pasted is where the real problem lies. Are you trying to set "variable=%%~nxF" and neglecting to enable delayed expansion when you go to retrieve !variable! perhaps? Commented May 11, 2015 at 16:14
  • Didn't know about the usebackq option before, it did the trick. I am processing the input within the other loop, which is why I have two. I do have set enabledelayedexpansion and no problems came from it. Again, thanks for letting me know usebackq is a thing. Commented May 11, 2015 at 17:26

1 Answer 1

1
for /R %%F in (*.CDP) do (
    for /F "usebackq delims=" %%A in ("%%~fF") do (

Changes:

  1. Instead of retrieving all tokens, this code disables the delimiters

  2. As the file name is quoted, usebackq is include to indicate that we are not processing a direct string, but the file contents

  3. As the code recurses folders, if the idea is to read the file contents then instead of directly using the file name and extension, the full path is needed as the file location can differ from current directory

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.