Super Simple Script to remove first characters of any png file

Well I searched the net with varying success, but it seems kinda hard to find a one/max 2 lined command to:

strip all *.png files in the folder from their first two characters.

Any help is appreciated.

In DOS commandline of course...

Strip the first 2 bytes from every png file, do you mean?

If you can use busybox win32, I'd try this in a busybox shell:

for FILE in *.png ; do ( dd bs=2 count=1 > JUNK ; cat ) < $FILE > new-$FILE ; echo mv new-$FILE $FILE ; done
rm -f JUNK

Remove the echo from that once you've tested it and are sure it does what you want.

And it'd make much more sense to look at if you didn't insist on jamming it into 2 lines.

1 Like

Are you talking of file names or file contents to strip?

Just the filenames.

With a ksh variable, you could do the following:-

#!/bin/ksh

for file in `ls *.png`
do
   echo mv $file "${file#??}"
done

Is this what you're after, or have I missed the point?

This will output the commands so you can check.

I hope that this helps,
Robin
Liverpool/Blackburn
UK

Oh.

Well that's simpler, I guess. :stuck_out_tongue:

More code for busybox bash:

for X in *.png ; do echo mv "$X" "${X:2}" ; done

Remove the 'echo' once you've tested and are sure it does what you want.

That is a rare and interesting combination useless use of backticks and useless use of ls *. The shell does not need the help of ls or backticks to use *.

I doubt the OP has KSH, but busybox for windows has sh as noted.

Yes. I agree. I don't know what I was thinking!

Apologies.

Robin

well. I'd love to do this without anything added besides the general DOS Shell.

I'd probably even cope with commands longer than 2 lines if needed.

Step 1: Install DOS. (You don't get that anymore, just Windows CMD.)

If CMD will do:

for %a in (*.png) DO ( set V=%a
echo rename %%V%% %%V:~2%% )

You can't do it in one statement because %a is not a 'real' variable, windows for is a strange beast.

Remove the echo once you've tested and are sure it does what you want.

Windows CMD is inconsistent here in that more %'s are required when you put this in a batch file than when you run it in a shell window; cmd's 'for' is a very strange beast. Some experimentation may be required.

dd's skip obviates both cat and the subshell. Therefore, it is with a heavy heart that I must award you this useless use of cat award :wink:

Regards,
Alister

Doesn't seem to work... neither directly in CMD nor in a bat file.

Like I said I want to achieve this without installing anything else...

To repeat:

I tried to make it so it would work in a batch file, but didn't get the right combination I guess. This exact code works when typed into shell:

for %a in (*.txt) DO (
        set V=%a
        echo rename %V% %V:~2%
)

Nope that doesn't work in DOS either.

I put a test.png in a folder.

1st try:
paste script in batch, execute -> Fail, png stays unrenamed

2nd try:
execute said script from CMD: -> Fail, output: "a cannot be syntactically processed here."

3rd try:
execute by pasting into command line -> see 1

Honestly I would be happy with ONLY ONE approach that will work while executed form a batch script...

You don't have DOS, you have CMD.

You really need to pay attention.

1) I already told you, syntax for batch file and prompt are slightly different. I give you code that works in prompt, you paste it in batch?

2) The command, as is, is echo rename. What exactly do you think that's going to do?

Check that it prints the right names, first. Then remove the echo, and it will run 'rename' instead of 'echo rename'.

I guarantee that the code as I gave it works in Windows CMD here.

See how it strips the first two letters from the names?

I'm using Windows XP. If you have that or newer, should support the same syntax.

And a version which works in batch:

for %%A in (*.txt) DO (
        set V=%%A
        echo rename %V% %V:~2%
        )

It needs %% instead of % because:

Well it wasn't paying attention on the cmd basis, cause I tried it three different ways.

HOWEVER the echo removed actually works !.... in cmd :slight_smile:

for %%A in (*.txt) DO (
        set V=%%a
        echo rename %V% %V:~2%
        )

nope, not working in a batch file

Perhaps now you understand my reluctance to use Windows CMD loops. echo rename works when rename doesn't because of inane inconsistencies in Windows' CMD parser.

Also, the variable set in the batch file is persistent, so if you mess it up one execution, you might still think it's working the next even when it isn't (because you messed up the syntax of set and it silently failed)

Oh, and sometimes it does the trick of setting the variable outside the loop but not inside it. Which makes interesting side-effects given the above.

The main problem is the difference between the special %a/%%a variable for generates, and the more normal environment variables required to do string operations. They don't cooperate, not being set properly inside the loop somehow. And all the special operations for for's special variables have been replaced with ones related to expanding file paths.