Page 1 of 1

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

Posted: 04 Mar 2011 04:56
by amichaelglg
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.

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

Posted: 04 Mar 2011 10:40
by avery_larry
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

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

Posted: 04 Mar 2011 12:26
by amichaelglg
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.

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

Posted: 04 Mar 2011 12:52
by !k

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!"
  )
)

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

Posted: 04 Mar 2011 16:14
by amichaelglg
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.

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

Posted: 05 Mar 2011 06:51
by aGerman
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

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

Posted: 05 Mar 2011 08:54
by !k

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

Posted: 05 Mar 2011 10:58
by amichaelglg
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.

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

Posted: 05 Mar 2011 11:18
by !k
amichaelglg wrote:directory levels were concatenating
Yes, I didn't carefully read the query