• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Renaming of Articles, Programmatically (PHP, VBS, Python, Etc.)

flexy

Diamond Member
I want to do the following:

Say I have a folder which contains any number of "articles", which MAY be named like article01.txt, article02.txt ... article99.txt (or whatever other *.txt)

Each particular article in his FIRST line has the title in it

Code:
What you need to know about Windows 8.1

Blah BlahBlah Blah
[the actual article]
The script (preferably running under Windows) should read this first line of the (each) article (in the folder) and rename the file. Say, turn "article01.txt" into "What You Need to Know About Windows.txt"

Preferably also having a character limit how long the title can be and obviously also filter out special characters which are not suitable to use in a filename.

OK:

REM @ echo off
SetLocal ENABLEDELAYEDEXPANSION
for %%i in (.\*.txt) do (
set /p var= <"%%i"
REM ren %%i "!var!.txt"
ren %%i "!var!.txt"
)


Problem for now only the filtering...
 
Last edited:
This would be trivially easy if you use a proper scripting language (not a batch file) with regular expressions. Perl would be my personal choice, but there are countless other options.


Edit: Here's an example of what it would look like in Perl
Code:
foreach $oldname (<*.txt>) {
   open file, '<', $oldname;
   $title = <file>;
   close file;

   $title =~ s/[\\\/:\*\?"<>\|\r\n]/_/g;
   $newname = substr($title, 0, 150) . ".txt";

   print "$oldname: $newname\n";
   rename $oldname, $newname;
}
 
Last edited:
Back
Top