Get files names from folders in a loop RRS feed

  • Question

  • Hello everyone,

    As specified in topic i need only name of txt files that are in folder through which i iterate with a loop. Blow code returns only file path with file name:(

    Code is run from .bat file. 

    path = C:\Users\lmyton\Documents\MyFiles\Scripts\
    SET HotelCode=H6734 H3440 H5477
    (For %%h in (%HotelCode%) do (
    ECHO %path%%%h
    for %%f in (%path%%%h\*.txt) do echo %%f

    For some reason using below code doesn't do the job.

    for /r %%f in (%path%%%h\*.txt) do echo %%f

    I would appreciate help to get the solution.



    Friday, June 26, 2020 11:14 AM

All replies

  • Don't waste your time trying to do that in a .bat file. Powershell is so much easier. 

    $path = 'C:\Users\lmyton\Documents\MyFiles\Scripts\'
    $HotelCode ='H6734', 'H3440', 'H5477'
    Foreach ($h in $HotelCode) {
        "Txt files in $path$h"
        Get-childitem -path $path$h -filter *.txt | foreach {$}

    Friday, June 26, 2020 12:29 PM
  • As for now i need to stick with .bat. 

    For /R C:\Users\lpiech\Documents\Lukasz\Scripts\ %%F IN (*.txt) do (
    Set MyFile = %%~nxF
    ECHO %MyFile%

    How can i capture


    into variable? The way i do it in the code doesn't work:(



    Friday, June 26, 2020 6:16 PM

    @echo off
    Set MyFile=MyStart
    echo MyFile at start is %myfile%
    For /R C:\Users\lmyton\Documents\MyFiles\Scripts\ %%F IN (*.txt) do (call :Setter %%~nxF) 
    echo MyFile at end is %myfile%
    goto end 
    	echo Do something with %1
    	set MyFile=%MyFile%:%1
    	goto :eof 

    Friday, June 26, 2020 8:07 PM
  • As for now i need to stick with .bat.


    -- Bill Stewart [Bill_Stewart]

    Friday, June 26, 2020 9:55 PM