• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

DOS batch : rename files in different directories

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.  
0
ambuli
Asked:
ambuli
  • 18
  • 11
  • 10
  • +2
2 Solutions
 
Steve KnightIT ConsultancyCommented:
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"
)
0
 
Bill PrewCommented:
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
0
 
subhashchyCommented:
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 ..
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
subhashchyCommented:
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

0
 
ambuliAuthor Commented:
Thanks a lot guys.  I am going away for a while.  Will come back and try it out.  Thanks again.
0
 
Steve KnightIT ConsultancyCommented:
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
0
 
Bill PrewCommented:
^^^ +1

~bp
0
 
ambuliAuthor Commented:
subhashchy:  What if I need to specify a sub folder than C:

Thanks.
0
 
subhashchyCommented:
as source of the files or as the target folder ?
0
 
ambuliAuthor 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
0
 
subhashchyCommented:
try this..

@echo off & setlocal enabledelayedexpansion
md C:\someDIr\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:\somedir\DirX\%%f.txt
cd..

)

cd..

)

Open in new window


Notice the changes on line 2 and 8 ..
Modify the actual dir name instead of somedir and put the batch file into C:\somedir .

C:\somedir = is the folder where all the subfolder DIRA,DIRB exists..
0
 
ambuliAuthor 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.
0
 
subhashchyCommented:
i think above code should work..did you tried that ?
0
 
ambuliAuthor 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.
0
 
ambuliAuthor Commented:
Sorry, I might have some problem. hold on I will get back to you.
0
 
subhashchyCommented:
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.)..




0
 
Bill PrewCommented:
Have you tried my code?  Worked here...

~bp
0
 
ambuliAuthor Commented:
I get syntax of the command is incorrect.
move Log.txt C:\Temp\moving\DirX\%%f.txt
0
 
ambuliAuthor Commented:
Hi billprew: I will try that in a while Thanks.
0
 
subhashchyCommented:
it seems to be working on my system..can you paste the actual batch you useed ?
0
 
ambuliAuthor 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 ..

)
0
 
ambuliAuthor 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

0
 
subhashchyCommented:
@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*"
0
 
Bill PrewCommented:
Looking...

~bp
0
 
ambuliAuthor 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

0
 
Bill PrewCommented:
@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
0
 
subhashchyCommented:
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 ..
0
 
lesterdsouzaCommented:
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...
0
 
Bill PrewCommented:
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
0
 
ambuliAuthor 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

0
 
ambuliAuthor 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" (

)

0
 
Bill PrewCommented:
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
0
 
ambuliAuthor 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.
0
 
Bill PrewCommented:
So will the pattern for directories to be processed always be that they have " - reset" at the end of their name?

~bp
0
 
ambuliAuthor Commented:
sorry, I am wasting your time.  But, I am trying to get this done for an urgent work.  
0
 
ambuliAuthor Commented:
Yes, they will have - Reset at the end
0
 
Bill PrewCommented:
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
0
 
ambuliAuthor Commented:
that is correct bill. It is Log.txt
0
 
Bill PrewCommented:
Okay, drop this in the moving folder and give it a go, it should do the job.

@echo off
set DestDir=%~p0DirX
if not exist "%DestDir%" md "%DestDir%"
for /F "tokens=*" %%D in ('dir /b /ad /s "* - reset"') do (
  for /F "tokens=1 delims=- " %%N in ("%%~nD") do (
    move "%%~D\log.txt" "%DestDir%\%%~N.txt"
  )
)

Open in new window

~bp
0
 
ambuliAuthor Commented:
Yes, it works.  Very much appreciated. I wish I can give 1000 points.  Thanks to you and subhaschi.  
0
 
Bill PrewCommented:
Welcome.

~bp
0
 
subhashchyCommented:
thanks Bill ..its always great to learn new things from you ..
0
 
Steve KnightIT ConsultancyCommented:
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
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 18
  • 11
  • 10
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now