We help IT Professionals succeed at work.

DOS batch : rename files in different directories

Medium Priority
388 Views
Last Modified: 2012-06-27
Hi I have to rename couple of files.  Not they are in this format

C:\dirA\prefix1-logs\logs.txt
C:\dirA\prefix2-logs\logs.txt
C:\dirB\prefix3-logs\logs.txt
C:\dirB\prefix4-logs\logs.txt

I want to move all these files to
C:\dirX\prefix1.txt
C:\dirX\prefix2.txt
C:\dirX\prefix3.txt
C:\dirX\prefix4.txt
C:\dirX\prefix5.txt

That is I want to move all the logs.txt files from different directories and rename them according to their prefix.  So, that I have all the logs.txt in one directory.  
Comment
Watch Question

Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
How many files are we talking about here?  Could we some actual example paths please in case there are any pit falls there.

if there are indeed only four and the path may be different like this you could either write a static batch file such as@

@Echo off
move C:\dira\prefix1-logs\log.txt C:\dirX\prefix1.txt

etc.

or
@echo off
set new=c:\dirx
move c:\dira\prefix1-logs\log.txt %new%\prefix1.txt

or we could read a list of paths from a text file, e.g. save this as "theabovetextfile.txt"

C:\dira,prefix1
c:\dira,prefix2
c:\dirb,prefix3
C:\dirb,prefix4

and then run this .cmd file against it.  If it looks OK remove the "echo" before the MOVE command to make it actually do the job.

@Echo off
REM Define the destination dir.
set newdir=c:\dirx

REM Make sure that it exists
mkdir "%newdir%" 2>NUL

REM Work down each line in the text file to see which logs to move.  The comma seperated fields on
REM each line are set into %%a and %%b for each line and a MOVE carried out.
REM If the file does not exist and error message is shown.
for /f "tokens=1,2 delims=," %%a in ('theabovetextfile.txt') do (
 echo Working on %%a\%%b-logs\logs.txt moving to %newdir%\%%b.txt
 if exist "%%a\%%b-logs\logs.txt" (
   echo move "%%a\%%b-logs\logs.txt" "%newdir%\%%b.txt"
) ELSE (
   echo ERROR -- No log found at "%%a\%%b-logs\logs.txt"
)
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Okay, give this a try:

@echo off
setlocal EnableDelayedExpansion

call :DoMove "C:\DirA" "C:\DirX"
call :DoMove "C:\DirB" "C:\DirX"
exit /b

:DoMove
  for /D %%D in ("%~1\*") do (
    for %%F in ("%%~D\logs.txt*") do (
      for /F "tokens=1 delims=-" %%N in ("%%~nD") do (
        move "%%~F" "%~2\%%N.txt"
      )
    )
  )
  exit /b

Open in new window

~bp
this will do as follow.

1. scan drive C:\ (no subflders) for all the folders starting with DIR, like DIR A , DIR B etc..
2. go into each of those folders,and then the subfolder starting with prefix.
3. Move the files from those folders to C:\DIRX\foldername.txt


:action
@echo off & setlocal enabledelayedexpansion
for /f %%a in ('dir /b dir*') do (
cd %%a
echo current directory = !cd!
for /f "tokens=1,2 delims=-" %%f in ('dir /b') do (
cd %%f-%%g
md C:\DirX
echo move logs.txt C:\DirX\%%f.txt
cd..
pause

)

popd

)

Open in new window


Hope one of us helps ..
i think above had a problem, it uses a pause..annoying.. updated one. Also one time instead of cd..i typed popd.
try this ..

:action
@echo off & setlocal enabledelayedexpansion
md C:\DirX
for /f %%a in ('dir /b dir*') do (
cd %%a
echo current directory = !cd!
for /f "tokens=1,2 delims=-" %%f in ('dir /b') do (
cd %%f-%%g
echo move logs.txt C:\DirX\%%f.txt
cd..

)

cd..

)

Open in new window

Author

Commented:
Thanks a lot guys.  I am going away for a while.  Will come back and try it out.  Thanks again.
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
OK, you can see we have all had to make some assumptions about what your real requirements are regarding how many dirs, what the real directory names are etc. -- i.e. whether to read the directory structure, or a pre-determined list in a text file like I did etc. so when you look at them just clarify those details for us and it will help please.

Late now, will follow up in the morning if anything to add.

thanks

Steve
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
^^^ +1

~bp

Author

Commented:
subhashchy:  What if I need to specify a sub folder than C:

Thanks.
as source of the files or as the target folder ?

Author

Commented:
So, basically I will have.  

C:\someDIr\dirA\prefix1-logs\logs.txt
C:\someDIr\dirA\prefix2-logs\logs.txt
C:\someDIr\dirB\prefix3-logs\logs.txt
C:\someDIr\dirB\prefix4-logs\logs.txt

and I wanted to move them to C:\someDIr\dirX
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
The final should be :

C:\someDIr\dirX\prefix1.txt
C:\someDIr\dirX\prefix2.txt
C:\someDIr\dirX\prefix3.txt
C:\someDIr\dirX\prefix4.txt

Hope I am not making you guys confused.  I have to work on some log files that I download and they come in separate zip files. That is why I had to do this.
Thanks.
i think above code should work..did you tried that ?

Author

Commented:
I did try, but It is not working, sorry!

I can see the right command being executed.

log.txt C:\Temp\moving\DirX\20110425211618662 .txt
But, I the file is not moved into dirX directory.
If I type the same command from the console, the file is moved just fine.

Author

Commented:
Sorry, I might have some problem. hold on I will get back to you.
yes..that was just to make sure we do not mess up anything without knowing what will happen..:).

