Page 1 of 1
Batch file to change files location.
Posted: 23 Nov 2011 09:32
by steinere
Hi,
i need to move files from all subdirs to a main directory, and add the "path difference" to the file name of each file in there. means:
From each file like c:\User\Desktop\Main\first\second\x.txt to c:\User\Desktop\Main\first_second_x.txt, when I define c:\User\Desktop\Main as my main folder.
I've tried to run it with "for /r" but i couldn't get it right.
any ideas ?
thanks.
Re: Batch file to change files location.
Posted: 23 Nov 2011 10:51
by Exouxas
I'm gonna try writing a script for you, but first i could tell you the basic idea:
first you use for /f and split the directory string wherever \ is and use the part number 5 and 6 if its only in one directory, so it would make two values and set them to those two stings.
c:\User\Desktop\Main\first\second\x.txt
this would then output two values
val1=first
val2=second
then you copy the/move the file x.txt to:
c:\User\Desktop\Main\%val1%_%val2%_x.txt
EDIT:
noticed now that you typed path difference, haha, here's more:
Code: Select all
@echo off
cls
set Var=c:\User\Desktop\Main\
set #=%Var%
set dir=c:\User\Desktop\Main\first\second\x.txt
REM dir is the directory of the file FYI
set /a length=0
:loop
if defined # (set #=%#:~1%&set /A length += 1&goto loop)
echo %length%
pause
set /a count=0
:loop2
if %count%==%length% goto skip
if defined dir (set dir=%dir:~1%&set /a count=%count%+1&goto loop2)
:skip
echo %dir%
pause
REM this would give you first\second\x.txt
After this you just split, glue, repeat
Re: Batch file to change files location.
Posted: 23 Nov 2011 11:40
by dbenham
I've done some rudimentary testing with this script and it worked. But
BE CAREFUL You could do a lot of damage with this script.I would make sure I had a good backup of everything before attempting to use this.
The script requires one argument - the path you want to collapse. Make sure you enclose the path in parentheses if it contains any spaces or special characters.
The script also accepts an optional 2nd argument - the delimiter.
The delimiter defaults to _ as in your original request.
Code: Select all
@echo off
:collapseTree rootPath [delimiter]
setlocal disableDelayedExpansion
pushd "%~1" || exit /b
set "delim=%~2"
if not defined delim set "delim=_"
set "root=%cd%"
if "%root:~1%" neq "\" set "root=%root%\"
call :strlen root rootSize
for /r %%F in (*) do (
set "target=%%F"
setlocal enableDelayedExpansion
set "target=!target:~%rootSize%!"
set "target=!target:\=%delim%!"
move /y "%%F" ".\!target!" >nul
endlocal
)
popd
exit /b
:strLen string len -- returns the length of a string
:: -- string [in] - variable name containing the string being measured for length
:: -- len [out] - variable to be used to return the string length
:: Many thanks to 'sowgtsoi', but also 'jeb' and 'amel27' dostips forum users helped making this short and efficient
:$created 20081122 :$changed 20101116 :$categories StringOperation
:$source http://www.dostips.com
( SETLOCAL ENABLEDELAYEDEXPANSION
set "str=A!%~1!"&rem keep the A up front to ensure we get the length and not the upper bound
rem it also avoids trouble in case of empty string
set "len=0"
for /L %%A in (12,-1,0) do (
set /a "len|=1<<%%A"
for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
)
)
( ENDLOCAL & REM RETURN VALUES
IF "%~2" NEQ "" SET /a %~2=%len%
)
EXIT /b
The script could be modified to delete each folder after the files have been moved out.
Dave Benham
Re: Batch file to change files location.
Posted: 24 Nov 2011 12:32
by steinere
Thank you dbenham, it works great.
Exouxas, I haven't tried it yet, i will tonight.
Thanks both of you for your efforts.