0

I am struggling to write a batch file to get a specific name from file name.

There are several xml files in a folder and all looks like the below

Plaintext1.xml
Plaintext2.xml
Plaintext3.xml
Encrypted_abcd_1_xml
Encrypted_samp_2.xml
Encrypted_xyz.xml
..

I want to get only files starts with Encrypted_*.xml and in the list of these files I want to get only word abcd, samp, xyz. i.e before symbol '' and after symbol ''.

Can you please provide some samples?

I have written upto this

@ECHO OFF
Z:
cd "C:\temp\"

for %%f in (Encrypted_*.xml) do (
Echo %%f
Echo x

)
:DONE

PAUSE:

1 Answer 1

2

This should do it:

 @echo off
 setlocal

 cd /d "C:\temp"
 for /f "tokens=2 delims=_." %%a in ('dir Encrypted_*.xml /b ') do echo %%a
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks It works but i am getting a value 'x' in the last value of the list.. the loop should stop once it reads all files but seems it goes one more loop even after the file count is over. Can you please help how to quit from the loop once it reads all file?
Do you still have echo x in the script like it shows above? if so, remove it. unless you have a file called Encrypted_x.xml, you shouldn't be getting an x.
There is no file called Encrypted_x.xml. When the for loop runs it run one more loop and as there is no file it echo only variable x alone. For Eg: if there are 5 files, then all 5 files showing correct value and there is 6th line showing x

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.