[request] .bat file to sequentially rename other (drag & drop) files

nasune

Well-Known Member
OP
Member
Joined
Sep 13, 2009
Messages
749
Trophies
1
Age
36
XP
1,347
Country
Netherlands
(If this is in the wrong spot I'll unreservedly apologize, I wasn't sure where to put this)

A quick little bit of background, an old friend of mine uploaded pics of our trip way back when. I'd like to download these, but, unfortunately, he decided to upload all the pics simply named foto. Downloading them and renaming them by hand got old really quick (there are a lot of pics), so I decided to try my hand at a batch file where I could drop the pic on the file and it'd rename it sequentially (001,002,etc) only to remember that I know exactly nothing about batch files.
After spending a couple of hours putting together pieces of other scripts (and likely horrifying anyone that knows anything about cmd or batch files) the most I got working was that the batch file would take the number from an external file to use as the new filename, and adding +1 to the number and writing it back to the external file. The copying the file and renaming part would not work at all.
So what I'd like to ask is if someone could help me with a batch script that would enable me to:
-drop a pic on the batch file
-copy it to the folder the batch file is in
-have it take a number from an external .txt file
-add the necessary number of zeroes to it to make it three digits long
-rename the pic to 00X, etc
-increase the number in the external .txt file by one
-???
-profit?

I'll ad what I've pieced together (read stolen from other scripts and mashed in to some Frankenstein esque monstrosity) in the spoiler.
@Echo off
:start
cls
cd /d G:
set /p pnumber=<number.txt
set "num=%pnumber:~-3%"
echo "%~1"
copy "%1" "%CD%" /Y /S
ren "%CD%\*.jpg" "%CD%\%num%.jpg"
set /a number=%pnumber%+1
echo %number% >number.txt
exit
If anyone can help me I'd be really grateful because doing this manually is murder on my hand (the fact that I'm using a stylus rather than a traditional mouse isn't helping matters either).
 

nasune

Well-Known Member
OP
Member
Joined
Sep 13, 2009
Messages
749
Trophies
1
Age
36
XP
1,347
Country
Netherlands
Yeah, I've looked at several programs and options, but nothing does what I want it to do specifically (which is dragging a pic from my firefox browser, and then dropping it on a program/batch file which auto renames it and puts it in a folder). As such I thought a batch file would be easiest, but, as I know nothing about cmd and batch files, it doesn't quite work out.
 

kuwanger

Well-Known Member
Member
Joined
Jul 26, 2006
Messages
1,510
Trophies
0
XP
1,783
Country
United States
Not a really helpful comment, but .bat and .cmd suck. Powershell is the closest efforts MS has ever come to making a decent shell scripting language, although I find it needlessly wordy and inconsistent. :/ Writing what you want in some *nix shell like bash would be relatively trivial. Hopefully you'll find some Windows program can do the job for you.
 
  • Like
Reactions: nasune

RHOPKINS13

Geek
Member
Joined
Jan 31, 2009
Messages
1,354
Trophies
2
XP
2,620
Country
United States
There are several free renaming tools already. If you wanted to, it would be relatively trivial to write a batch script that reads the current number from a file, adds one, renames the file that was dropped on it, using that new number, and then stores the new number back in the file, where it would read it back from the next time you dragged a file on it.

But that is way overkill. Browsers will already let you download multiple files with the same name, and append a number in parenthesis at the end of it. Just download all the files, and if you MUST have the name in a specific format after you're done, use one of the many free tools that are already out there. I used to use Rename Master.
 
  • Like
Reactions: nasune

nasune

