Archive: Batch append to file name?


Batch append to file name?
I've got an installer that I need to batch rename some files by just adding some letters to the beginning of the file names. For example, I have:

17-this file.ppt
36-another file.ppt

All I want to do is rename them to include "CW" before the file name. Thus:

CW17-this file.ppt
CW36-another file.ppt

And I've got a whole folder of these.

I tried:

rename "$INSTDIR\*.*" "$INSTDIR\CW*.*"

But that doesn't work.

Any ideas?


Make a loop with FindFirst/FindNext e.g.

ClearErrors
FindFirst $0 $1 "$INSTDIR\*.ppt"
IfErrors end
Rename "$INSTDIR\$1" "$INSTDIR\CW$1"

loop:
FindNext $0 $1
IfErrors end
Rename "$INSTDIR\$1" "$INSTDIR\CW$1"
goto loop

end:
FindClose $0

Thanks a bunch ... but here's how my file was named:
CWCWCWCWCWCWCWCWCWCWCWCWCWCWCWCWCWCWCWCWCWCWCWCWCW
CWCWCWCWCWCWCWCWCWCWCWCWCWCWCWCWCWCWCWCWCWCWCWCWCW
CWCWCWCWCWCWCWCWCWCWCWCWCWCW2-Name.ppt

:)

Could it have something to do with me already using $0 and $1? Here's my code:


Section -Full SEC0000
CheckSpace:
StrCpy $0 3000000 ; kb u need
StrCpy $1 'C:'; check drive c: for space
Call CheckSpaceFunc
IntCmp $2 1 Begin
MessageBox MB_RETRYCANCEL "Message" IDRETRY CheckSpace
Quit
Begin:
${GetDrives} "CDROM" "ContinueInstall"
StrCmp $R0 Founded +3
MessageBox MB_RETRYCANCEL "Message" IDRETRY begin
Quit
SectionEnd

Function ContinueInstall
IfFileExists "$9\Library Catalog.fdb" 0 end
SetOverwrite on
# Copy PPTs from disc
CreateDirectory $INSTDIR\Library
# Extract program folders to HD
SetOutPath $INSTDIR
File "preslib.ico"
# Move/rename files from old version(s)
ClearErrors
FindFirst $0 $1 "$INSTDIR\Library\Images\C\CreationWise\*.ppt"
IfErrors endRen
Rename "$INSTDIR\Library\Images\C\CreationWise\$1" "$INSTDIR\Library\Images\C\CreationWise\CW$1"

loop:
FindNext $0 $1
IfErrors endRen
Rename "$INSTDIR\Library\Images\C\CreationWise\$1" "$INSTDIR\Library\Images\C\CreationWise\CW$1"
goto loop

endRen:
FindClose $0
rename "$INSTDIR\Library\Videos\Day Month Year V001.avi" "$INSTDIR\Library\Videos\AiG\Day Month Year V001.avi"
rename "$INSTDIR\Library\Videos\Sunless Day V002.avi" "$INSTDIR\Library\Videos\AiG\Sunless Day V002.avi"
# Write Portfolio Preferences in the Registry
WriteRegStr HKLM "${REGKEY}\Components" Full 1
WriteRegDWORD HKEY_CURRENT_USER "Software\Extensis\Portfolio 8.0\Preference" "Tabbed Window Mode" 0
WriteRegDWORD HKEY_CURRENT_USER "Software\Extensis\Portfolio 8.0\Preference" "Dbl Click Item Action" 2
StrCpy $R0 Founded
StrCpy $0 StopGetDrives
end:
Push $0
FunctionEnd

Strange... this works perfect for me, let me check further

outfile 'find.exe'
ShowInstDetails show

section -
SetOutPath "$EXEDIR"
CreateDirectory "$EXEDIR\boo"

FileOpen $R0 "$EXEDIR\boo\test1.txt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\test2.txt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\test3.txt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\test4.txt" w
FileClose $R0

StrCpy $INSTDIR "$EXEDIR\boo"

ClearErrors
FindFirst $0 $1 "$INSTDIR\*.txt"
IfErrors end
Rename "$INSTDIR\$1" "$INSTDIR\CW$1"

