Try to read folder name then output partial name of the folder as variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Try to read folder name then output partial name of the folder as variable

#1 Post by goodywp » 18 Apr 2018 09:03

Hi,
this time trying to output a folder name then output partial of its name into variable
Let's say I got the following path showing the folder

C:\auto_pkg_build\Sources\Source_schemes\QA_Infra\T501-08700-0100-T3_QA_Infra-SL_0102_signed_SL_Key_schemes_pack\T501-08700-0100\...\...

Under Source_schemes folder, there are many different subfolders similar as T501-08700-0100-T3_QA_Infra-SL_0102_signed_SL_Key_schemes_pack
I have a known value or variable as T501-08700-0100 as the above case.

What I need is to extract T501-08700-0100-T3_QA_Infra-SL_0102_signed_SL_Key_schemes_pack
then output T3_QA_Infra-SL_0102_signed_SL_Key_schemes_pack as variable

Thanks

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Try to read folder name then output partial name of the folder as variable

#2 Post by Squashman » 18 Apr 2018 11:19

Code: Select all

pushd C:\auto_pkg_build\Sources\Source_schemes\QA_Infra
for /D %%G in (*) DO (
    FOR /F "TOKENS=3* delims=-" %%H in ("%%~G") DO echo %%I
)
popd

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: Try to read folder name then output partial name of the folder as variable

#3 Post by goodywp » 18 Apr 2018 12:34

Squashman wrote:
18 Apr 2018 11:19

Code: Select all

pushd C:\auto_pkg_build\Sources\Source_schemes\QA_Infra
for /D %%G in (*) DO (
    FOR /F "TOKENS=3* delims=-" %%H in ("%%~G") DO echo %%I
)
popd
Thanks Squashman!

I tried your code with a little change and got the expected results

Code: Select all

@ECHO OFF
pushd C:\auto_pkg_build\Sources\Source_schemes\QA_Infra
if exist pkg_description.txt (del pkg_description.txt)
for /D %%G in (T501-08700-0100-*) DO (
    FOR /F "TOKENS=4* delims=-" %%H in ("%%~G") DO echo %%H
)>>pkg_description.txt
popd
The only thing is that the following string which I expected get should not contain -
QA_Infra-SL_0102_signed_SL_Key_schemes_pack
otherwise I only got QA_Infra
But If I changed this string without - like this QA_Infra_SL_0102_signed_SL_Key_schemes_pack

Then after running the above code, I will get QA_Infra_SL_0102_signed_SL_Key_schemes_pack
Thanks

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Try to read folder name then output partial name of the folder as variable

#4 Post by Squashman » 18 Apr 2018 14:08

What was wrong with using the FOR /F command as I provided it to you. Pretty sure it works the way you described your output in your first post in this thread.

Let's just test that from the cmd prompt real quick.

Code: Select all

I:\>FOR /F "TOKENS=3* delims=-" %H in ("T501-08700-0100-T3_QA_Infra-SL_0102_signed_SL_Key_schemes_pack ") DO @echo %I
T3_QA_Infra-SL_0102_signed_SL_Key_schemes_pack

I:\>

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: Try to read folder name then output partial name of the folder as variable

#5 Post by goodywp » 19 Apr 2018 07:22

Squashman wrote:
18 Apr 2018 14:08
What was wrong with using the FOR /F command as I provided it to you. Pretty sure it works the way you described your output in your first post in this thread.

Let's just test that from the cmd prompt real quick.

Code: Select all

I:\>FOR /F "TOKENS=3* delims=-" %H in ("T501-08700-0100-T3_QA_Infra-SL_0102_signed_SL_Key_schemes_pack ") DO @echo %I
T3_QA_Infra-SL_0102_signed_SL_Key_schemes_pack

I:\>
As I said in the first post, I only know T501-08700-0100 instead of T501-08700-0100-T3_QA_Infra-SL_0102_signed_SL_Key_schemes_pack :-)
from T501-08700-0100 to get T501-08700-0100-T3_QA_Infra-SL_0102_signed_SL_Key_schemes_pack then chop off to get
T3_QA_Infra-SL_0102_signed_SL_Key_schemes_pack as final results

Thanks

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Try to read folder name then output partial name of the folder as variable

#6 Post by Squashman » 19 Apr 2018 07:57

I don't know how to better explain that the changes you made to the FOR /F command that I gave you will not work. If you use the FOR /F command as I posted in my code it will work.
I am tapping out of this thread.

Post Reply