Making dir from search result

March 16, 2011 at 03:22:49
Specs: Window 2003 Server
Hi All,

I am out of my wit already .. so really appreciate for all your help/comment and advice

Everyday , I will get alot of request to search and copy file in various servers.

so I come out with this script to search the files and write to dirlist.txt
problem now is sometimes ,there are more than 1 file with same name

for e.g. C:\test.txt
C:\tester\test.txt
and so on.
So When I copy the file using script, those files with same name overwrite each other.. and I end up with just 1 file.

Objective here is read the dir path from dirlist.txt then make dir according to dirlist and copy the file .

For e.g.
dirlist.txt contains as following
C:\test.txt
C:\tester\test.txt
C:\tester2\test.txt

then I want the batch job to read dirlist.txt and mkdir like following
C:\15March2011\
C:\15March2011\tester\
C:\15March2011\tester2\

and copy the file accordingly.

Hope you don't find this as long winded and ,again, appreciate all ur help in advanced !


See More: Making dir from search result

Report •


#1
March 16, 2011 at 15:23:33
out your existing script here, we will modify it to suite your needs..

Subhash Chandra.


Report •

#2
March 16, 2011 at 18:51:03
So far, my script is to read the location of file and copy the file according to the dirlist.txt one by one as below.
For making dir , I used the same script to get the location of file but I don't know how to cut out the file name to just make the dir .

For e.g. I can read C:\test2\test.txt from input file (filelist.txt ) but I need to make dir like C:\17March2011\test2\ from that line .
I tried like
mkdir C:\17March2011\%file1% but it returned error as there was file name behind

----------------

@echo on
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (dirlist.txt) do (
set /a N+=1
set v!N!=%%a
)
echo !v1!
set file1=!v1!
set file2=!v2!
set file3=!v3!
set file4=!v4!
set file5=!v5!
set file6=!v6!
set file7=!v7!
set file8=!v8!


copy %file1% C:\calvintester\
copy %file2% C:\calvintester\
copy %file3% C:\calvintester\
copy %file4% C:\calvintester\
copy %file5% C:\calvintester\
copy %file6% C:\calvintester\
copy %file7% C:\calvintester\
copy %file8% C:\calvintester\
pause


Report •

#3
March 16, 2011 at 20:33:20
I dont think the script is complete or may be something i wont understood how it works..However at the same place where you are getting the file path you can get the foldername as well..

see this..run from any command prompt.

for /f %a in ('dir /b /s') do echo %~pa

You will get the file path (Not the filename) you can put it anywhere in the batch where you want to know the directry path

Subhash Chandra.


Report •

Related Solutions

#4
March 16, 2011 at 20:50:27
Hi Chandra,

Thanks for the reply. I am using below script to retrieve the location of requested files first .I need to know where are those files located too ,so I can copy all the files.

My workflow is this way
Customer will give me text file with files list he wants, then I will use that text file as input file and search all the files , copy into backup dir and send back to requester.

-----------
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (ace_request.txt) do (
set /a N+=1
set v!N!=%%a
)
echo %!v1!%
set file1=!v1!
set file2=!v2!
set file3=!v3!
set file4=!v4!
set file5=!v5!
set file6=!v6!
set file7=!v7!
set file8=!v8!

cd C:\

dir /b /s "%file1%" > Files.txt
dir /b /s "%file2%" >> Files.txt
::dir /b /s "%file3%" >> Files.txt
::dir /b /s "%file4%" >> Files.txt
::dir /b /s "%file5%" >> Files.txt
::dir /b /s "%file6%" >> Files.txt
::dir /b /s "%file7%" >> Files.txt
::dir /b /s "%file8%" >> Files.txt


for /f %%a in (Files.txt) do copy %%a \Collect > nul

echo Done!
pause


Report •

#5
March 17, 2011 at 17:07:19
so, I guess the problemtic line is "for /f %%a in (Files.txt) do copy %%a \Collect > nul" It overwrites the files with same name.

I cannot simulate this scenario on my system but i guess below should help.Modify the code as below..

for /f %%a in (Files.txt) do (
if exist \collect\%%~nxa (
md newfoldername
copy copy %%a \Collect\newfoldername > nul
)
copy %%a \Collect > nul

)

Hopefully it will check if that destination file already present and create a new folder and copy the fle into new folder...If it works then we can further add other counters like the new folder name will be dynamic as per your choice etc.

Subhash Chandra.


Report •

#6
March 20, 2011 at 19:23:25
Hi Chandra,

Thanks for the reply.Collecting location of the file script is working fine, The only problem is when I read that list of file and try to copy into new location as below and it will overwrite if the file name is same since destination folder is same ..
So I hope to create the same set of folder according to the list generated by above script and copy to same location ,
for e.g. if system read the C:\Test\test.txt from Files.txt( input file ) then I want to create C:\backup\Test\ folder and copy the text.txt to that backup folder . Hope you understand what I am trying to say.Thanks!


@echo on
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (dirlist.txt) do (
set /a N+=1
set v!N!=%%a
)
echo !v1!
set file1=!v1!
set file2=!v2!
set file3=!v3!
set file4=!v4!
set file5=!v5!
set file6=!v6!
set file7=!v7!
set file8=!v8!


copy %file1% C:\calvintester\
copy %file2% C:\calvintester\
copy %file3% C:\calvintester\
copy %file4% C:\calvintester\
copy %file5% C:\calvintester\
copy %file6% C:\calvintester\
copy %file7% C:\calvintester\
copy %file8% C:\calvintester\
pause


Report •


Ask Question