2

I have done some research and not found very much and what I have found I could not get to work. Here is one thing that I have found here on stackoverflow. cmd - batch file rename - Stack Overflow

I use a big cannon copier to scan files into a folder on a network drive. I name the file before it is scanned and the copier puts a timestamp on the end of the file which looks like "_20141021165008" and I would like to remove the timestamp when the file is sent to the computer. The files are scanned to one of three locations and I would like to have each of them do the same thing.

This is what it looks like coming from the copier:

001-Bob_(1-100)_20141021165008
002-Bob_(1-25)_20141021170515
1-67676_Ex.Sheets_20141021172043

and this is what I would like them to look like:

001-Bob_(1-100)
002-Bob_(1-25)
1-67676_Ex.Sheets

Also, the names and numbers change all of the time. I have it so that file extensions are not visible. But the files are 99.9% of the time .PDF

What I would like to do is:

  • Have timestamp automatically removed when the files arrive in the folder.
  • not rename a file more than once so that I don't lose any of the file name.

I have a batch file that removes the last 12 characters of the file name and I created a shortcut to it and use a keyboard shortcut but it doesn't work on everything, isn't fully automated, and I have to reset the shortcut every few days because it just stops working...

Running Win7 Pro 64x

Thank you to anyone that helps!

Community
  • 1
  • 1
DomCan
  • 37
  • 3
  • 8

3 Answers3

2

Almost, but not quite what you asked for. And the command can be run as many times as you want without risk of changing the name of an already renamed file.

ren *_*.pdf *_.pdf

The above command will give the following results with the files you listed in your question:

001-Bob_(1-100)_20141021165008.pdf    -->  001-Bob_(1-100)_.pdf
002-Bob_(1-25)_20141021170515.pdf     -->  002-Bob_(1-25)_.pdf
1-67676_Ex.Sheets_20141021172043.pdf  -->  1-67676_Ex.Sheets_.pdf

See How does the Windows RENAME command interpret wildcards? for an explanation of why the above command works.

Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
0
for /f "tokens=1-3* delims=_" %A in ('dir /b *_*_*.*') do ren "%A_%B_%C%D" "%A_%B%D"

This reads the output of the dir command. It won't work on files with more than two underscores. If you need that just say so. In a batch file change %A etc to %%A.

0
@echo off

setlocal
set "target_folder=c:\some_dir"
pushd "%target_folder%" 2>nul

rem  --Remove comments in case of network drive.
rem net use h: %target_folder%
rem pushd h:

setlocal enableDelayedExpansion
for %%a in (*_2014*) do (
    set filename=%%~na
    set newname=!filename:~0,-14!
    rem --Remove the echo if the new file name is OK
    echo ren "%%~nxa" "!newname!%%~xa"

)
endlocal
endlocal
npocmaka
  • 55,367
  • 18
  • 148
  • 187