interchange filename character from batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ritesh.kumar07
Posts: 4
Joined: 26 Jun 2010 02:18

interchange filename character from batch

#1 Post by ritesh.kumar07 » 26 Jun 2010 02:30

Hi,

I am facing problem to rename file name using batch script.

Suppose in one folder I have many CSV files
e.g. PF.DL5107.ACE.ACEJR.123949.20100618.124546.csv
PF.DL6224.MNO.SCEJR.245890.20100625.127890.csv


Now I have to create batch script which will rename files which are in this folder. New file name should be as follows.

PF.ACE.ACEJR.DL5107.123949.20100618.124546.csv

PF.MNO.SCEJR .DL6224.245890.20100625.127890.csv


Please help me out.

Thanks in advance.

Regards,
Ritesh

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: interchange filename character from batch

#2 Post by aGerman » 26 Jun 2010 07:02

Try something like that

Code: Select all

@echo off &setlocal
for /f "delims=" %%a in ('dir /a-d /b *.csv') do (
  set "oldname=%%~na"
  call :proc
)
pause
goto :eof

:proc
for /f "delims=. tokens=1-7" %%a in ("%oldname%") do (
  ren "%oldname%.csv" "%%a.%%c.%%d.%%b.%%e.%%f.%%g.csv"
)
goto :eof


But be careful! The parts of the file names are rotating each time you'll run the batch. Better you move the renamed files to another folder.

Regards
aGerman

ritesh.kumar07
Posts: 4
Joined: 26 Jun 2010 02:18

Re: interchange filename character from batch

#3 Post by ritesh.kumar07 » 27 Jun 2010 23:26

Thanks a lot for your quick response.

I edited your script little bit to point correct folder. This batch script is in folder E:\ETL\ECF2\CSV_FileRename and csv files are in E:\ETL\ECF2\Report_OutPut_BK\24.06.2010 folder. When I am trying to run this script then it is not working and gives error like system can not find files. If I place batch script and csv files in same folder then it is working.

Hope you will guide me to sort out this issue as well.

for %%a in (E:\ETL\ECF2\Report_OutPut_BK\24.06.2010\*.csv) do (
set "oldname=%%~na"
call :proc
)
pause
goto :eof

:proc
for /f "delims=. tokens=1-7" %%a in ("%oldname%") do (
ren "%oldname%.csv" "%%a.%%c.%%d.%%b.%%e.%%f.%%g.csv"
)
goto :eof


Regards,
Ritesh Kumar

ritesh.kumar07
Posts: 4
Joined: 26 Jun 2010 02:18

Re: interchange filename character from batch

#4 Post by ritesh.kumar07 » 28 Jun 2010 01:20

I have incorporated your script into my script which is given below. This script replace underscore (-) to dot (.) from filename and add prefix (PF.)

After completion of this process your script comes into picture which interchange character.

Finally I move these files into date wise archive folder.

Please look into this script. Move part is not working as a part of this script (individually move code is working)and whole script works when script and csv files are in same folder.

Your precious advice will help me out from this problem.

@echo off
setlocal enabledelayedexpansion

for %%j in (E:\ETL\ECF2\Report_OutPut_BK\24.06.2010\*.csv) do (

set filename=%%~nj
set filename= PF.%%~nj
set filename=!filename:_=.!

if not "!filename!"=="%%~nj" ren "%%j" "!filename!%%~xj"

)


for %%a in (E:\ETL\ECF2\Report_OutPut_BK\24.06.2010\*.csv) do (
set "oldname=%%~na"
call :proc
)
goto :eof

:proc
for /f "delims=. tokens=1-7" %%a in ("%oldname%") do (
ren "%oldname%.csv" "%%a.%%c.%%d.%%b.%%e.%%f.%%g.csv"


)
goto :eof


Set folder=%Date:~-10,10%
Set folder=%folder:/=-%
mkdir E:\ETL\ECF2\Report_OutPut_BK\24.06.2010\Report_With_NewFileName\%folder%

if exist E:\ETL\ECF2\Report_OutPut_BK\24.06.2010\*.csv goto MOVEFILE
:MOVEFILE
move E:\ETL\ECF2\Report_OutPut_BK\24.06.2010\*.csv E:\ETL\ECF2\Report_OutPut_BK\24.06.2010\Report_With_NewFileName\%folder%

if not exist E:\ETL\ECF2\Report_OutPut_BK\24.06.2010\*.csv goto END
:END

Thanks & regards,
Ritesh Kumar

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: interchange filename character from batch

#5 Post by aGerman » 28 Jun 2010 10:25

Untested:

Code: Select all

@echo off &setlocal

pushd "E:\ETL\ECF2\Report_OutPut_BK\24.06.2010"

Set "folder=%Date:~-10,10%"
Set "folder=E:\ETL\ECF2\Report_OutPut_BK\24.06.2010\Report_With_NewFileName\%folder:/=-%"
mkdir "%folder%"

for %%a in (*.csv) do (
  set "oldname=%%~na"
  call :proc
)

popd
goto :eof

:proc
for /f "delims=._ tokens=1-6" %%a in ("%oldname%") do (
  move "%oldname%.csv" "%folder%\PF.%%b.%%c.%%a.%%d.%%e.%%f.csv"
)
goto :eof



Regards
aGerman

ritesh.kumar07
Posts: 4
Joined: 26 Jun 2010 02:18

Re: interchange filename character from batch

#6 Post by ritesh.kumar07 » 29 Jun 2010 01:10

Thanks a lot. Script is working fine.

Regards,
Ritesh Kumar

Post Reply