Solved

Remove last few characters of a folder name

Posted on 2013-02-07
12
992 Views
Last Modified: 2013-02-18
I found this script on this website to remove the last few characters of a file name but I was wondering if the same could be done for a folder name:

@echo off
setlocal enabledelayedexpansion
 
for %%F in (*.docx) do (
  set fn=%%~nF
  ren  "%%F"  "!fn:~0,-4!%%~xF"
)

Can it be changed to check a folder and rename all the sub folders by removing the last 7 characters?

Thank you.
0
Comment
Question by:cbones
  • 6
  • 6
12 Comments
 
LVL 53

Expert Comment

by:Bill Prew
ID: 38865569
Yes, this would do what you wanted.  Typically folders don't have extensions, if that isn't trrue in your case let me know and we can adjust.  Change the folder name in the PUSHD to match the root folder you want to search for subfolders in.

@echo off
setlocal enabledelayedexpansion

pushd "c:\temp"

for /D %%F in (*) do (
  set fn=%%~nF
  ren "%%~F" "!fn:~0,-4!
)

Open in new window

~bp
0
 

Author Comment

by:cbones
ID: 38866242
Thank you for your response.  I have folders with foldernames like below:

folder1_folder_12345678_1234567
folder2_folder_98765432_9876543
folder3_folder_12345321_8765348

When I run the script above it returns:

folder1_folder_1234
folder2_folder_9876
folder3_folder_8765

It seems to be erasing more than 7 characters.  Any ideas?

Thank you.
0
 

Author Comment

by:cbones
ID: 38866328
BP,

If I add 'echo' to the script it shows the rename of the folders correctly.  I can change the # 7, 8, 9 or 10 characters and it shows the rename correctly.  Once I remove the 'echo' it erases more than the # of characters.

@echo off
setlocal enabledelayedexpansion

pushd "c:\temp"

for /D %%F in (*) do (
  set fn=%%~nF
echo  ren "%%~F" "!fn:~0,-4!
)
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 53

Accepted Solution

by:
Bill Prew earned 400 total points
ID: 38866609
Looks like I lost a double quote along the way, try changing:

echo  ren "%%~F" "!fn:~0,-4!

to

echo  ren "%%~F" "!fn:~0,-4!"

~bp
0
 

Author Comment

by:cbones
ID: 38867596
BP,

I tried that and when I use echo it returns the correct folder name.  Once I take the echo out, I receive this message "a duplicate file name exists, or the file cannot be found".  Right after that almost the whole file name gets erased.  

Thank you.
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 38868310
Wht is the file name causing a problem, does it have any special characters in it (non alphabetic)?

~bp
0
 

Author Comment

by:cbones
ID: 38868369
All the folders have underscores in them and commas.

Folder1_test_20130208_1234567
Folder2_test_20130208_1234987
Folder3_folder_02130208_9876543
Test,test1_02130208_1234876

Can be named like that. Different names, all have the date in them.
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 38869880
Odd, I ran the test here, and it worked as expected.  See the logging below.

C:\ee\EE28024090>dir
 Volume in drive C is System
 Volume Serial Number is 0000-0000

 Directory of C:\ee\EE28024090

02/08/2013  05:08 PM    <DIR>          .
02/08/2013  05:08 PM    <DIR>          ..
02/08/2013  05:06 PM               417 EE28024090.bat
02/08/2013  05:06 PM    <DIR>          folder1_folder_12345678_1234567
02/08/2013  05:06 PM    <DIR>          Folder1_test_20130208_1234567
02/08/2013  05:06 PM    <DIR>          folder2_folder_98765432_9876543
02/08/2013  05:06 PM    <DIR>          Folder2_test_20130208_1234987
02/08/2013  05:06 PM    <DIR>          Folder3_folder_02130208_9876543
02/08/2013  05:06 PM    <DIR>          folder3_folder_12345321_8765348
02/08/2013  05:06 PM    <DIR>          Test,test1_02130208_1234876
               1 File(s)            417 bytes
               9 Dir(s)             999 bytes free

C:\ee\EE28024090>EE28024090.bat

C:\ee\EE28024090>dir
 Volume in drive C is System
 Volume Serial Number is 0000-0000

 Directory of C:\ee\EE28024090

02/08/2013  05:09 PM    <DIR>          .
02/08/2013  05:09 PM    <DIR>          ..
02/08/2013  05:09 PM               417 EE28024090.bat
02/08/2013  05:06 PM    <DIR>          folder1_folder_12345678_123
02/08/2013  05:06 PM    <DIR>          Folder1_test_20130208_123
02/08/2013  05:06 PM    <DIR>          folder2_folder_98765432_987
02/08/2013  05:06 PM    <DIR>          Folder2_test_20130208_123
02/08/2013  05:06 PM    <DIR>          Folder3_folder_02130208_987
02/08/2013  05:06 PM    <DIR>          folder3_folder_12345321_876
02/08/2013  05:06 PM    <DIR>          Test,test1_02130208_123
               1 File(s)            417 bytes
               9 Dir(s)             999 bytes free

Open in new window

~bp
0
 

Author Comment

by:cbones
ID: 38873089
BP,

Thank you.  Sorry, yes it is working correctly.  What I didn't notice is that there can be folders like below:

FOLDER_FOLDER1_20130210_1234567
FOLDER_FOLDER1_20130210_9876543

When the script runs it does what it is suppose to and removed the last 7 characters and prompts that there is a duplicate name because once the last 7 are removed those folders would be identical.

I am not sure how to handle that.

Thank you
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 38874089
If you can come up with the logic that you want when a duplicate occurs we can try to add that to the script.  Should really have it in there since dupes can occur.  Would you want to add something to the end?  Or just flag as a problem? Etc.

~bp
0
 

Author Comment

by:cbones
ID: 38874131
What if there were duplicates could you leave 1 character, delete 6 instead of 7 characters. Or add a number 1 to it?  

Would that be difficult?

Anything would work.
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 38901960
What if there were duplicates could you leave 1 character, delete 6 instead of 7 characters. Or add a number 1 to it?  

Would that be difficult?

Anything would work.
I see you closed this question (thanks for the feedback).  If you still want help with the duplicate problem, post as another question, link it to this one, and I'll work something up.

~bp
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
.csv files 5 94
Run Internet Explorer as an administrator from CMD 20 135
VB Script/Bat: check if a file has been modified based on prefix 10 76
ROBOFTP UNZIP 1 46
When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question