Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 601
  • Last Modified:

Change file name - Batch or DOS

In a directory I have a bunch of files that have the suffix:

'_20110417_1430.csv'

I want to chomp off the last 14 characters of all the files in the directory.  So if the file name was 'testfile_20110417_1430.csv', it would change it to 'testfile.csv'.   Just do that to all the files in the directory. Prefer in DOS, if possible.
0
elwayisgod
Asked:
elwayisgod
  • 6
  • 6
  • 3
  • +2
1 Solution
 
Bill PrewCommented:
Here's a small batch file that should do the job.  The renames are ECHO right now, so the first time you run it will just display the renames it would do, so you can verify it looks right.  If so remove the ECHO before the REN and run again.  Adjust the folder name at the top to where the files exist.

@echo off
setlocal EnableDelayedExpansion
pushd "c:\yourdir"
for %%A in (*.*) do (
  set Name=%%~nA
  ECHO ren "%%~A" "!Name:~0,-14!%%~xA"
)
popd

Open in new window

~bp
0
 
Christopher McKayMicrosoft Network AdministratorCommented:
If all files are using the underscore in the same places, you could use that as a delimiter.

Try this for your command:

For /F "tokens=1,2* delims=_" %i in ('dir *.csv') do ren %i_%j_%k %i.csv
0
 
Christopher McKayMicrosoft Network AdministratorCommented:
Oh man, I'm really slow today. Need more coffee.

Bartender_1
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
elwayisgodAuthor Commented:
I run it and pipe it to log file and I get this but none of the files change.

ren "adflajfladfjaldjf_20110515_1258.csv" "adflajfladfjaldjf.csv"
ren "alijrioeutofjgb;la;suiedtojqg_20110515_1258.csv" "alijrioeutofjgb;la;suiedtojqg.csv"
ren "kasdjfglajsdfljdlfjl_20110515_1258.csv" "kasdjfglajsdfljdlfjl.csv"
0
 
Bill PrewCommented:
Correct, notice I said this earlier:

"The renames are ECHO right now, so the first time you run it will just display the renames it would do, so you can verify it looks right.  If so remove the ECHO before the REN and run again."

It does look like it's doing what you want so you should be able to remove the ECHO and proceed.

~bp
0
 
elwayisgodAuthor Commented:
jesus... I need to read :)  just woke up :)

thanks
0
 
Bill PrewCommented:
No problem, been there, done that...

~bp
0
 
elwayisgodAuthor Commented:
Maybe adding something where it only does it to files that are atleast 15 chars or more.. Just in case there are some files in the directory that should not be there etc...Can that be done?
0
 
Bill PrewCommented:
Getting the length of a string in BAT is actually fairly difficult (although doable, I have a little routine around someplace that does it...), so how about this approach.  First we use a bit more specific of a mask in the FOR to get files that match our desired pattern.  Then as a safety check we make sure the first underscore is where we expect it.

@echo on
setlocal EnableDelayedExpansion
pushd "c:\yourdir"
for %%A in (*_????????_????.*) do (
  set Name=%%~nA
  if "!Name:~-14,1!" EQU "_" (
    ECHO ren "%%~A" "!Name:~0,-14!%%~xA"
  )
)
popd

Open in new window

~bp
0
 
paultomasiCommented:
Yes, it can be done according to the length (number of characters) in the filename and/or... by 'pattern-matching.

For example, I notice the files in your question are CSV-files so there's a start. Also, I notice your filenames are in the format: abcdefghijklmnopq_12345678_1234.csv ie, ending in an uderscore, 8-digits, another underscore and 4-digits. If this is a consistent format throughout the files you want to rename then the process is very simple.

I'm sure your question will have been answered by another expert by the time I finish typing and posting this comment but if it isn't I will attempt to serve you myself.

0
 
paultomasiCommented:
I was right!
0
 
elwayisgodAuthor Commented:
Here's what I get with echo on:

C:\temp\chomp>setlocal EnableDelayedExpansion

C:\temp\chomp>pushd "c:\temp\chomp\files"

C:\temp\chomp\Files>for %A in (*_????????_????.*) do (
set Name=%~nA  
 if "!Name:~-14,1!" EQU "_" (ECHO ren "%~A" "!Name:~0,-14!%~xA" )
)

C:\temp\chomp\Files>(
set Name=andrew_20110417_1258  
 if "!Name:~-14,1!" EQU "_" (ECHO ren "andrew_20110417_1258.csv" "!Name:~0,-14!.csv" )
)
ren "andrew_20110417_1258.csv" "andrew.csv"

C:\temp\chomp\Files>(
set Name=chris_20110417_1258  
 if "!Name:~-14,1!" EQU "_" (ECHO ren "chris_20110417_1258.csv" "!Name:~0,-14!.csv" )
)
ren "chris_20110417_1258.csv" "chris.csv"

C:\temp\chomp\Files>(
set Name=sam_20110417_1258  
 if "!Name:~-14,1!" EQU "_" (ECHO ren "sam_20110417_1258.csv" "!Name:~0,-14!.csv" )
)
ren "sam_20110417_1258.csv" "sam.csv"

C:\temp\chomp\Files>popd
0
 
elwayisgodAuthor Commented:
The files in the directory are:

sam.csv
chris.csv
andrew.csv

sam_20110417_1258.csv
chris_20110417_1258.csv
andrew_20110417_1258.csv

So i think it's ignoring the correct ones......
0
 
Bill PrewCommented:
Yes, looks like it's renaming just the ones you want properly.

~bp
0
 
elwayisgodAuthor Commented:
so i remove the echo and run it doesn't rename anything.  Do I need to echo off too?
0
 
Bill PrewCommented:
Sorry, I had ECHO ON for testing here.  This should be waht you want to run it for real.  It may not display anything if all goes well, but the files should be renamed.

@echo off
setlocal EnableDelayedExpansion
pushd "c:\yourdir"
for %%A in (*_????????_????.*) do (
  set Name=%%~nA
  if "!Name:~-14,1!" EQU "_" (
    ren "%%~A" "!Name:~0,-14!%%~xA"
  )
)
popd

Open in new window

~bp
0
 
pgorodCommented:
For a non-programmatic approach, try the free Siren Renamer, very powerful and easy to use (well, after a bit of learning).

I know this is not what was requested, and that there are already valuable answers here, but anyway I felt it could interest the question author to know of different approaches.

I'm off for a cup a coffee... :-)
0
 
paultomasiCommented:
I would have thought DOS was equiped well enough to rename a file...
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 6
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now