Ok as the title says, i need a way to rename a text document, to the text there are in the text document, so if the document have this text (cake) then it need to rename the file name to cake.
Asked
Active
Viewed 184 times
-2
-
2What have you tried so far? What problems are you having? – boxdog Oct 14 '19 at 21:10
-
i have only looked on the internet for like 5 hours, and i can't find something that works, that's why i'm asking – Biast12 Oct 14 '19 at 21:20
-
2Please note that you question is too broad. Please ask a question regarding a specific issue with your provided code. That question should be one only, so choose whether to post your failing batch file or your failing powershell script. Along with your properly formatted code, we need to know some example text file names, what their new names are to be, what the content of those files look like, and what happened when you ran it that was unwanted. – Compo Oct 14 '19 at 21:25
-
@Compo if you read what i said over your comment, then you can see i have tried for 5 hours, and haven't found something that works, and that's why i'm asking is there is a way – Biast12 Oct 14 '19 at 21:38
-
1What if the text file has more than one line in it? What should the file be named then? – SomethingDark Oct 15 '19 at 00:26
-
@SomethingDark it's only getting the first word, so if there are 20 words in the text, it will only get the first one – Biast12 Oct 15 '19 at 13:59
1 Answers
0
Here is a pseudo code in batch that use a Regex with a vbscript in order to read and match the first word in the text file when reads from it, and then if the file is not empty, the script will rename your text file with the variable extracted from the vbscript.
How to test this code ?
Just Copy and paste this code below and save it as Rename-First-Word-Content.bat
and create a text file named Test.txt
in the same folder of your batch and write some words on it and save it. Finally just double click on the batch and see the result !
@echo off
Set "InputFile=Test.txt"
Call :Extract "%InputFile%" NewName
If defined NewName (
echo NewName = "%NewName%"
Ren "%InputFile%" "%NewName%.txt"
)
pause & exit
::-----------------------------------------------------------------------------------
:Extract <InputFile> <Variable NewName to be Set>
(
echo WScript.StdOut.WriteLine Extract(Data^)
echo Function Extract(Data^)
echo Dim strPattern,oRegExp,Matches
echo Data = "%~1"
echo Data = WScript.StdIn.ReadAll
echo strPattern = "[^-_\\/:;\x22*.?<>|]([\w\-]+)"
echo Set oRegExp = New RegExp
echo oRegExp.Global = False
echo oRegExp.IgnoreCase = True
echo oRegExp.Pattern = strPattern
echo set colMatches = oRegExp.Execute(Data^)
echo For Each Match in colMatches
echo Content = Match.Value
echo Next
echo Extract = Content
echo End Function
)>"%tmp%\%~n0.vbs"
@For /f "delims=" %%i in ('cscript //nologo "%tmp%\%~n0.vbs" ^< "%~1" 2^>nul') do Set "%2=%%i"
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
Exit /B
::----------------------------------------------------------------------------------

Hackoo
- 18,337
- 3
- 40
- 70
-
ok thanks for the help :D i only got one question, can the code include dots (there are dots in the text)? – Biast12 Oct 15 '19 at 13:06
-