Remove the "echo" before the MOVE command to make it actually do the job in line nuber 8..

echo move logs.txt C:\somedir\DirX\%%f.txt  (Remove echo from this line, rest will be same.)..




Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Have you tried my code?  Worked here...

~bp

Author

Commented:
I get syntax of the command is incorrect.
move Log.txt C:\Temp\moving\DirX\%%f.txt

Author

Commented:
Hi billprew: I will try that in a while Thanks.
it seems to be working on my system..can you paste the actual batch you useed ?

Author

Commented:
:action
@echo off & setlocal enabledelayedexpansion
md C:\Temp\moving\DirX
for /f %%a in ('dir /b ') do (
cd %%a
echo current directory = !cd!
for /f "tokens=1,2 delims=-" %%f in ('dir /b') do (
cd %%f-%%g
echo current directory = !cd!
move Log.txt C:\Temp\moving\DirX\%%f.txt
cd..
)
cd ..

)

Author

Commented:

:action
@echo off & setlocal enabledelayedexpansion
md C:\Temp\moving\DirX
for /f %%a in ('dir /b ') do (
cd %%a
echo current directory = !cd!
for /f "tokens=1,2 delims=-" %%f in ('dir /b') do (
cd %%f-%%g
echo current directory = !cd! 
move Log.txt C:\Temp\moving\DirX\%%f.txt
cd..
)
cd ..

)

Open in new window

@Bill. I m thinking why its not working .. Can you suggest what could be wrong here..

other than line #4. He used dir /b instead of :dir /b dir*"
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Looking...

~bp

Author

Commented:
The following is what I get when I run.   Error in line 2 is fine as I have the directory.  But, why am I getting the syntax error?

C:\Temp\moving>traverse.bat
A subdirectory or file C:\Temp\moving\DirX already exists.
current directory = C:\Temp\moving\browser
current directory = C:\Temp\moving\browser\20110425160318045 - Reset
The syntax of the command is incorrect.
current directory = C:\Temp\moving\browser\20110425211618662 - Reset
The syntax of the command is incorrect.
current directory = C:\Temp\moving\DirX
The directory name is invalid.
current directory = C:\Temp\moving
The system cannot find the path specified.
current directory = C:\Temp\moving
The system cannot find the file specified.
The system cannot find the path specified.
current directory = C:\Temp
The system cannot find the file specified.
The system cannot find the path specified.
current directory = C:\
The system cannot find the file specified.

Open in new window

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
@ambuli

So, in your test, are the DIRA and DIRB folders in C:\TEMP?

And if so, how are they to be seperated from the other subfolders in there, is hardcoding the names the only way?

~bp
i think you have space in the folder name "20110425211618662 - Reset" which you didn't mentioned earlier.

and there are files as well in the folder where all the folders are..

I would say let us know what else we are missing ..
Ambuli,

Try this:

in the folder of DirA and DirB create a new batch file called run-1.bat with the following line:
for /f %%a IN ('dir /ad /b') do call run-2.bat %%a

in the same folder create another new batch file called run-2.bat with the following line:
for /f %%a IN ('dir .\%1 /ad /b') do copy .\%1\%%a\logs.txt c:\dirX\%%a.txt

save both batch files and execute run-1.bat

let me know if this works.

