Foldername first letter of filename

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
STEPHAN
Posts: 2
Joined: 16 Jan 2019 13:01

Foldername first letter of filename

#1 Post by STEPHAN » 16 Jan 2019 13:10

I have a large number of files in a folder structure.
I need to address them via a batch.
How do I get the first letter of a filename?

Structure:
\A\A
\A\AUHUIHHU
\A\AZZZAHHH
\B\BABBBA
\B\BUHAHHA
etc.

Filenames can start with letters A-Z and numbers 0-9.

I have the filename in %1

Example:
BUHAHHA

Batch:
COPY \B\BUHAHHA \TEMP\

So, I need the \B\ from the first letter of BUHAHHA

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

Re: Foldername first letter of filename

#2 Post by aGerman » 16 Jan 2019 16:21

STEPHAN wrote:
16 Jan 2019 13:10
I have the filename in %1
All you need is assigning it to a regular environment variable. Then use the substring syntax as described in the help of the SET command.

Code: Select all

set "initial=%~1"
set "initial=%initial:~0,1%"
Steffen

STEPHAN
Posts: 2
Joined: 16 Jan 2019 13:01

Re: Foldername first letter of filename

#3 Post by STEPHAN » 16 Jan 2019 16:59

Thanks. I was not aware of the substring options.

Aacini
Expert
Posts: 1617
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: Foldername first letter of filename

#4 Post by Aacini » 17 Jan 2019 09:56

Mmmm.... Why do you need the first letter of the filename? Are not all files that start with "A" in a folder named "A"? And the same for the rest of letters?

Antonio

Post Reply