M
M
masmirnov2019-02-22 23:23:30
cmd/bat
masmirnov, 2019-02-22 23:23:30

How to parse txt file using bat file?

Good day! There is a folder with text files.
File structure:
Name - Ivan
Surname - Ivanov
Phone - 79999999999
You need to create a folder for each such user and move the corresponding file there. The name of the folders is the user's phone number. I tried to write the following bat script to solve the problem:

@echo off
 set Src = D:\tmp
 For %%i in ("%Src%\*.txt") do (
  set filepath = %%i
  For %%a in ('findstr "Телефон" %filepath%') do (
  set foldername = %%a
  md %Src%\%foldername%
  )
  move %Src%\%%~nxi %Src%\%foldername%
)

But it doesn't work.
I would be grateful if you help me fix this script to make it work or suggest your own version!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
res2001, 2019-02-23
@res2001

Look at the description:
for /?
Your case is for /f - reads the file line by line and tokenizes it.
For example:

for /f "usebackq tokens=1,2 delims= -" %%a in ("%Src%\*.txt") do (
  echo.%%a = %%b
)

On the set account, I can add another point to Saboteur 's answer - so that spaces that accidentally fall at the end of the line are not assigned to the variable, you need to give the command in this form:
set "Src=D:\tmp"
Your task is normally solved on cmd, so you can don't look for another option.
At the expense of other options - out of the box in Windows are present, except for cmd: PowerShell, JavaScript, VBScript

S
sergey, 2019-02-24
kuzmin @sergueik

in cmd so that the assigned value is used further
in the lines

set foldername=%%a
  md %Src%\%foldername%

should be used
set foldername=%%a
  md %Src%\!foldername!

and at the very beginning to write
, please note: there are a few more errors in the script ...

A
aosvxs7ui, 2019-03-13
@aosvxs7ui

for /f "tokens=* delims=" %%a in ('type [path to file]') do (
for %%b in (%%a) do (
what will you do with what you get from the string
)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question