0

I am trying to get the file names using a batch file from a folder but it just doesn't work.

I followed guidelines from here but for some reason this isn't returning anything at all when it should!

FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Desktop\UPD\*.txt') DO SET result=%%G

I also tried:

FOR /F "tokens=*" %%G IN (dir /b C:\Users\Desktop\UPD\*.txt') DO SET _result=%%~G

echo %_result% >> %~dp0Outputfile.txt

What I get is:

ECHO is on.

EDIT

Here is what I did so far now:

IF EXIST C:\Users\Nathanael\Desktop\UPD\*.txt (
    echo file found >> %~dp0Outputfile.txt
    chDIR C:\Users\Nathanael\Desktop\UPD\
    dir *.txt /b >> %~dp0Outputfile.txt

    FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Nathanael\Desktop\UPD\*.txt') DO SET result=%%G
    echo %result% >> %~dp0Outputfile.txt
)

The output is:

file found
NewVHD.txt
random.txt
ECHO is on.
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Possible duplicate of [Example of delayed expansion in batch file](https://stackoverflow.com/questions/10558316/example-of-delayed-expansion-in-batch-file) – aschipfl May 13 '18 at 09:36
  • The 'result' is already output as a metavariable, `%G`, so your post would not have an issue were you to use that metavariable within the loop instead of setting it first to a local variable and using that, `For /F %%G IN ("C:\Users\Nathanael\Desktop\UPD\*.txt") Do (Echo %%G)>>"%~dp0OutputFile"`. But you're not really just echoing to a file, are you? because you already know that, `Dir /B "C:\Users\Nathanael\Desktop\UPD\*.txt">"%~dp0OutputFile"` can do that. It would be much better were you to show us what you're intending to do with `%result%` so that solutions can be tailored accordingly. – Compo May 13 '18 at 10:20

2 Answers2

0
FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Desktop\UPD*.txt') DO SET result=%%G

Make sure that the path is correct (for example, perhaps it should be c:\Users\YourName\Desktop\UPD*.txt where YourName is the user name)?

jdigital
  • 11,926
  • 4
  • 34
  • 51
  • Thanks for the replied, I noticed all of that after posting :S. Did the changes and still the same results – Nathanael Vargas May 13 '18 at 03:02
  • I saw your fix to _result so I removed that part of my answer. Try running the `dir` command from the command line to make sure that it returns the expected value. – jdigital May 13 '18 at 03:03
  • IF EXIST C:\Users\Nathanael\Desktop\UPD\*.txt ( echo file found >> %~dp0Outputfile.txt chDIR C:\Users\Nathanael\Desktop\UPD\ dir *.txt /b >> %~dp0Outputfile.txt FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Nathanael\Desktop\UPD\*.txt') DO SET result=%%G echo %result% >> %~dp0Outputfile.txt ) The output: CheckVHD started nathan file found NewVHD.txt random.txt – Nathanael Vargas May 13 '18 at 03:04
0
  • If the for /f returns multiple files the last one will overwrite the previous in the set
  • The way cmd.exe parses (code blocks) requires delayed expansion when a var is set and used inside the same code block as is the case with your if exist

So either avoid the code block with reversed logic

IF NOT EXIST "C:\Users\Nathanael\Desktop\UPD\*.txt" Goto :Eof or other label

Or (always indent code blocks to better keep track) :

Setlocal EnableDelayedExpansion
IF EXIST "C:\Users\Nathanael\Desktop\UPD\*.txt" (
    echo file found >> %~dp0Outputfile.txt
    chDIR "C:\Users\Nathanael\Desktop\UPD\"
    dir "*.txt" /b >> %~dp0Outputfile.txt
    FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Nathanael\Desktop\UPD\*.txt') DO SET result=%%G
echo(!result! >> %~dp0Outputfile.txt
)
  • It's a good habit to always enclose pathes in double quotes
  • to avoid echo is off messages use an other command separator than a space if the var is possibly empty (I used a ( here