Page 1 of 1
Help on a simple script to rename folders
Posted: March 31st, 2012, 8:18 am
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?
Re: Help on a simple script to rename folders
Posted: March 31st, 2012, 8:45 am
by shypike
A good start would be to tell on which operating system you want to run it.
Re: Help on a simple script to rename folders
Posted: March 31st, 2012, 10:24 am
by Bozzy
Yes, it would!
Im on Windows 7 x64
Re: Help on a simple script to rename folders
Posted: April 1st, 2012, 4:53 am
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.
Re: Help on a simple script to rename folders
Posted: April 1st, 2012, 3:37 pm
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%"
Re: Help on a simple script to rename folders
Posted: April 2nd, 2012, 8:13 am
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"
Re: Help on a simple script to rename folders
Posted: April 16th, 2012, 3:45 pm
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%
Re: Help on a simple script to rename folders
Posted: February 16th, 2013, 2:49 am
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?
Re: Help on a simple script to rename folders
Posted: February 16th, 2013, 4:59 am
by shypike
On WIndows, disable folder renaming in Config->Switches.
Re: Help on a simple script to rename folders
Posted: March 1st, 2013, 7:10 pm
by Bozzy
Unfortunately it already is Shypike.....
Re: Help on a simple script to rename folders
Posted: March 3rd, 2013, 4:21 pm
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.