Renaming Files With A Gpe Or Sh Script


lowtech

Still Fresh
Joined
Jun 6, 2009
Messages
22
hello everyone!

here's my pickle...I would like to change the file names of a group of video files in my movie folder, on SD card, with a script file (hopefully a gpe file) can i do this? with the 'mv' or 'rename' commands? if so, could anyone provide an example script that works?

Simply put, I'm hoping for one script that would for example change 'test.avi' to 'test,' without file extension and then a second script to change it back.

I've tried this with gpe files and termula. odlly, when running a .sh script, with the command 'mv', I get the error "invalid argument" in termula. from other posts, I realized that I should use a text editor like notepad++, which I am now using. however, I get the same error in termula. BUT, when I manually use the mv command in termula, changing filenames works.

I'm especially hoping I can do this with a gpe script.

any input would be appreciated

:D
 
I assume you want to rename all avi files? If so

CODE
for file in *.avi
do
mv "$file" "`basename "$file" .avi`"
done


to get back

CODE
for file in *
do
mv "$file" "$file".avi
done


but you might be better to change the extension to something that's not .avi rather than removing it as the second script renames all files.
 
yes, I could rename all files. Defiantly makes it simpler. I tried your code as a gpe file, as the following:

CODE
#!/bin/sh

for file in *.avi
do
mv "$file" "`basename "$file" .sub`"
done


(after taking your advice to have it change the file extension instead of removing it) Unfortunately, when running this gpe file from the LAUNCHER menu, it sits at the "loading" screen. I have to turn off then on again and no changes were made.

I then changed my sub2.gpe file to sub2.sh and executed from inside termula, I get the error syntax error..expected word 'do'

awaiting your input, I'm going to try this script with everything on one line, also, should I call the wiz's menu at the end of the script like I've seen in the gpe script that comes with the quake2 port?
 
Simply put, I'm hoping for one script that would for example change 'test.avi' to 'test,' without file extension and then a second script to change it back.
Sounds a lot like someone's trying to hide a video file.

I wonder what it could be. =X
 
Last edited by a moderator:
I haven't got a wiz so I can't try it, but I'll try it on the gp2x when I get a chance

Yes, you should call the menu an the end, I think that's why it sticks at the loading screen

The change to .sub is wrong, the second parameter on basename is the suffix to remove.

To change it to .sub it should be
CODE
mv "$file" "`basename "$file" .avi`.sub"


In termula run the script with sh -vx sub2.sh that will show how the script is expanded and executed.
 
thanks Parkydr,

its obvious that I'm a beginner at shell scripting, made an exploratory change to your code...will try your new example asap. I'm extremely appreciative that you're taking an interest in my pickle (predicament).

