#1 2009-05-23 12:28

dinosaur-jr
Member
Registered: 2009-02-24
Posts: 5

Copy folder.jpg & rename copied file like parentfolder

Hey Guys,

I need a script to copy folder.jpg and rename it to parentfoldername.jpg

for example:

D:\A View To A Kill (1985)\folder.jpg -> create a copy of folder.jpg in the same folder and rename it to D:\A View To A Kill (1985)\A View To A Kill (1985).jpg

Does anyone know of any script to do that?

Offline

#2 2009-05-23 20:50

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: Copy folder.jpg & rename copied file like parentfolder

I know nothing about possibility of coping files by ReNamer.
So you might need to do it yourself (eg. by a dos batch file) before using ReNamer.

You can use :File_FolderName: metatag to access parent folder name. See that topic for similiar problem.
So Delete rule first and then Insert (metatag) rule.

Last edited by krtek (2009-05-23 20:50)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#3 2009-05-25 11:38

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: Copy folder.jpg & rename copied file like parentfolder

I have forgotten why there is no copy function with PascalScript. Or i have overseen this feature?

So i have used the function ExecConsoleApp(const CommandLine: String; out Output: String): Cardinal;
to execute an simple batch:

C:\tmp\cop.cmd

@ECHO off
ECHO %0 - %1  - %2
COPY %1 %2 
PAUSE

Second ECHO and PAUSE is only to see if the command line  is correct.
Note: DOS batch have problems with extended ascii chars.   
I will exchange this batch with an autohotkey script.
But the idea is the same:  use an external tool to do the copy-work.

(BTW:
Denis, the  ExecConsoleApp drops the very first char of the returning output,
f.ex.:   the app (my batch) returns "c:\tmp\cop.cmd" (due the ECHO %0 command),   but  i get  ":\tmp\cop.cmd"  only)




From the PascalScript i call this external tool with two parameters: old name + new name,
to make the copy of an file. (here only for the file with basename 'folder')

This should copy 'folder.jpg'  to 'folder_new.jpg'    (just an temp name)



Then i split the path of 'folder.jpg'  into parts at "\"  and find the last part with UBound(array).
This is the 'parent folder'  name part.

Then i build an new name with  "FileName := myParent+myExtension;"


############################

