Solved

Truncate folder name

Posted on 2013-02-05
6
599 Views
Last Modified: 2013-02-06
Hello,

I am looking for a script that can truncate folders in a directory.  Each folder in the main directory has three underscores (_) in the file name.  I need to have a script that will remove the third underscore and everything after it.

Can this be done with a batch file?

Thank you.
0
Comment
Question by:cbones
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38857614
If there aren't any other underscores in the filenames you can probably do it with this... it will look for all dirs with with three underscores in then pass the bit before the first _ into %%a and the bit after the underscores into %%b

Steve

@echo off
cd /d "c:\root directory"
for /f "tokens=1* delims=_" %%a in ('dir /b /ad *___*') do echo rename "%%a___%%b" "%%a__"

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38857622
That will SHOW you the commands it will run on the screen.  If they look OK remove the word echo before the rename command.

If there is the possibility of other _ characters in the foldername will have to be done in different way.

Steve
0
 

Author Comment

by:cbones
ID: 38857674
Steve,

Thank you for your response.  When I run this it removes two underscore only if the three are in the folder name together.  

I can have a file name like this:  file_test_one_temp_0212312

What I am looking to do is to remove everything after the third underscore.

Thank you.
0
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 
LVL 43

Accepted Solution

by:
Steve Knight earned 175 total points
ID: 38857736
Ahh OK then if there are always three or more underscores and you want a maximum of two then try this:

@echo off
cd /d "c:\root directory"
for /f "tokens=1,2,3* delims=_" %%a in ('dir /b /ad *_*_*_*') do echo rename "%%a_%%b_%%c_%%d" "%%a_%%b_%%c"

Open in new window


Looks for the pattern *_*_*_* and splits the filename at the _ and renames to the first three parts.

In your example "file_test_one_temp_0212312" becomes "file_test_one"

Steve
0
 
LVL 55

Assisted Solution

by:Bill Prew
Bill Prew earned 175 total points
ID: 38857753
This should do what you are looking for.  Adjust the BaseDir name as needed.  Currently it wont rename the folders, just show the command it would execute. This will allow you to test and verify, if it looks good remove the ECHO from the REN line.

@echo off
setlocal

set BaseDir=c:\ee\EE28021328\basedir

for /D %%D in ("%BaseDir%\*_*_*_*") do (
  for /F "tokens=1-3 delims=_" %%A in ("%%~nD") do (
    ECHO ren "%%~D" "%%~A_%%B_%%C"
  )
)

Open in new window

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38859800
Thanks.  Should have done it like Bill did really anyway, only reason I didn't is because I thought you meant three underscores in a row.
Steve
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

695 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