Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read File Name in Windows Batch Programming

I want to read the name of a file in windows batch programming. I am trying by using different methods but failed please help.

Scenario has been given below.

I have different files in a folder but the length for filename is same for all files. E.g.

1000342578_30062011.PDF
1000342329_30062011.PDF

And I just want to save the part after _ and before .PDF (e.g. 30062011) part in a variable.

Below are just tries but I am not able to get through.

@echo off
echo.Date   : %date%
echo.Year   : %date:~10,4%
dir /s /b C:\Users\zeeshando\Desktop\*.txt
for %%x in (c:\temp\*.xls) do echo %%x 
lfnfor off 
for %%x in (*.*) do echo %%x > filelist.txt 
PAUSE
for %i in (C:\Users\zeeshando\Desktop) do 
    echo %~ni
PAUSE

UPDATE

My current code, based on @Alex K.'s answer:

@setlocal enabledelayedexpansion
for %%x in (C:\Hi\*.*) do (
    set fn=%%x
    set fn=!fn:~22,8!
    echo !fn!
    call:handler
)
goto:eof

:handler
echo !fn!
copy C:\Hi\*.* E:\!fn!
goto:eof


PAUSE

But it is not copying the files. Please check the above code.

like image 375
Mareena Avatar asked Mar 07 '26 14:03

Mareena


1 Answers

You could do this in your for loop using _ and . as delimiters, like this:

FOR /F "tokens=2 delims=_." %%i IN ('DIR /b C:\Users\zeeshando\Desktop\*_*.*') DO ECHO %%i
like image 71
Dave Avatar answered Mar 09 '26 16:03

Dave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!