Need a batch script to rename files

August 27, 2010 at 17:46:01
Specs: XP, 4 mb
Ok. This might be an easy problem, but I haven't developed a .bat file in 10 years.

I need to take 1000 files named: yyyymmdd.country.xyz.shp and yyyymmdd.country.xyz.prj and rename them to yyyymmdd_country_xyz.shp and yyyymmdd_country_xyz.prj I need to replace the first 2 periods with underscores.

Is there an easy .bat file that can be created to do this?

Thanks in advance.


See More: Need a batch script to rename files

Report •

#1
August 27, 2010 at 18:51:37
if you can use Python,

import os
root=os.path.join("C:\\","test")
os.chdir(root)
for file in os.listdir("."):
   if os.path.isfile(file) and "." in file:
      s=file.rsplit(".",1)      
      front="_".join( s[0].split("."))      
      back=s[-1]
      newfile=front+"."+back
      os.rename(file,newfile)

GNU win32 packages | Gawk


Report •

#2
August 28, 2010 at 01:37:48
Try this - untested. Remove (or REM out) the emboldened commands when you are sure the coding does what you require.

:: Code begins...
@echo off
cls
setlocal enabledelayedexpansion

pushd Path\to\files\||echo Pushd failed - job terminated&&exit /b

set ext=shp&call :process

set ext=prj&call :process

popd

exit /b


:process
set nbr=001

for /f "tokens=*" %%1 in ('dir /b *.%ext%') do (
set Filename=%%1
set NewFilename=%%~n1&set NewFilename=!NewFilename:.=_!.!ext!

echo OldFilename = %%1
echo NewFilename = !NewFilename!
echo.
pause>nul

REM ren %%1 !NewFilename!
)
:: Code ends...


Report •
Related Solutions


Ask Question