-4

I am trying to rename 2000 txt files based on a String within the file. I've been looking at these links, but do not know at all where to start.

Rename text files based on multiple strings in their contents sublime text- "list lines containing 'find' string

On LINE 56 of the txt file, the string will always read: A30,765,0,1,1,2,N,"4596"

On LINE 66 of the txt file, the string will always read: B60,867,0,3,3,8,220,N,"7731853555"

I want to rename in bulk all the files to whatever is within the quotations. So for this example, I want the file name to be renamed to:

4596 7731853555.txt

I appreciate any help that I can get. thanks in advance.

Regards, Tom

Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
Tom
  • 1
  • 3
  • 4
    Welcome to Stack Overflow. This is not a code/SQL/regex writing service, where you post a list of your requirements and language of choice and a code monkey churns out code for you. We're more than happy to help, but we expect you to make an effort to solve the problem yourself first. Once you've done so, you can explain the problem you're having, include the relevant portions of your work, and ask a specific question, and we'll try to help. Good luck. – Magoo May 04 '18 at 20:15
  • I apologize, but I simply do not know how to code. Currently, my solution as found on Google, is using sublimetext to find the regular expression and output the text onto an excel. I take the CSV file and load it into a program called RenameMaster to do the renaming. However, when the files do not line up, the renamed filenames are shifted by one or two . – Tom May 04 '18 at 20:23
  • Can you please verify that both lines carry a different number of fields? Can you please also verify whether those line numbers are chosen regardless of whether previous lines contain visible characters? – Compo May 04 '18 at 20:24
  • Sorry if i'm slow to understand. Every text file on line 56 will be: A30,765,0,1,1,2,N, "Variable text" Every text file on line 66 will be: B60,867,0,3,3,8,220,N, "Variable text" I'm trying to put the two variable text together as the renamed file – Tom May 04 '18 at 20:28
  • 2
    Where are the text files? Do they currently have a specific naming schedule? It's bad enough that you're asking an off-topic question for free code, the least you can do is to set out the whole environment by making sure that those willing to help don't need to ask you for more information. – Compo May 04 '18 at 20:34

2 Answers2

1
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "outfile=%destdir%\outfile.txt"
(
FOR /f "delims=" %%a IN (
 'dir /b /a-d "%sourcedir%\*.txt" '
 ) DO (
 SET "filename=%%a"
 FOR /f "tokens=1*delims=[]" %%h IN ('find /v /n "" "%sourcedir%\%%a"') DO (
  IF "%%h"=="56" SET "first=%%i"
  IF "%%h"=="66" SET "second=%%i"&CALL :rentxt
 )
)
)>"%outfile%"

GOTO :EOF

:rentxt
FOR %%p IN (%first%) DO SET "part1=%%~p"
FOR %%p IN (%second%) DO SET "part2=%%~p"
ECHO REN "%sourcedir%\%filename%" "%part1% %part2%.txt"
GOTO :eof

You would need to change the settings of sourcedir and destdir to suit your circumstances.

Produces the file defined as %outfile%

Meh - I'm bored, so a few minutes of light relief...

I set the names of directories an files into variables for convenience - to test the procedure on my u: drive.

As written, the code should produce a list of the rename commands it intends to perform in the file nominated as outfile. This allows you to examine the file to determine whether there are any errors in the data in the files (such that lines 56/66 do not follow the form described) without taking action.

To actually rename the files when the testing-for-veracity is complete, simply remove the ECHO keyword in the ECHO REN... line. This will perform the actual rename instead of simply reporting it. The report is then irrelevant, so the single ( line before the for...%%a... can be removed along with the )>"%outfile%" line.

First, the for...%%a performs a directory list of the *.txt files in the source directory, and assigns the filenames found in turn to %%a. The /b and /a-d switches produce a basic (name-only) format and suppress the directory names found respectively.

The find searches for lines in the file which /v do not match "" (which will be every line - even empty ones) and /n number the lines within square brackets. The for/f reads each line of the find output, using [ and ] as delimiters, so the line number appears as the first "token" into %%h and the line contents as the second token (%%i).

Then select line 56 and 66 into separate variables, and note that filename contains potatoes.

When line 66 is found, we call the subroutine :rentxt which processes the strings in first and second from lines 56 and 66 respectively. Since each line consists of a series of comma-separated strings, and comma is a separator, the variables part* are assigned each of these strings in turn, so the for loop ends with the last variable assigned to part*. The ~ conveniently removes any enclosing quotes.

Then, since we have the parts of the lines and the original filename now safely in variables, all we need to do is build the rename instruction and well - echo it for verification, or with the echo removed, rename the file.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thank you for taking the time in explaining to me so elaborately, I appreciate it. I tried to understand but to no avail. I shall pick up some books at a later time. – Tom May 04 '18 at 21:38
0

A PowerShell script, change the Pushd path to fit your environment

## Q:\Test\2018\05\04\SO_50182439.ps1
Pushd "X:\path\to\files"

Get-ChildItem *.txt | ForEach-Object {
    $txtFile = Get-Content $_
    If ($txtFile.Count -ge 66){
       $NewName = $txtfile[55].split(',')[-1].Trim().Trim('"')+" "+
                  $txtfile[65].split(',')[-1].Trim().Trim('"')+$_.Extension
       $_ | Rename-Item -NewName $NewName
    }
}
PopD

  • The script iterates the txt files, reads contents into var $txtFile
  • checks the contents has at least 66 lines
  • splits the lines 55 and 65 (zero based) at the commas
  • gets the last element and trims it from spaces and double quotes
  • puts a space between and appends the extension
  • finally does the renaming.
  • Many thanks to you. This worked beautifully, you saved me a lot of time at my day job. I appreciate it. – Tom May 04 '18 at 21:38