G
G
granus5002021-01-30 11:57:45
cmd/bat
granus500, 2021-01-30 11:57:45

Is there a bat file code to rename all txt files in a folder, replacing the name with the first line of the file's content?

The folder contains many text files named File001.txt, File002.txt, etc. With the following content
File001.txt
Contents of the file: Winter evening at the dacha
Nina Dedlova Half-darkness. Not too late. Monday. Silence. Pine trees speak with winter. The air is clean and frosty ... And in the country I am alone.

File002.txt Contents of the file:
Coming of winter
Lyudmila Shishenina Fluffy snow - a swan coverlet, Magpie trampled in the morning. Patterns, circling, painted, And proudly walks on the carpet. etc.

We need a bat file to rename all files in the folder, replacing the name with the first line of the file's contents, for example:
File001.txt
File002.txt
, etc. Renames to:

Winter evening in the country. txt
The arrival of winter. txt
, etc.

Windows

Or a program that can.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
granus500, 2021-01-30
@granus500

Solution:
Updated PowerShell
Write batch:
@echo off
powershell -executionpolicy bypass -command "Get-ChildItem -Filter '*.txt' | foreach {Rename-Item $_.FullName """$(Get-Content $_.FullName ^ | Select-Object -First 1)$($_.Extension)"""}"
pause
exit
Solution cyberforum en

W
wisgest, 2021-02-14
@wisgest

For example, something like this on your own CMD:

for %%i in (file???.txt) do call :1 "%%i"
exit /b

:1
for /f "usebackq delims=" %%1 in (%1) do (
  ren %1 "%%1.txt"
  exit /b
)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question