Lester...
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
I have to start by saying that my original script posted should work fine, disappointed it isn't being tried.

That being said, here's my suggested improvements to the @subhashchy script, hope this helps.

@echo off
setlocal enabledelayedexpansion
if not exist C:\Temp\moving\DirX md C:\Temp\moving\DirX
for /f %%a in ('dir /b /ad dir?') do (
  if /I "%%a" NEQ "dirx" (
    pushd %%a
    echo current directory = !cd!
    for /f "tokens=1,2 delims=-" %%f in ('dir /b /ad') do (
      pushd %%f-%%g
      echo current directory = !cd! 
      move Logs.txt C:\Temp\moving\DirX\%%f.txt
      popd
    )
    popd
  )
)

Open in new window

~bp

Author

Commented:
subhashchy:

Sorry, I did not realize the space in the name before.  Here is the contents of the dir.

:\Temp\moving>dir /s
Volume in drive C has no label.
Volume Serial Number is BCE3-35BE

Directory of C:\Temp\moving

4/26/2011  08:58 PM    <DIR>          .
4/26/2011  08:58 PM    <DIR>          ..
4/26/2011  08:57 PM    <DIR>          browser
4/26/2011  09:33 PM    <DIR>          DirX
4/26/2011  09:31 PM               292 traverse.bat
              1 File(s)            292 bytes

Directory of C:\Temp\moving\browser

4/26/2011  08:57 PM    <DIR>          .
4/26/2011  08:57 PM    <DIR>          ..
4/26/2011  10:05 PM    <DIR>          20110425160318045 - Reset
4/26/2011  10:05 PM    <DIR>          20110425211618662 - Reset
              0 File(s)              0 bytes

Directory of C:\Temp\moving\browser\20110425160318045 - Reset

4/26/2011  10:05 PM    <DIR>          .
4/26/2011  10:05 PM    <DIR>          ..
4/26/2011  04:58 PM             9,155 DeviceInfo.txt
4/26/2011  04:58 PM           764,383 Log.txt
              2 File(s)        773,538 bytes

Directory of C:\Temp\moving\browser\20110425211618662 - Reset

4/26/2011  10:05 PM    <DIR>          .
4/26/2011  10:05 PM    <DIR>          ..
4/26/2011  04:58 PM             8,248 DeviceInfo.txt
4/26/2011  04:58 PM           764,383 Log.txt
              2 File(s)        772,631 bytes

Directory of C:\Temp\moving\DirX

4/26/2011  09:33 PM    <DIR>          .
4/26/2011  09:33 PM    <DIR>          ..
              0 File(s)              0 bytes

    Total Files Listed:
              5 File(s)      1,546,461 bytes
             14 Dir(s)  149,501,550,592 bytes free

:\Temp\moving>

Open in new window

Author

Commented:
billprew: sorry!  I did not want to hard code the directory name in the batch file.

I tried your modification to subhachi's code.  But, it is failing on the
condition
if /I "%%a" NEQ "DirX" (

)

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
So, what do you want to specify to make this process work?  

Just C:\Temp\moving

or

C:\Temp\moving\browser
C:\Temp\moving\DivX

or

C:\Temp\moving\browser\20110425160318045 - Reset
C:\Temp\moving\browser\20110425211618662 - Reset
C:\Temp\moving\DivX

~bp

Author

Commented:
IIf I have my batch file in say C:\Temp\moving.  I want the batch file to go over all the sub directories under C:\Temp\moving to be processed.
That is C:\Temp\moving\browser\20110425160318045 - Reset
C:\Temp\moving\browser\20110425211618662 - Reset

and say if I also have another dir C:\Temp\moving\server\something - Reset, I want that one to be processed as well.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
So will the pattern for directories to be processed always be that they have " - reset" at the end of their name?

~bp

Author

Commented:
sorry, I am wasting your time.  But, I am trying to get this done for an urgent work.  

Author

Commented:
Yes, they will have - Reset at the end
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
And the file name to be moved is log.txt, not logs.txt, right?

I think it's pretty simple now that I better understand the specifics.

~bp

Author

Commented:
that is correct bill. It is Log.txt
Test your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Yes, it works.  Very much appreciated. I wish I can give 1000 points.  Thanks to you and subhaschi.  
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Welcome.

~bp
thanks Bill ..its always great to learn new things from you ..
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
oh well, looks like you sorted that while I sleep

amboli - if you had given us actual paths, how to identify them etc. would have been easier as we had no way of knowing when I posted what the eventual outcome was...

steve
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.