Home > Software design >  Scan for all .csv in current folder and add new column to be populated later based on a previous col
Scan for all .csv in current folder and add new column to be populated later based on a previous col

Time:06-09

I have lots of CSV files with 3 columns where I need to add a 4th one preferably with a batch script or Powershell.

On the 4th column I should add the following string extracted between HTTPs:// and .shopdomain...

This is how it looks now:

ID buyer item url
01 Tom https://safariland.shopdomain.com/fghj/shoes
02 Sam https://beastofshoes.shopdomain.com/ukgl/offers/1424

And how it should look after new column:

ID buyer item url shop
01 Tom https://safariland.shopdomain.com/fghj/shoes safariland
02 Sam https://beastofshoes.shopdomain.com/ukgl/offers/1424 beastofshoes

At my search Info is not being as every question out there is looking for a formula where they need to sum, average or something.

My needs are to cut the text between the before column, not related to sum or average or maths.

Would like a bit of light on how to achieve this if someone can help.

CodePudding user response:

If the url column format is uniform: Split on '/' and take the third item; split that on '.' and take the first item.

s = '''https://safariland.shopdomain.com/fghj/shoes'''
_,_,z,*_ = s.split('/')
whatiwant,*_ = z.split('.')
print(z)
print(whatiwant)

>>>
safariland.shopdomain.com
safariland

CodePudding user response:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory & destination directory are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files\t w o\subdir3"
SET "destdir=u:\your results"

for /f "delims=" %%G in ('dir /b /a-d "%sourcedir%\*.csv"') do (
 rem filename in %%G
 SET "header=Y"
 FOR /f "usebackqdelims=" %%e IN ("%sourcedir%\%%G") DO (
  FOR /f "tokens=2delims=/." %%q IN ("%%e") DO ECHO %%e,%%q&SET "header="
  IF DEFINED header ECHO %%e,"columnheader4"
 )
)>"           
  • Related