Well-Known Member
OP
Member
Joined
Sep 13, 2009
Messages
749
Trophies
1
Age
36
XP
1,347
Country
Netherlands
Thanks for the tips guys, but I've managed to puzzle it together.
@Echo off
:start
set /p pnumber=<C:\n\number.txt
set "num=%pnumber:~-4%"
echo "%~1"
xcopy /s "%1" "G:\1\"
ren "G:\1\*.JPG" "000%num%.jpg"
set /a number=%pnumber%+1
echo %number% >c:\n\number.txt
xcopy /s "G:\1\*.jpg" "G:\2\"
del "G:\1\*.jpg"
del "C:\n\*.jpg"
exit
While the code will most likely still be horrifying to anyone that knows what they're doing it does the job. With this it'll even delete the pic if I happen to drop it in the folder the batch file is located in (C:\n), rather than directly on the batch file (due to the stylus, sometimes I'll hit the edges causing it to drop inputs immediately).
Granted, there are still some limitations like the hard coded paths. This is simply because I couldn't get relative paths to work, plus it's not really a big deal to change the paths if I need to. Thanks everyone.
 

kuwanger

Well-Known Member
Member
Joined
Jul 26, 2006
Messages
1,510
Trophies
0
XP
1,783
Country
United States
While the code will most likely still be horrifying to anyone that knows what they're doing it does the job.

Yea, that's about how I expected it to look. Admittedly, I only know enough about bat/cmd voodoo to avoid it. Comparable results in bash would admittedly be not too far off, but they would make more sense (at least to me).

If curious, based on what I understand of what you're doing, something like this might work with bash although I haven't tested it:

Code:
#!/bin/bash
num="$(cat ~/number.txt)"
cp -av "$1" ~/2/000$(printf %04 "$num").jpg
echo $((num+1)) > ~/number.txt
 
  • Like
Reactions: nasune

RHOPKINS13

Geek
Member
Joined
Jan 31, 2009
Messages
1,354
Trophies
2
XP
2,620
Country
United States
While the code will most likely still be horrifying to anyone that knows what they're doing it does the job. With this it'll even delete the pic if I happen to drop it in the folder the batch file is located in (C:\n), rather than directly on the batch file (due to the stylus, sometimes I'll hit the edges causing it to drop inputs immediately).
Granted, there are still some limitations like the hard coded paths. This is simply because I couldn't get relative paths to work, plus it's not really a big deal to change the paths if I need to. Thanks everyone.

That code is horrifying. You shouldn't be using those wildcards like that, if you're dragging a file on top of the batch file it's sent as the first parameter. Basically, a lot of your * wildcards should be replaced with %1. But like I said earlier, it's still a pretty crummy way of handling this.

1) Use a real scripting language. Even bash would do, if you weren't running Windows. Heck, you could use C++ if you have the skills, or even VB.NET if you don't.
2) Make the script download the photos for you. Use curl, wget, or whatever functions your scripting language has for making HTTP requests, and grab the index file.
3) Then parse that index file, and throw all the links for them into an array.
4) Download the photos, perhaps using curl/wget again, inside of a for loop, i.e. for($i = 1; $i <= $photos.length; $i++) .
 
Last edited by RHOPKINS13,
  • Like
Reactions: nasune

nasune

Well-Known Member
OP
Member
Joined
Sep 13, 2009
Messages
749
Trophies
1
Age
36
XP
1,347
Country
Netherlands
Yeah, I keep reminding myself to learn an actual scripting language, though my only experience with that is some very basic BASIC on an old commodore a long time ago. Problem is that every time I want to sit down and do so something else always pops up causing me to put it off for another day :shy:. Still I should be able to use Linux if it becomes necessary, I have a couple of raspberry pi boards lying around after all.
Fortunately I did manage to get all the photo's, and immediately printed them out. He's sent me these exact photo's three times before (and they were lost every time), so I said fuck it, print those suckers now.

PS
Sorry for the late response, things have been hectic (I got sick, my two year old niece got sick, and my little brother (her father) got into a car accident, so yeah).
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
    K3Nv2 @ K3Nv2: https://youtube.com/shorts/WOppJ92RgGU?si=KE79L6A_3jESsGQM