Articles

Solved Need batch file to mass edit text

December 5, 2012 at 06:38:50
Specs: Windows 7

Hi guys, I really need help to mass edit text files. More than 5000+ .txt need to edit. Here is description:

1) Put the filename inside txt files as the first line.
For example, my file name: Data-presentation-on-march-2012.txt

Copy that filename and remove the “-”, and make it the first title of the body of my .txt file

So, it will become:
Data presentation on march 2012.

The .txt contents data which is “bla bla bla bla”


So inside my .txt should become;

Data presentation on march 2012

bla bla bla bla

*contain paragraph like that.

Don’t make it it Data presentation on march 2012. bla bla bla bla.


2) Please make it possible for me to add “?” at the end of the title. This is because some of .txt files contains How-To-Create-Bla-Bla.txt

I put these type of .txt on other folder.

I need the script which works as first script above but I able to add “?” on every title because the filename does not contain “?”.

Or if it possible to include anything that start with "who, how, where, when, which" will automatically add "?"

3) Some of filename has many “-”. For example: Data-presentation-2012--Backup only.txt
Make sure the script will take “---” as “ .” , so it will become:
Data presentation 2012. Backup only.

That’s all i need. I really appreciate your help. :) It will take months if I do all these manually. Thanks.


See More: Need batch file to mass edit text

Report •


✔ Best Answer
December 5, 2012 at 18:25:12

On my tests, the path interfered with the renaming operation:
ren temps.txt work\file-name.txt
You could subsitute "move" to fix this error, as written, OR,
since this needs to be a loop anyway, maybe use the drive-path-name features:

@echo off
for /f "tokens=*" %%a in ('dir /b *.txt') do (
set filename=%%na
set filename1=%%nxa
call :title
)
goto :eof

:title
set filename=%filename:-= %
set filename=%filename:backup=.backup%
echo %filename% > temps
copy temps+%filename1%
echo preparing to: move /y temps %filename1%
pause
move /y temps %filename1%



#1
December 5, 2012 at 07:50:05

::This should deal with one and three

@echo off
cd\
set filename=%1
set filename=%filename:c:\=%
set filename1=%filename%
set filename=%filename:-= %
set filename=%filename:backup=.backup%
set filename1=%filename1:c:\=%
echo %filename%>>temps.txt
type "%filename1%">>temps.txt
del %1
ren temps.txt %filename1%
pause

test everything twice :)

:: mike


Report •

#2
December 5, 2012 at 08:37:16

Hi Mike,

Thanks for your effort. But it seem not working for me. I save the file as .bat and I put the file on the folder. Then I run .bat file and nothing changed, cmd show errors.

How to use that code?

My apologize for my ignorance.


Report •

#3
December 5, 2012 at 10:23:29

to test, click and drag one of the files onto the batch

:: mike


Report •

Related Solutions

#4
December 5, 2012 at 10:52:49

I try using some text files, drag them to .bat. Got these errors:

The system cannot find the file specified.
Error occurred while processing: zz\Mathematics.
The system cannot find the file specified.
Error occurred while processing: -History-And-Importance.txt.
The syntax of the command is incorrect.
Press any key to continue . . .


Report •

#5
December 5, 2012 at 11:53:24

sorry try removing the cd\

:: mike


Report •

#6
December 5, 2012 at 12:01:04

So, it will be;

@echo off
set filename=%1
set filename=%filename:c:\=%
set filename1=%filename%
set filename=%filename:-= %
set filename=%filename:backup=.backup%
set filename1=%filename1:c:\=%
echo %filename%>>temps.txt
type "%filename1%">>temps.txt
del %1
ren temps.txt %filename1%
pause

Still not working. How do you make it works? Thanks Mike! I appreciate your effort to help me.


Report •

#7
December 5, 2012 at 13:14:06

Its wierd, worked fine on mine.

ok try adding this to the begining.

cd c:\folder\folder\

and modify the folder to be the working directory

:: mike


Report •

#8
December 5, 2012 at 18:25:12
✔ Best Answer

On my tests, the path interfered with the renaming operation:
ren temps.txt work\file-name.txt
You could subsitute "move" to fix this error, as written, OR,
since this needs to be a loop anyway, maybe use the drive-path-name features:

@echo off
for /f "tokens=*" %%a in ('dir /b *.txt') do (
set filename=%%na
set filename1=%%nxa
call :title
)
goto :eof

:title
set filename=%filename:-= %
set filename=%filename:backup=.backup%
echo %filename% > temps
copy temps+%filename1%
echo preparing to: move /y temps %filename1%
pause
move /y temps %filename1%


Report •


Ask Question