You are not logged in.
Pages: 1
Hello
I'm trying to figure out how to rename my .txt files according to the names of my .missing files.
Please note that they are close, but slightly different (- instead of _ in some places).
Here is what I would like to achieve:
Is there a way to do that?
Thanks in advance!
Offline
Hi and welcome.
IOW, you want to replace every second underscore in a file name by an hyphen?
Read the *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)
Offline
Hello Stefan, thanks for your reply & the welcome.
No, that would be too easy, unfortunately.
I want each .txt file to be renamed exactly like the corresponding .missing file. Hyphens/underscores are not always the only difference, and differences are not always located at the same places.
I think the most reliable method would be to tell Renamer that .txt file beginning with number 1 should have the same name as .missing file beginning with number 1, .txt file beginning with number 2 should have the same name as .missing file beginning with number 2, etc.
For example:
1-abc.txt
1-12aq55.missing
-> 1-abc.txt should be named 1-12aq55.txt
The resulting filenames would be:
1-12aq55.missing
1-12aq55.txt (renamed file)
---------------
2-f48792.txt
2-g445115.missing
-> 2-f48792.txt should be named 2-g445115.txt
The resulting filenames would be:
2-g445115.missing
2-g445115.txt (renamed file)
Also, I would like to exclude files beginning with 0. For example, 0-12546.txt should NOT be renamed.
However, I don't know if that's possible...
Best regards.
Last edited by GS (2019-09-01 15:16)
Offline
Try this Pascal Script:
{ Renaming files based on other files with same number prefix but different extension }
function ExtractNumberPrefix(const S: WideString): WideString;
var
I: Integer;
begin
Result := '';
for I := 1 to Length(S) do
begin
if WidePos(S[I], '0123456789') > 0 then
Result := Result + S[I]
else
Break;
end;
end;
function FindMatchingFile(const FilePath: WideString): WideString;
var
I: Integer;
NumberPrefix, MatchNumberPrefix, Mask: WideString;
Files: TWideStringArray;
begin
Result := '';
NumberPrefix := ExtractNumberPrefix(WideExtractBaseName(FilePath));
if Length(NumberPrefix) > 0 then
begin
SetLength(Files, 0);
Mask := NumberPrefix + '*.missing';
WideScanDirForFiles(WideExtractFileDir(FilePath), Files, False, False, False, Mask);
for I := 0 to Length(Files) - 1 do
begin
MatchNumberPrefix := ExtractNumberPrefix(WideExtractBaseName(Files[I]));
if MatchNumberPrefix = NumberPrefix then
Result := Files[I];
end;
end;
end;
var
MatchingFile: WideString;
begin
MatchingFile := FindMatchingFile(FilePath);
if Length(MatchingFile) > 0 then
FileName := WideExtractBaseName(MatchingFile) + WideExtractFileExt(FilePath);
end.
See the demo screenshot:
Last edited by den4b (2019-09-01 16:31)
Offline
EDIT: oh, I am way too slow.
- - - - -
So:
- For each file name like "<digit(s)><random text>" with extension ".missing"
-- if there is a file name whit same digit(s) at the beginning and extension ".txt"
- - - rename that txt file with the name of the "missing"-file.
Or no, we would do it the other way around:
- For each "*.txt" file, is there a "*.missing" file with the same digits at the begin,
than rename that txt with the base name of that missing file.
FROM:
1-abc.txt
1-12aq55.missing
TO:
1-12aq55.txt <<<< renamed
1-12aq55.missing
Usage:
- have a backup!!!
- save below code into ReNamer folder as "test.pas" text file, see
http://www.den4b.com/wiki/ReNamer:Rules:PascalScript
- load all files
- add this script as PascalScript-Rule
- click [Preview]
//http://www.den4b.com/forum/viewtopic.php?pid=10995#p10995
//For each TXT, if there is a MISSING with same begin as the TXT file,
//then rename the TXT by the base name of the MISSING file.
//
// FROM:
// 1-abc.txt
// 1-12aq55.missing
// TO:
// 1-12aq55.txt <<<< renamed
// 1-12aq55.missing
var
strArrItem,strCurrDir,strDigitsAtStart,strDigitsAtStart2: WideString;
arrFilesInFld,arrRegExSubPatterns: TWideStringArray;
idx:Integer;
begin
//if file has extension "*.txt"
if WideUpperCase(WideExtractFileExt(FileName)) = '.TXT' then
begin
//get first digits from file name:
arrRegExSubPatterns:=SubMatchesRegEx(FileName,'^(\d+).+',false);
if Length(arrRegExSubPatterns) <1 then exit;
strDigitsAtStart := arrRegExSubPatterns[0];
if (strDigitsAtStart = '0') then exit; //by user request
//get files in dir with extension "*.missing"
strCurrDir := WideExtractFileDir(FilePath);
SetLength(arrFilesInFld, 0);
WideScanDirForFiles(strCurrDir, arrFilesInFld,False, False, False, '*.missing');
if Length(arrFilesInFld) <1 then exit;
//for each of that files:
for idx:=0 to Length(arrFilesInFld)-1 do
begin
strArrItem := WideExtractBaseName(arrFilesInFld[idx]);
//get first digits from file name:
arrRegExSubPatterns:=SubMatchesRegEx(strArrItem,'^(\d+).+',false);
if Length(arrRegExSubPatterns) <1 then exit;
strDigitsAtStart2 := arrRegExSubPatterns[0];
//if same digits on both names:
if (strDigitsAtStart = strDigitsAtStart2) then
FileName := strArrItem
+ WideExtractFileExt(FileName);
end;
end;
end.
HTH?
Last edited by Stefan (2019-09-01 17:18)
Read the *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)
Offline
Hello, it works perfectly, thank you to both of you
Offline
Pages: 1