Extract Part of a Filename

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
wolves1
Posts: 7
Joined: 10 Oct 2019 10:50

Extract Part of a Filename

#1 Post by wolves1 » 10 Oct 2019 11:05

Hello,

I receive nightly CSV data in a folder and I merge all the CSV data into one file for upload to another portal. For now, in my script I have it where it also extracts the file and inputs it back into the target file (completed merged copy) as a new column. Instead of the full filename, can I extract partial of the file name which is a date. This is my current script currently, and I'm not too sure how to implement that piece into it. Any help is greatly appreciated!!

All the CSV files have the same naming format of: HelloFiles.2019-10-08.1111 I only need this part: 2019-10-08

Code: Select all

@echo off
setlocal
set CUR_YYYY=%date:~10,4%
set CUR_MM=%date:~4,2%
set CUR_DD=%date:~7,2%
 
set folder=C:\Users\myname\Desktop\Extract
set mask=*.csv
set output=C:\Users\myname\Desktop\Combined\%CUR_YYYY%%CUR_MM%%CUR_DD%_Data-Upload.csv
 
for /f "tokens=*" %%G in ('dir "%folder%\%mask%" /a:-d /b') do (
 for /f "tokens=*" %%H in ('type "%folder%\%%G"') do (
  echo %%G	%%H>> "%output%"
 )
)
pause
Last edited by aGerman on 10 Oct 2019 11:59, edited 1 time in total.
Reason: code formatting

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

Re: Extract Part of a Filename

#2 Post by aGerman » 10 Oct 2019 12:06

Not sure what to do with the date. Extracting it from the file name should be quite easy though. Say, the file name is in %%G in your nested loop ...
untested:

Code: Select all

for /f "tokens=2 delims=." %%I in ("%%~nG") do echo %%I
You should find the date in %%I, right?

Steffen

wolves1
Posts: 7
Joined: 10 Oct 2019 10:50

Re: Extract Part of a Filename

#3 Post by wolves1 » 10 Oct 2019 12:39

I tried extracting only those characters from the file name, but I also couldn't get it to work. I'm probably doing something wrong :(

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

Re: Extract Part of a Filename

#4 Post by aGerman » 10 Oct 2019 12:50

Not sure what you tried because you didn't show the code.

Steffen

wolves1
Posts: 7
Joined: 10 Oct 2019 10:50

Re: Extract Part of a Filename

#5 Post by wolves1 » 10 Oct 2019 14:34

Sorry, I did find something that was closely related, but I wasn't sure how to incorporate it (if possible) with what I have already. I'm still relatively new in scripting, so please excuse me if confusing any part of it. This script was part of another post that I found while researching. The "set extract" is somewhat what I'm actually looking for to only extract the date out of the filename.

Code: Select all

@echo off
setlocal
for %%a in (c:\myfolder\*.*) do set filename=%%~na
set extract=%filename:~3,4%


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

Re: Extract Part of a Filename

#6 Post by aGerman » 10 Oct 2019 16:00

But that's not the code I offered to you. Have you been able to include it in your nested loop as I wrote? Maybe like that:

Code: Select all

for /f "tokens=*" %%G in ('dir "%folder%\%mask%" /a:-d /b') do (
  for /f "tokens=2 delims=." %%I in ("%%~nG") do echo %%I
)
Steffen

wolves1
Posts: 7
Joined: 10 Oct 2019 10:50

Re: Extract Part of a Filename

#7 Post by wolves1 » 10 Oct 2019 16:30

Hi Steffen,

I tried the code you have me tried in the nested loop like this, but it returned nothing in my file. Please let me know if I placed it incorrectly. I'm sorry :(

Code: Select all

for /f "tokens=*" %%G in ('dir "%folder%\%mask%" /a:-d /b') do (
for /f "tokens=2 delims=." %%I in ("%%~nG") do echo %%I
 for /f "tokens=*" %%H in ('type "%folder%\%%G"') do (
  echo %%G	%%H>> "%output%"
 )
)
pause

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

Re: Extract Part of a Filename

#8 Post by aGerman » 10 Oct 2019 17:04

It writes the date into the console window since you don't redirect it into the file. You have to know how it should look like in your file. I don't even understand what the
for /f "tokens=*" %%H in ('type "%folder%\%%G"') do ( ...
is for.

Steffen

wolves1
Posts: 7
Joined: 10 Oct 2019 10:50

Re: Extract Part of a Filename

#9 Post by wolves1 » 10 Oct 2019 17:30

The script was originally written by a coworker of mine, but I'm assuming that is the line that appends the filename as a new column to the target (completed merged CSV) file. See attached image of how it would look like when I open the CSV file in Excel (confidential information is erased). My goal is to have only the date characters of the filename instead of the full filename.
Attachments
Capture.PNG
Capture.PNG (2.22 KiB) Viewed 2498 times

wolves1
Posts: 7
Joined: 10 Oct 2019 10:50

Re: Extract Part of a Filename

#10 Post by wolves1 » 10 Oct 2019 17:35

I'm not sure if that's possible, or another option would be to extract the "Date Modified" of the file and append it as a new column in the target CSV file. Which I'm also not too sure on where to begin for that.

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

Re: Extract Part of a Filename

#11 Post by aGerman » 11 Oct 2019 00:27

OK now It makes more sense even if the delimiter in a csv file is usually not a tab.

Code: Select all

for /f "tokens=*" %%G in ('dir "%folder%\%mask%" /a:-d /b') do (
 for /f "tokens=2 delims=." %%I in ("%%~nG") do (
  for /f "tokens=*" %%H in ('type "%folder%\%%G"') do (
   echo %%I	%%H>> "%output%"
  )
 )
)
Steffen

wolves1
Posts: 7
Joined: 10 Oct 2019 10:50

Re: Extract Part of a Filename

#12 Post by wolves1 » 11 Oct 2019 09:45

Steffen,

Thank you so much for your help. It worked perfectly just how I needed it to!! :D

Post Reply