not so staright forward dos script query.....

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
amichaelglg
Posts: 14
Joined: 04 Mar 2011 04:43

not so staright forward dos script query.....

#1 Post by amichaelglg » 04 Mar 2011 04:56

I have googled for ths left and right and centre and cannot find anything.
Basically I need a script to search for the file ABCD which exists in various sub folders under folder c:\ANY_DIR*\run\ (and there are various ones).
Ths file, ABCD is be put into one specific folder, therefore this will be overwritten so I therefore want the folder name from 2 levels up to be part of the new file name in the new folder.

eg.
file c:\ANY_DIR1\run\ABCD to go to c:\newfolder\ANY_DIR1_ABCD
file c:\ANY_DIR2\run\ABCD to go to c:\newfolder\ANY_DIR2_ABCD
file c:\ANY_DIR\JOEY\run\ABCD to go to c:\newfolder\JOEY_ABCD
etc

Many thanks.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: not so staright forward dos script query.....

#2 Post by avery_larry » 04 Mar 2011 10:40

Do you mean c:\any_dir*\run OR do you mean c:\*\run?


if you meant c:\any_dir*\run\ABCD then:

Code: Select all

set dest=c:\newfolder
md "%dest%" >nul 2>nul
rem Start in the parent folder
cd /d "c:\"
for /d %%a in (any_dir*) do move "%%a\run\ABCD" "%dest%\%%a_ABCD" >nul 2>nul


if you meant c:\*\run\ABCD then:

Code: Select all

set dest=c:\newfolder
md "%dest%" >nul 2>nul
rem Start in the parent folder
cd /d "c:\"
for /d %%a in (*) do move "%%a\run\ABCD" "%dest%\%%a_ABCD" >nul 2>nul

amichaelglg
Posts: 14
Joined: 04 Mar 2011 04:43

Re: not so staright forward dos script query.....

#3 Post by amichaelglg » 04 Mar 2011 12:26

thanks, you make it look simple - like most things when you know how !

btw, I did mean the first example.

It works to a certain point, but.....

In my third example it is not picking/copying the file i,.e in the the case

file c:\ANY_DIR\JOEY\run\ABCD to go to c:\newfolder\JOEY_ABCD

It works for files in my first 2 examples i gave earlier. Reading back what I wrote in the original post I didn't explain that situation although I did give an example of it.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: not so staright forward dos script query.....

#4 Post by !k » 04 Mar 2011 12:52

Code: Select all

@echo off
setlocal enabledelayedexpansion
set "file=ABCD"
set "newfolder=c:\newfolder\"

md "%newfolder%" 2>nul
for /d %%d in (c:\any_dir*) do (
  cd /d "%%d"
  for /f "delims=" %%f in ('dir /b/s "%file%"') do (
    set "name=%%f"
    set "name=!name:%%~df\=!"
    set "name=!name:\run\=_!"
    set "name=!name:\=_!"
    move "%%f" "%newfolder%\!name!"
  )
)
Last edited by !k on 05 Mar 2011 08:51, edited 1 time in total.

amichaelglg
Posts: 14
Joined: 04 Mar 2011 04:43

Re: not so staright forward dos script query.....

#5 Post by amichaelglg » 04 Mar 2011 16:14

for some strange reason it's not working at all now ? and it seems to take a lot longer to run.

Also I can't see where it specifically only searches in directories:
c:\any_dir*\*\run\ (or c:\any_dir*\run\ ) for the file ABCD.
It sounds like it's searching the whole of the c drive and something is not going right as it's not even finding the ABCD file ?
Basically nothing is in the newly created folder.

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

Re: not so staright forward dos script query.....

#6 Post by aGerman » 05 Mar 2011 06:51

Try that:

Code: Select all

@echo off &setlocal
set "dest=C:\newfolder"

md "%dest%" 2>nul

pushd C:\
for /f "delims=" %%a in ('dir /ad /b "any_dir*"') do (
  for /f "delims=" %%b in ('dir /a-d /b /s "%%~fa\ABCD"') do (
    set "fullname=%%~b"
    call :proc "%%~dpb.."
  )
)
popd
goto :eof

:proc
move "%fullname%" "%dest%\%~nx1_ABCD"
goto :eof


Regards
aGerman

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: not so staright forward dos script query.....

#7 Post by !k » 05 Mar 2011 08:54


amichaelglg
Posts: 14
Joined: 04 Mar 2011 04:43

Re: not so staright forward dos script query.....

#8 Post by amichaelglg » 05 Mar 2011 10:58

aGerman appears to have cracked it.

!k revised version works to a certain degree, but in the example of file:
c:\ANY_DIR\JOEY\run\ABCD it was going to
c:\newfolder\ANY_DIR_JOEY_ABCD (i.e the 2 directory levels were concatenating when I really just wanted the 1 level below to be part of the new file name - i.e c:\newfolder\JOEY_ABCD).

Anyway thanks to aGerman who got their in the end, but especially to the others who gave it a damn good go.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: not so staright forward dos script query.....

#9 Post by !k » 05 Mar 2011 11:18

amichaelglg wrote:directory levels were concatenating
Yes, I didn't carefully read the query

Post Reply