3

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.

2
  • Do you know VBScript? Its string manip libs are easy to use Commented Jul 12, 2011 at 13:12
  • If you want to update your question, please add the new information, do not replace the entire post's contents completely. Commented Jul 13, 2011 at 13:26

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

9 Comments

I am currently using the following code but its output is nothing even there are multiple files in the mentioned dirstory. @echo off FOR /F "tokens=2 delims=_." %%i IN ('DIR /b D:\bpm\PDF\PDF*_.') DO ECHO %%i PAUSE
Are you testing with a batch file or just from the command line? If you're running it from the command line directly, use %i instead of %%i.
Your script is working for me on Windows 7 in a .cmd batch file, what OS are you trying on?
I am using windows 2003 Server
I am using a batch file and even I have used PAUSE in the end of the file but still window is vanishing from the screen
|
2
@ECHO OFF
SET "destpath=destinationPath"
FOR %%f IN (sourcePath\*.*) DO CALL :process "%%f"
GOTO :EOF

:process
SET srcfile=%1
SET "name=%~n1"
SET "name=%name:*_=%"
COPY %srcfile% "%destpath%\%name%"

The SET "name=%~n1" line extracts from the full path just the name without extension.

The SET "name=%name:*_=%" line deletes the _ character and everything before it in the value of name and stores the result back to name.

Ultimately, before executing the COPY command, name only contains that part of the original filename which is between _ and the extension.

2 Comments

I'm a newbie on this, could you please explain me The SET "destpath=destinationpath" right syntax? thanks a lot
@Lale: Hey there. I'm happy to help but I'm not sure what part of the line you have trouble with exactly. Could you elaborate please?
0

As they are of a fixed length you can use :~startpos,len syntax;

set fn=1000342578_30062011.PDF
set fn=%fn:~11,8%
echo %fn%

>30062011

In a for loop;

setlocal enabledelayedexpansion
for %%x in (*.*) do (
    set fn=%%x
    set fn=!fn:~11,8!
    call:handler
)
goto:eof

:handler
    echo !fn!
goto:eof

5 Comments

I know how to split the string but dont know how to take the string from a filename and then split it ??
This is working for me but to some extent, is there any method to set the value as global because I need the echo !fn! value after the for () paranthesis??
After the () its out of the loop so would just contain the last value, you can branch as above
It is also working for me...now let me proceed further... thanks to all :))))
ok, got it, let me try, sorry to bother again and again...I am new user :)

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.