loop:
FindNext $0 $1
IfErrors end
Rename "$INSTDIR\$1" "$INSTDIR\CW$1"
goto loop

end:
FindClose $0
sectionend

I think I got it, it's because the file name begins with numeric value so when the file 01-file.ppt is renamed goes down and findnext finds it again.
We need a work around this issue, I'll post back in a while.


Thanks! You're just oozing with NSIS knowledge. I really appreciate your help!


Thanks!
I have a work around...

outfile 'find.exe'
ShowInstDetails show

!include logiclib.nsh

section -
SetOutPath "$EXEDIR"
CreateDirectory "$EXEDIR\boo"

FileOpen $R0 "$EXEDIR\boo\01-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\02-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\03-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\04-some file.ppt" w
FileClose $R0

StrCpy $INSTDIR "$EXEDIR\boo"

ClearErrors
FindFirst $0 "$1" "$INSTDIR\*.*"

${do}
FindNext $0 "$1"
${If} ${Errors}
${exitdo}
${else}
StrCpy $2 $1 "" "-3"
${unless} $2 != "ppt"
Rename "$INSTDIR\$1" "$INSTDIR\CW$1"
${endunless}
${endif}
${loop}

FindClose $0
sectionend

Would this work?

ClearErrors
FindFirst $0 $1 "$INSTDIR\*.ppt"
IfErrors end
StrCpy $2 $1
;Rename "$INSTDIR\$1" "$INSTDIR\CW$1"

loop:
FindNext $0 $1
IfErrors end
Rename "$INSTDIR\$1" "$INSTDIR\CW$1"
goto loop

end:
Rename "$INSTDIR\$2" "$INSTDIR\CW$2"
FindClose $0


Nope. That still looped into "CWCWCWCWCWCWCW..."


BTW, it's very interesting that the first file in the sequence gets properly named. But everything else gets the CW overdrive treatment.


I figured it out. The problem was what I thought (and posted earlier).

I used Red Wine's first script, and just changed the variables, since some of them were already used. $0 -> $3, $1 -> $4, etc. And then it worked.

[b]EDIT]/b]: Actually, I had to comment out a line as bnicer suggested, lest the last file get renamed twice.


GRR! Strike that. The problem seems to arise when I have more than 10 files.

EDIT: No, the problem is with 27 or more files. Is there some limitation to variables?


Well, did you try to compile/execute the work around I've posted above?
Works perfect for me :)


Oh. I didn't see that. The same problem with this script, but this time, only with 25 or more files. The original script worked until 27 files. Weird stuff.


I think this is ok now,

outfile 'find.exe'
ShowInstDetails show

!include logiclib.nsh

section -
SetOutPath "$EXEDIR"
CreateDirectory "$EXEDIR\boo"

FileOpen $R0 "$EXEDIR\boo\01-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\02-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\03-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\04-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\05-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\06-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\07-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\08-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\09-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\10-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\11-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\12-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\13-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\14-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\15-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\16-some file.ppt" w
FileClose $R0

FileOpen $R0 "$EXEDIR\boo\17-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\18-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\19-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\20-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\21-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\22-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\23-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\24-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\25-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\26-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\27-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\28-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\29-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\30-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\31-some file.ppt" w
FileClose $R0
FileOpen $R0 "$EXEDIR\boo\32-some file.ppt" w
FileClose $R0

StrCpy $INSTDIR "$EXEDIR\boo"

ClearErrors
FindFirst $0 "$1" "$INSTDIR\*.*"

${do}
FindNext $0 "$1"
${If} ${Errors}
${exitdo}
${else}
StrCpy $2 $1 "" "-3"
${if} $2 == "ppt"
StrCpy $2 $1 "2" ""
${andif} $2 != "CW"
Rename "$INSTDIR\$1" "$INSTDIR\CW$1"
${endif}
${endif}
${loop}

FindClose $0
sectionend

Are the FileOpen / FileClose commands required? I need to enter each file name into those?


Nope! these required for my example in order to create 32 files with .ppt extension and numeric value in file name to test my example ;)


Hooray! That works! Thank you very much!