how to read filename from folder then output as variable?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

how to read filename from folder then output as variable?

#1 Post by goodywp » 13 Apr 2018 09:37

Hi all,

I know there is a lot of this topic, but my case I can not put two steps as one. Basically I tried to search txt files end with *_debug_remove.txt then

Code: Select all

@echo off
if exist foundfn.txt (del foundfn.txt)
for %%F in ("C:\Users\abcd\*_debug_remove.txt") do echo %%~nxF>>foundfn.txt 
I used the above code to get the TAS_debug_remove.txt
I also want to output TAS as a variable since the above TAS_debug_remove.txt can be any like AAAbbb_debug_remove.txt .....

Now I tried this code, but not working, why?

Code: Select all

@echo off
if exist foundfn.txt (del foundfn.txt)
for %%F in ("C:\Users\abcd\*_debug_remove.txt") do (
set var=%%~nxF
set var=%var:~0,-17%
echo %var%>>foundfn.txt 
 )
Last edited by goodywp on 13 Apr 2018 09:59, edited 2 times in total.

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

Re: how to read filename from folder then output as variable?

#2 Post by Squashman » 13 Apr 2018 09:46

Should I go back and show you all your questions that we mentioned Delayed Expansion to you.

But you do have a syntax problem with your substring in the SET command. I would bet we have shown you the proper syntax for that as well in plenty of your questions.

Code: Select all

set var=!var:~0,17!
From one of your very first questions on Dostips.
viewtopic.php?f=3&t=7999&p=53173#p53174

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

Re: how to read filename from folder then output as variable?

#3 Post by goodywp » 13 Apr 2018 10:05

Squashman wrote:
13 Apr 2018 09:46
Should I go back and show you all your questions that we mentioned Delayed Expansion to you.

But you do have a syntax problem with your substring in the SET command. I would bet we have shown you the proper syntax for that as well in plenty of your questions.

Code: Select all

set var=!var:~0,17!
From one of your very first questions on Dostips.
viewtopic.php?f=3&t=7999&p=53173#p53174
Tried this

Code: Select all

@echo off
setlocal enabledelayedexpansion
if exist foundfn.txt (del foundfn.txt)
for %%F in ("C:\Users\abcd\*_debug_remove.txt") do (
set var=%%~nxF
echo var=!var:~0,-17!>>foundfn.txt
 )
no output

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how to read filename from folder then output as variable?

#4 Post by aGerman » 13 Apr 2018 10:23

-17 :?: It was 17 in your initial post

Steffen

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

Re: how to read filename from folder then output as variable?

#5 Post by Squashman » 13 Apr 2018 10:27

1) Debug the script. Remove the redirection and actually see if you are getting any output from the FOR command.

2)Show us real world examples of your file names.

3) Based on the obfuscated file name example you have given, you could just split the file name at the first underscore.

Code: Select all

@echo off
if exist foundfn.txt (del foundfn.txt)
pushd C:\Users\abcd
(for /F "tokens=1 delims=_" %%F in ('dir /a-d /b *_debug_remove.txt') do echo %%F)>foundfn.txt
popd 

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

Re: how to read filename from folder then output as variable?

#6 Post by Squashman » 13 Apr 2018 10:40

aGerman wrote:
13 Apr 2018 10:23
-17 :?: It was 17 in your initial post

Steffen
If my memory serves me correctly, I believe it was

Code: Select all

set var=%var:0-17%
My brain probably interpreted that as wanting characters 0 to 17.

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

Re: how to read filename from folder then output as variable?

#7 Post by goodywp » 13 Apr 2018 14:14

Squashman wrote:
13 Apr 2018 10:27
1) Debug the script. Remove the redirection and actually see if you are getting any output from the FOR command.

2)Show us real world examples of your file names.

3) Based on the obfuscated file name example you have given, you could just split the file name at the first underscore.

Code: Select all

@echo off
if exist foundfn.txt (del foundfn.txt)
pushd C:\Users\abcd
(for /F "tokens=1 delims=_" %%F in ('dir /a-d /b *_debug_remove.txt') do echo %%F)>foundfn.txt
popd 
I will take this one. Thanks

Post Reply