none
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. 

    ECHO OFF
    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
    ))
    PAUSE

    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.

    Regards



    _Lukas_

    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 {$_.name}
    }
    
    
    

    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%
    )
    PAUSE

    How can i capture

    %%~nxF

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

    Regards


    _Lukas_

    Friday, June 26, 2020 6:16 PM
  • https://ss64.com/nt/for.html

    @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 
    :Setter 
    	echo Do something with %1
    	set MyFile=%MyFile%:%1
    	goto :eof 
    :end 
    

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

    Why?


    -- Bill Stewart [Bill_Stewart]

    Friday, June 26, 2020 9:55 PM
    Moderator