In termula, your code examples give "expected 'do'" errors. I read that removing spaces would help, and it did (I moved 'do' to the end of the beginning line, separated with a semicolon (or maybe without *scratches head*) and the error went away, but a new error saying that it didn't know what the "done" word was for? Although, it did show that it was finding the files in the directory; good thing. but complained that it couldn't change their names. again, just typing the mv command to change specific files works. going to try your new code now, will update with results...

b1ueskycomp1ex gets a Nobel prize and should change his user name to sherlock Holmes. His deductive skills are simply inhuman. (just teasing)

EDIT: while its still fresh in my mind, running your script as follows:

CODE
#!/bin/sh

for file in *.avi;do mv "$file" "`basename "$file" .avi`.sub"
done

produces errors: "cannot rename kungfupanda.avi : invalid argument." the script does cycle through all avi's in the folder and when using the "sh -vx" you mentioned, it shows this "+ mv kungfupanda.avi kungfupanda.sub : invalid argument" the same for each other file as well. However, right afterward, I manually type in "mv kungfupanda.avi kungfupanda.sub" and that works.

Why do you suppose it won't work from within a script? / would quotes help around file names? if so, how would we insert them?
 
SOLUTION ... well, 90 percent solution hehe, slight modification of your code yields satisfactory results...

CODE
#!/bin/sh

for file in *.sub; do mv "$file" "`basename "$file" .sub`.avi"; done;

notice the use of semi colons? somehow, this makes this piece of...code...work. Interestingly though, termula gives the error " : not found"...perhaps it says that once it runs out of avi files during it's search?

If I can get this to work in a gpe file, I'll be set. (little question though, why are the codeboxes in your posts smaller than mine?)
 
Using semicolons just allow you to put the commands on one line, it should make no difference but I think I know what's going on.

What editor are you using? Windows notepad?

I wonder if you've got <cr><lf> on the end of your lines. Linux uses just <lf>

If you try vi in termula, do you see lots of ^M characters at the end of the lines?

PS I don't know why the code box is smaller.

Edit: should have read the first post - you're using notepad++ but I bet you have got ^M in there, that would explain the errors
 
I do say my dear boy, you underestimate my intellectual and very complex crime deducing skills! For you see, I am but a wizard under the guise of genius detective! *poof*
 
Last edited by a moderator:
Parkydr, I'm inclined to accept your assumption and thanks for the help. I'm now using the scripts as sh files in termula. Although, I seem to be not using the CD command correctly in the script files. I need to cd up two folders, then cd into the movie folder.

I tried "CD .." and "CD ../.." and they don't work from within the script, but are fine when manually typing it in. (I know you said that you don't have a wiz, but I'm wondering, shouldn't the absolute paths in termula be the same as those one would use within as script being run within termula? I try to use an absolute path and it doesn't work from within the sh file.)

Also, I have yet to get any script working as a gpe file. My feeling is that by default, the linux on the wiz doesn't allow you to execute potentially dangerous code? (as in, having to be logged in as root or using a sudo like command)

For now, I'm happy with just using termula; My pickle (predicament) is a little tastier. But if anyone can shed light as to why a gpe script can't rename files or how it can be modified so that it can, I'm all ears.


----
QUOTE
I do say my dear boy, you underestimate my intellectual and very complex crime deducing skills! For you see, I am but a wizard under the guise of genius detective! *poof*


140px-Estatua_de_Sherlock_Homes_en_Londres.jpg
A wizard? And not a gifted detective? that would take away all his awesomeness.
 
I would expect your script to be running as root, I doubt the wiz will have other users set up.

Try pwd in termula I expect the current directory is where termula runs and cd ../.. gets you back to the root of your SD card.

From the menu, I'd expect the current directory to be where the script lives.

cd should work, but not CD, this isn't windows - commands are case sensitive.
 
Lemme guess, you are not 18 yet. One of your parents uses the Wiz too, and you dont want to see you have a vid on your Wiz called: Girl sucks her dad's p*n*s and you dont want to manually change the name everytime?
 
I would expect your script to be running as root, I doubt the wiz will have other users set up.
I appreciate the input and I agree with your reasoning, I figured the same thing, but I'm hoping someone who actually knows how the wiz handles bash scripts could shed light on this. I tried an empty script that just calls the wiz's menu, copied from the gpe script from the sdlquake2 port, without success. It just hangs at "loading". (I might not have copied it well, I tried this before I started using notepad++, will try again)

and cd ../.. gets you back to the root of your SD card.
I know what it does, and it is my intention to get to the root of the SD card, so that I can then cd down into the movie folder (within the script). (Looking back on this, I realize that it should be easier to just cd directly into an absolute path? Although, its probably novice of me to ask but, shouldn't the paths shown in termula be exactly the same as the paths the wiz uses to refer to sd, nand and etc?)

but thanks for the help anyway! :D


I read in another post where when getting a certain script to work, they said to change #!/bin/bash to #!/bin/sh. Although, mine use #!/bin/sh. So maybe I should try #!/bin/bash? -- lol I'm such a noob it's disgusting. but whatcha gonna do?

Lemme guess, you are not 18 yet. One of your parents uses the Wiz too, and you dont want to see you have a vid on your Wiz called: Girl sucks her dad's p*n*s and you dont want to manually change the name everytime?
You have a twisted view of adult entertainment. And no, I'm not under 18, but sometimes kids or friends might use my wiz, and the girlfriend would shave my beard with a chainsaw, so a little privacy is a good thing. And yes, manually changing the name every time is tedious and slow compared to just typing "sh avi.sh" in termula. And this way allows me to do this on the wiz itself, without the need of a computer. I love Linux.
 
Last edited by a moderator:
Back
Top