Help on a simple script to rename folders

Get help with all aspects of SABnzbd
Forum rules
Help us help you:
  • Are you using the latest stable version of SABnzbd? Downloads page.
  • Tell us what system you run SABnzbd on.
  • Adhere to the forum rules.
  • Do you experience problems during downloading?
    Check your connection in Status and Interface settings window.
    Use Test Server in Config > Servers.
    We will probably ask you to do a test using only basic settings.
  • Do you experience problems during repair or unpacking?
    Enable +Debug logging in the Status and Interface settings window and share the relevant parts of the log here using [ code ] sections.
Post Reply
Bozzy
Newbie
Newbie
Posts: 10
Joined: March 31st, 2012, 7:57 am

Help on a simple script to rename folders

Post by Bozzy »

Hi all,

I would like to run a simple scrip that renames the folder that's automatically extracted to have the date in front of it, ie, to go from

'blah blah extrated folder' ---> ''2012-03-31 - blah blah extrated folder'

Using the date format of YYYY-MM-DD

Could anyone help me on where to start?
User avatar
shypike
Administrator
Administrator
Posts: 19773
Joined: January 18th, 2008, 12:49 pm

Re: Help on a simple script to rename folders

Post by shypike »

A good start would be to tell on which operating system you want to run it.
Bozzy
Newbie
Newbie
Posts: 10
Joined: March 31st, 2012, 7:57 am

Re: Help on a simple script to rename folders

Post by Bozzy »

Yes, it would!

Im on Windows 7 x64
User avatar
shypike
Administrator
Administrator
Posts: 19773
Joined: January 18th, 2008, 12:49 pm

Re: Help on a simple script to rename folders

Post by shypike »

Code: Select all

@echo off
cd %1
set name=%3
set name=%name:"=%
set stamp=%date:~3%
set dd=%stamp:~,2%
set mm=%stamp:~3,2%
set yy=%stamp:~8,2%

ren "%name%" "20%yy%-%mm%-%dd% - %name%"
With the assumption that the %date% variable gives dd-mm-yyyy
If not, then you must change the order in the last line.
Bozzy
Newbie
Newbie
Posts: 10
Joined: March 31st, 2012, 7:57 am

Re: Help on a simple script to rename folders

Post by Bozzy »

Many thanks for that, I didn't think it was as simple as an MSDOS bat file!
You set me on a path to remember my old programming days, so I had ago myself too......this was my attempt, both work well.

Code: Select all

@echo off
cd "%1"
set name="%3"
@For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @( 
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set All=%%C-%%B-%%A
)
ren "%1" "%All% - %name%"
User avatar
shypike
Administrator
Administrator
Posts: 19773
Joined: January 18th, 2008, 12:49 pm

Re: Help on a simple script to rename folders

Post by shypike »

I think you need these two lines:
set name=%3
set name=%name:"=%
They remove the surrounding quotes (") from the foldername, otherwise you will get commands like this:
ren "bla" "2012-04-02 - "bla"
Bozzy
Newbie
Newbie
Posts: 10
Joined: March 31st, 2012, 7:57 am

Re: Help on a simple script to rename folders

Post by Bozzy »

Cheers again for the help.

Ended up changing it a bit and taking your advice :)

Code: Select all

@echo off
cd %1
set name=%3
set name=%name:"=%

echo %DATE% %TIME%
for /F "tokens=1-3 delims=/" %%a in ("%DATE%") do set DAY=%%a& set MTH=%%b& set YR=%%c
for /F "tokens=1-3 delims=:." %%a in ("%TIME%") do set HR=%%a& set MIN=%%b& set SEC=%%c
if "%HR:~0,1%"==" " set HR=0%HR:~1,1% 
set MYDATE=%YR%%MTH%%DAY%-%HR%%MIN%%SEC%
set NEWNAME=%MYDATE%_%3

ren %1 %NEWNAME%
Bozzy
Newbie
Newbie
Posts: 10
Joined: March 31st, 2012, 7:57 am

Re: Help on a simple script to rename folders

Post by Bozzy »

Well.......

I never quite got this to work, so I may as well bump it.

It works on some folders, but not on others, and there doesn't seem to be any pattern as to why (ie similar non alpha-numeric characters)

I'm thinking maybe windows may be locking the folder for thumbnail generation or something similar. I know it's a bit vague, but does anyone have any advice?
User avatar
shypike
Administrator
Administrator
Posts: 19773
Joined: January 18th, 2008, 12:49 pm

Re: Help on a simple script to rename folders

Post by shypike »

On WIndows, disable folder renaming in Config->Switches.
Bozzy
Newbie
Newbie
Posts: 10
Joined: March 31st, 2012, 7:57 am

Re: Help on a simple script to rename folders

Post by Bozzy »

Unfortunately it already is Shypike.....
Bozzy
Newbie
Newbie
Posts: 10
Joined: March 31st, 2012, 7:57 am

Re: Help on a simple script to rename folders

Post by Bozzy »

I was running the code with echo on to try and see what was wrong and found it was adding a space instead of a 0 when it was midnight...also added a ping in to create a 2 second pause in case there was permission locking.

Code: Select all

cd "%1"
set name="%3"
set name=%name:"=%

echo %DATE% %TIME%
for /F "tokens=1-3 delims=/" %%a in ("%DATE%") do set DAY=%%a& set MTH=%%b& set YR=%%c
for /F "tokens=1-3 delims=:." %%a in ("%TIME%") do set HR=%%a& set MIN=%%b& set SEC=%%c
IF "%HR:~0,1%" == " " SET HR=0%HR:~1,1%
set MYDATE=%YR%%MTH%%DAY%-%HR%%MIN%%SEC%
set NEWNAME=%MYDATE%_%3

ping 127.0.0.1 -n 2

rename %1 %NEWNAME%
This should work now it's tweaked.
Post Reply