Get the first few characters without the extension of the file in DOS

3

My files are AAAAAAA-01.pdf , BBBBBB-02.pdf, CCCCCCC-03.pdf I was able to generate an excel list of file name in the folder without the extension using this command below:

for %%i in (*.pdf) do @echo %%~ni >> C:\LIST.xls

How do I have it get the first 7 characters of the file? I want the out put list of just AAAAAA , BBBBBBB, CCCCCCC , ...

My second questions is: How do I only get those characters before the DASH - ? For example my files are ABCD-01.pdf , A-03.pdf, AB-00.pdf, ... I want the output to be ABCD , A, AB, ...

I found this set first7=%%I:~0,7% but don't know how to get it to work.

Thanks in advance for your help

Chaunghian

Posted 2015-06-19T16:30:33.070

Reputation: 33

You can always import the whole name into Excel then convert the data using excel formulas. That is almost always my pref. Example: import your list to column A, make column B something like =LEFT(A1,7) and copy it down the column, then hide Column A – Tyson – 2015-06-19T16:39:33.983

I am running this in a BATCH FILE and schedule it to run multiple times a day. I have other excel file pull data from this output list using vlookup. I don't want to open the output list and have it generate 7 characters everytime. – Chaunghian – 2015-06-19T16:47:26.503

Answers

1

@echo off
setlocal enabledelayedexpansion
if exist List.xls del List.xls
for %%a in (*.pdf) do (
    set fn=%%~na
    set fn=!fn:~0,7!
    for /f "tokens=1 delims=-" %%f in ('echo !fn!') do echo %%f>>List.xls
)

This will print the first 7 letters of each file name. However if there's a hyphen present then it will further truncate and print only the letters before the hyphen.

Karan

Posted 2015-06-19T16:30:33.070

Reputation: 51 857

He he, my answer is shorter than yours - effectively a one liner ;) – DavidPostill – 2015-06-19T17:24:04.830

The second part of my answer prints only the letters before the hyphen however many there are (in one for /f statement). You don't need to extract the 7 letters first. – DavidPostill – 2015-06-19T17:29:15.180

Unhh... What if the hyphen's present after the 8th letter? Won't you end up with 8 letters then? My batch file takes care of both his requirements in one go. Hope that's clear now? – Karan – 2015-06-19T17:30:58.977

OK. You win ;) I read his (a little unclear) question as two kind of unrelated parts ... – DavidPostill – 2015-06-19T17:34:06.810

@Karan thank you for your answer. I just tried it. It created 2 spaces and the end of the file name. Do you know how to remove those 2 blank spaces?. Also, everytime I ran the batch file, the list add on to the previous list. Is there a way to overwrite the old list? – Chaunghian – 2015-06-19T18:34:42.630

@Chaunghian: Kindly let me know the original file names and the corresponding strings that the batch file printed, because I'm not able to figure out where it's printing these 2 spaces you speak of. To overwrite the old list is simple. Just use echo %%f>List.txt instead of echo %%f>>List.txt. A single > overwrites the file whereas >> appends to the file instead. – Karan – 2015-06-19T18:39:36.640

@Karan I tried it like this echo !_name:~0,7!>c:\list.xls . It overwrited the file. However, only 1 file name appear in the list now. This one, with the space at the end "T3L954- " . The original file names are K14O211-02.pdf, R14L211-03.pdf, T3L954-56.pdf, S3L054-41.pdf – Chaunghian – 2015-06-19T19:09:07.613

@Chaunghian: Try the modified batch file above. – Karan – 2015-06-19T19:18:03.413

@Chaunghian: You're welcome. :) – Karan – 2015-06-19T19:30:08.337

2

How do I have it get the first 7 characters of the file?

I want the out put list of just AAAAAA , BBBBBBB, CCCCCCC , ...

Use the following batch file (to extract the first 7 letters of the filename).

test.cmd:

@echo off
@Setlocal EnableDelayedExpansion
for %%i in (*.pdf) do (
  set _name=%%~ni
  echo !_name:~0,7!>> c:\list.xls
  )

example output:

C:\test>dir *.pdf
 Volume in drive C has no label.
 Volume Serial Number is C8D0-DF1E

 Directory of C:\test

19/06/2015  17:56                 0 AAAAAAA-01.pdf
19/06/2015  17:56                 0 BBBBBBB-02.pdf
19/06/2015  17:57                 0 CCCCCCC-03.pdf
               3 File(s)              0 bytes
               0 Dir(s)  92,871,524,352 bytes free

C:\test>test

C:\test>type c:\list.xls
AAAAAAA
BBBBBBB
CCCCCCC

C:\test>

How do I only get those characters before the DASH - ?

For example my files are ABCD-01.pdf , A-03.pdf, AB-00.pdf, ... I want the output to be ABCD , A, AB, ...

Use the following batch file (to extract all of the letters before a -)

test.cmd:

@echo off
for /f "usebackq tokens=1 delims=-" %%i in (`dir /b *.pdf`) do echo %%i>> c:\list.xls

example output:

C:\test>dir *.pdf
 Volume in drive C has no label.
 Volume Serial Number is C8D0-DF1E

 Directory of C:\test

19/06/2015  18:13                 0 A-03.pdf
19/06/2015  17:56                 0 AAAAAAA-01.pdf
19/06/2015  18:14                 0 AB-00.pdf
19/06/2015  18:13                 0 ABCD-01.pdf
19/06/2015  17:56                 0 BBBBBBB-02.pdf
19/06/2015  17:57                 0 CCCCCCC-03.pdf
               6 File(s)              0 bytes
               0 Dir(s)  92,870,991,872 bytes free

C:\test>test

C:\test>type c:\list.xls
A
AAAAAAA
AB
ABCD
BBBBBBB
CCCCCCC

C:\test>

Further reading

DavidPostill

Posted 2015-06-19T16:30:33.070

Reputation: 118 938

Answer updated to answer second part of question. – DavidPostill – 2015-06-19T17:25:24.857

@Chaunghian the other answer is better as my two batch files don't work if there is more than 7 characters before the - :(. If you don't mind more than 8 characters then the scond part of my answer is shorter than the other one. ;) – DavidPostill – 2015-06-19T18:22:28.587

second part of your answer created a space at the end of each file name. Any idea how to get rid of that? – Chaunghian – 2015-06-19T18:26:37.267

remove the space before >> like echo %%i>> c:\list.xls – DavidPostill – 2015-06-19T18:41:16.670

thanks David, I export the list as .txt then it's fine. However, export it to .xls then it has the space at the end. Thanks for your help. – Chaunghian – 2015-06-19T19:18:04.880