Now  i have forgotten that the  'folder_new.jpg'   is still remain :-(
But this could simply be added to the script, i think.

EDIT: that's not that easy as i thought, because i have 'timing' problems, what renaming command is executed first, intern or extern.

So this script is not 100% finished !     And as always:  test with test files in test folders on test pc's :-)

But this second part (rename folder_NEW.jpg back)  is commented out, so this script should work.
Next, load the files again, add an replace rule:  "_NEW" with <nothing> and you're done.
(Maybe modify the   myNewString := '_NEW'; -part  to something  better not conflicting with parts of your file names?)

############################



//needs c:\tmp\cop.cmd :
//@ECHO off
//ECHO Test  %0 - %1 - %2
//COPY %1 %2 
//PAUSE

var
Command, Output, myBaseName, myExtension, myNewString, myParent, myCommand, myPattern: string;
myPathArray: TStringsArray;
UBound: Integer;

Begin
// User settings (fill some var's with content):
myCommand := 'c:\tmp\cop.cmd  ';
myNewString := '_NEW';
myPattern := 'folder';

  
  // action only, IF file base name is like 'folder': ////////////////////////////////////////////////
  IF (WideExtractBaseName(FileName) = myPattern) then
  begin

   // Fill var's with current path and file name parts:  ////////////////////////////////////////////
   myBaseName := WideExtractFilePath(FilePath)+WideExtractBaseName(FileName);
   myExtension := WideExtractFileExt(FileName);

   //Copy original file to new file /////////////////////////////////////////////////////////////////////
 //Command := 'c:\tmp\cop.cmd                 %1                                  %2           ';   
   Command := myCommand +'"'+myBaseName+myExtension+'"  "'+myBaseName+myNewString+myExtension+'"';
   WideShowMessage(Command);
   ExecConsoleApp(Command, Output);
   WideShowMessage(Output);


    //find parent folder name and rename original file to parentfoldername://///////////
    myPathArray := WideSplitString(myBaseName, '\');
    UBound := Length(MyPathArray);
    myParent := myPathArray[UBound -2];
    //WideShowMessage(myParent);
    //WideShowMessage(FileName);
    FileName := myParent+myExtension;
    
    //rename backup-file back to original name:  ////////////////////////////////////////////
    //   Command := myCommand + ' "'+myBaseName+myNewString+myExtension+'"  "'+myBaseName+myExtension+'" ';
    //   WideShowMessage(Command);
    //   ExecConsoleApp(Command, Output);
    //   WideShowMessage(Output);

  End
End.

Last edited by Stefan (2009-05-25 15:04)


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

#4 2009-05-25 15:34

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: Copy folder.jpg & rename copied file like parentfolder

It much simpler:

But  as always:  test with test files in test folders on test pc's :-)

//needs c:\tmp\cop.cmd :
//    @ECHO off
//    COPY %1 %2 


var
Command, Output, myPath, myBaseName, myExtension, myParent, myCommand, myPattern: string;
myPathArray: TStringsArray;

Begin
myCommand := 'c:\tmp\cop.cmd  ';
myPattern := 'folder';

  IF (WideExtractBaseName(FileName) = myPattern) then
  begin
    myPath := WideExtractFilePath(FilePath);
    myBaseName := WideExtractBaseName(FileName);
    myExtension := WideExtractFileExt(FileName);

    //find parent folder name:
    myPathArray := WideSplitString(myPath, '\');
    myParent := myPathArray[Length(MyPathArray) -2];
    
    Command := myCommand +'"'+myPath+myBaseName+myExtension+'"  "'+myPath+myParent+myExtension+'"';
    ExecConsoleApp(Command, Output);
  End
End.

I have:
A View To A Kill (1985)\folder.jpg

I get:
A View To A Kill (1985)\folder.jpg
A View To A Kill (1985)\A View To A Kill (1985).jpg


HTH?


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

#5 2009-05-25 19:01

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: Copy folder.jpg & rename copied file like parentfolder

OK, i have compiled an AutoHotkey script which does the same as the DOS batch above,
but should work with umlaute and accents too, due the power of AutoHotkey  (www.AutoHotkey.com).

Just download the CopyAB.exe (196kB) and put it right near the ReNamer.exe.
    http://rapidshare.de/files/47296585/CopyAB.exe.html
(Denies, you may adapt this exe and the script, onces if it is tested, and add it to your distribution, if you want)


But  as always:  test with test files in test folders on test pc's :-)


Here is an script CopyAB.pas to use this CopyAB.exe

// CopyAB.pas, copies an file to an new name.
// Needs CopyAB.exe (an compiled AutoHotkey script which does only copy myOldName to myNewName) near ReNamer.exe
//"FileCopy, %myOldName%, %myNewName%, %Overwrite%", See AutoHotkey help 

// Note: this script act on file-base-name 'folder', f.ex. for 'folder.jpg'
// For other file-base-names you can modify myBaseNamePattern var

// To act on full file name like 'name.ext', you have to modify the IF-statement, f.ex. like: 
//   myNamePattern := 'name.ext';
//   IF (WideExtractFileName(FileName) = myNamePattern) then


var
Command, myPath, myBaseName, myExtension, myParentFolderName: string;
myTool, myNamePattern, myBaseNamePattern, myOldName, myNewName: string;
myPathArray: TStringsArray;

Begin
myTool := 'COPYAB.exe  ';
myBaseNamePattern := 'folder';
//myNamePattern := 'name.ext';

  IF (WideExtractBaseName(FileName) = myBaseNamePattern) then
  //IF (WideExtractFileName(FileName) = myNamePattern) then
  begin
    myPath := WideExtractFilePath(FilePath);
    myBaseName := WideExtractBaseName(FileName);
    myExtension := WideExtractFileExt(FileName);
    myOldName := myPath+myBaseName+myExtension 
    
    //find parent folder name:
    myPathArray := WideSplitString(myPath, '\');
    myParentFolderName := myPathArray[Length(MyPathArray) -2];

    //build new name:
          myNewName := myPath+myParentFolderName+myExtension
    // or myNewName := myPath+myBaseName+'_COPY'+myExtension
    // or myNewName := myPath+myBaseName+myExtension+'bak'
    // or myNewName := myPath+myParentFolderName+'_'+myBaseName+myExtension+'bak'

    //use external command to copy file to new name:
    Command := myTool +'"'+myOldName+'"  "'+myNewName+'"';
    ExecuteProgram(Command, True);
  End
End.


Here is the AutoHotkey source code of CopyAB.exe

;// simple AutoHotkey script to copy file to an new name    Stefan, Mai 2009 for ReNamer by Den4b
;// Syntax: %A_ScriptName% "para1"  "para2" [Flag]
;// Instead of Flag write an 1 to overwrite existend file if you want.
#NoTrayIcon
#SingleInstance force

If %0% > 2
    {
myOldName = %1%
myNewName = %2%
Overwrite = %3%
}
Else
MsgBox This tool does nothing then copy parameter 1 To parameter 2`nSyntax: %A_ScriptName% "para1"  "para2" [Flag]`nInstead of Flag write an 1 to overwrite existend file if you want.

;FileCopy, SourcePattern, DestPattern [, Flag]
; Flag (optional) this flag determines whether to overwrite files if they already exist:
; 0 = (default) do not overwrite existing files
; 1 = overwrite existing files

FileCopy, %myOldName%, %myNewName%, %Overwrite%

ExitApp

HTHsome1?


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

#6 2009-05-26 20:06

dinosaur-jr
Member
Registered: 2009-02-24
Posts: 5

Re: Copy folder.jpg & rename copied file like parentfolder

Hey guy's

First of all: Thank you for all the suggestions / solutions.
Second sorry for this late response, had a busy few days.

Anyways, i will give it a shot and report back A.S.A.P.

BTW. Have to say i love this forum, because you always have a great solution.

Salute!
big_smile

Offline

#7 2009-05-26 20:37

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: Copy folder.jpg & rename copied file like parentfolder

Yeah, we like to make to much quite often.

Stefan, welcome in the club: http://www.den4b.com/forum/viewtopic.php?pid=3287#p3287
big_smile lol big_smile

Last edited by krtek (2009-05-26 20:37)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#8 2009-05-27 11:04

Andrew
Senior Member
Registered: 2008-05-22
Posts: 542

Re: Copy folder.jpg & rename copied file like parentfolder

lol Good one! lol

BTW, since a batch file is being used anyway (in the earlier solution at least, before the AHK script), how about this one instead that does the whole job? (ReNamer is not always the right tool for the job IMO, especially when things such as copying files, editing files etc. are involved.)

You can either download it from http://www.mediafire.com/download.php?zmymjemgdjm, save it to any directory you want to scan and run it, or else you can copy the code below, paste it in Notepad, save it as "CopyFiles.bat" (with the quotes) and run it. Kindly test and let me know if it solves your problem. smile

@echo off
cls
echo Batch Script by Andrew for http://www.den4b.com/forum/viewtopic.php?id=641
echo.
set dir1=
set dir2=
setlocal enabledelayedexpansion
for /r %%f in (folder.jpg) do (
    set dir1=%%~dpf
    set dir2=%%~dpf
    call :getcurdir "!dir2!"
    if exist "!dir1!folder.jpg" (
        attrib -h -s "!dir1!folder.jpg"
        echo Copying "!dir1!folder.jpg"
        copy "!dir1!folder.jpg" "!dir1!!dir2!.jpg">NUL
    )
)
goto end

:getcurdir
set dir2=%1
set dir2=%dir2:"=%
:loop
for /f "tokens=1,* delims=\" %%i in ("%dir2%") do (
    if not "%%j"=="" (
        set dir2=%%j
        goto loop
    )
)
set dir2=%dir2::=%
set dir2=%dir2:\=%
goto :eof

:end
set dir1=
set dir2=
endlocal
echo.
echo Scan for folder.jpg in current directory and all its subdirectories complete!

Note: Not guaranteed to run on any version of Windows prior to Windows 2000!

Last edited by Andrew (2009-05-27 15:46)

Offline

Board footer

Powered by FluxBB