# .bat to list a specific folder within servers



## xonewbxo (Nov 14, 2012)

Hi guys and thanks in advance for any help you can offer me.

I am trying to write a simple (i hope) script in order to scan a list of servers and output results to a csv file.

What I am trying to do is scan servers for all directories that have a .OLD filename, output the resultsin a csv file for me.
However having little or no experience in writing scripts this is all new to me.

After doing a bit of reading I think I need to have a .txt file with the server names listed as 
server1
server2 
server3
the .bat file to call this file from my C drive, scan the network and output the results to .csv format (sounds simple) haha I wish...

Really hope someone can help me out here.....
Thanks for looking


----------



## foxidrive (Oct 20, 2012)

xonewbxo said:


> What I am trying to do is scan servers for all directories that have a .OLD filename, output the resultsin a csv file for me.


With a list of \\server\sharename and assuming you have free access to the servers


```
@echo off
for /f "delims=" %%a in (servers.txt) do (
pushd "%%a"
echo searching server "%%a" ================== >> "filelist.txt"
dir \*.old /a-d /b /s >> "filelist.txt"
echo  ================================= >> "filelist.txt"
popd
)
```


----------



## foxidrive (Oct 20, 2012)

Are you looking to list folders or filenames? The subject says folders.


----------



## xonewbxo (Nov 14, 2012)

Hi foxidrive and thanks for your reply
I am looking to output a list of folders with a .OLD ext on them

Perhaps I should have been more specific, but looking to run the batch file from my own PC, call the txt file with the servers listed in it, scan through the list and output a txt or csv file on my PC with the results to look something like
server1 - abc.old, def.old
server2 - ab.old, cde.old
so on and so on.

Will give the above a bash though and see how I get on )


----------



## foxidrive (Oct 20, 2012)

This should help (untested). It assumes you have free access to all the servers and the servers.txt is in a format of 
\\server\share


```
@echo off
del "filelist.csv" 2>nul
for /f "delims=" %%a in (servers.txt) do (
 pushd "%%a"
 set /p "=%%a," >>"filelist.csv" <nul
    for /f "delims=" %%b in ('dir \*.old /ad /b /s') do (
    set /p "=%%b," >>"filelist.csv" <nul
  )
echo.>>"filelist.csv"
 popd
)
```


----------



## xonewbxo (Nov 14, 2012)

thanks for the update, getting a little further forward but not getting any output to the CSV file and ending up with mapped drives for each for the servers I have listed in the txt file.

It's only now I'm starting to see how much I've forgotten over the years.

Any further help is greatly appreciated... Am I needing to enter further instructions from the do ( ?


----------



## foxidrive (Oct 20, 2012)

I had a syntax error in the del line but that wouldn't have stopped it working.

It shouldn't leave mapped drives behind either, although it does map the drives with the pushd command.

Try this and see what appears on the screen, to see if you have permissions.

Does the DIR show each server in turn?


```
@echo off
for /f "delims=" %%a in (servers.txt) do (  
pushd "%%a" 
dir 
pause
popd
)
```


----------



## xonewbxo (Nov 14, 2012)

Yeah that's now giving the output of all the folders within the directory of the two servers I am testing with
permissions are okay - meant to say that earlier


----------



## foxidrive (Oct 20, 2012)

So what does it leave in the CSV file?
Just this?

server1\share,
server2\share,


----------



## foxidrive (Oct 20, 2012)

Did you leave off the trailing ) at the end in your copy/paste?


----------



## xonewbxo (Nov 14, 2012)

The CSV file is appearing empty 
and I still end up with drives mapping for the shares I am looking up
Seems almost like its there as the -
@echo off
for /f "delims=" %%a in (servers.txt) do ( 
pushd "%%a" 
dir 
pause
popd
)
The above does list all the information within the directory its scanning to the command prompt but on joining the two parts together I still get an empty CSV file and mapped drives..


----------



## xonewbxo (Nov 14, 2012)

ignore my last post please I am talking rubbish,
I am now getting the output predicted on each of the servers.
My next task is to try and have the results append on one csv file if at all possible?!

Many thanks for your input with this very much appreciated


----------



## foxidrive (Oct 20, 2012)

Ok. But it is designed to create one CSV file, with a line for each server.

What are you seeing?


----------



## xonewbxo (Nov 14, 2012)

I was getting a csv file being created on each share it's scanning with the \\server\share and the .OLD

I had it working for a minute but seem to have broken it again and now not able to proceed after the first server, the pause shows and then it hangs after that!

Would you be able to copy and paste into a reply how I should have the script complied as a whole please? 
Each time I edit it I seem to end up making it worse than before...


----------



## foxidrive (Oct 20, 2012)

Oh, I see that issue now. Try this - the second line is the location of the file


```
@echo off
set "file=c:\filelist.csv"

del "%file%" 2>nul
for /f "delims=" %%a in (servers.txt) do (
 pushd "%%a"
 set /p "=%%a," >>"%file%" <nul
    for /f "delims=" %%b in ('dir \*.old /ad /b /s') do (
    set /p "=%%b," >>"%file%" <nul
  )
 echo.>>"%file%"
 popd
)
```


----------



## xonewbxo (Nov 14, 2012)

cheers, still hitting errors though
I think I need to merge the following though with another part from above as whats below works - 
@echo off
for /f "delims=" %%a in (servers.txt) do ( 
pushd "%%a" 
dir 
pause
popd
)
The above displays the results in the command prompt however each time I am running through a list to output into a csv file there are errors 
So I guess if I was able to take the above and merge it with another part that was working previous as I was getting the results expected with the .OLD's appearing on a CSV however they were located on each server - that part I was happy but something has changed and I have broken it and unable to get back to that point again..

WIll leave for today though and come back with a fresh head tomorrow I think..


----------



## foxidrive (Oct 20, 2012)

What errors do you get?


----------



## xonewbxo (Nov 14, 2012)

Hi,
not errors as such, I am just having trouble getting the complete script to work as a whole, I can see results from the following below, which will display the results and create a file on the first server scanned however all the output is showing is \\server\share and there is no .OLD results showing (I had this working previous from another post) but cant seem to recreate it again!!
@echo off
for /f "delims=" %%a in (servers.txt) do ( 
pushd "%%a" 
dir 
pause
popd
)


----------



## foxidrive (Oct 20, 2012)

That script doesn't create anything. It just shows you a DIR of each server.

The last script I posted will create the file in c:\ called filelist.csv


----------



## xonewbxo (Nov 14, 2012)

sorry I'm not being very clear here, and now gone back to the start again, 
I will post below what i think is missing a line to delete the the CSV file on each server.

When I run whats posted below the CSV file is created with no result accept the \\server\share however once I delete the csv and disconnect the mapped drive it does display the results I am looking for (although being created on the server rather than C drive) I can come back to that though.
So I think whats below just needs a line added in to overwrite the csv file to output the .old results -

@echo off
for /f "delims=" %%a in (servers.txt) do (
pushd "%%a"
set /p "=%%a," >>"filelist.csv" <nul
for /f "delims=" %%b in ('dir \*.old /ad /b /s') do (
set /p "=%%b," >>"filelist.csv" <nul
)
echo.>>"filelist.csv"
popd
)


----------



## foxidrive (Oct 20, 2012)

You missed my update on the previous page...

http://forums.techguy.org/8525086-post15.html


----------



## xonewbxo (Nov 14, 2012)

ah darn it, 
foxidrive that's working grand now ) 
Although I am still having to disconnect the mapped drives which in turn then updates the csv file and appends the result (not to much of an issue)
But wonder if you would have the code in order to save me disconnecting each drive?
Thanks again for all your help


----------



## foxidrive (Oct 20, 2012)

The command POPD is supposed to unmap the drives. I can't see what you've done so I don't know how it can be happening. Does each server get mapped to a different drive letter?


----------



## xonewbxo (Nov 14, 2012)

No they are all using the Z drive, however I am looking to check through 23 servers in total so I may need to split this after all.
But the code I've used was just copied and pasted, reposted below though

@echo off
set "file=c:\temp1\testing\filelist.csv"

del "%file%" 2>nul
for /f "delims=" %%a in (servers.txt) do (
pushd "%%a"
set /p "=%%a," >>"%file%" <nul
for /f "delims=" %%b in ('dir \*.old /ad /b /s') do (
set /p "=%%b," >>"%file%" <nul
)
echo.>>"%file%"
popd
)


----------



## foxidrive (Oct 20, 2012)

xonewbxo said:


> No they are all using the Z drive


Oh, then it's just a history of network usage. They aren't actually still mapped to z:
As for 23 servers. it should handle them all just fine.

The comment that you made about them appending info to the file when you unmap then doesn't make any programming sense. Something is happening, apparently, but it is not related to a single instance of the batch file running.


----------



## xonewbxo (Nov 14, 2012)

yeah it's probably me and the way I am trying to explain...

So currently when I run the .bat file, \\server1\share\ maps to Z and creates .csv file on my C:\temp1 directory with the info \\server1\share - no .old data.
However when I disconnect the mapped Z drive I get \\server2\share mapping to it which then updates the .csv file with the .old data from \\server1\share.
The same process happening each time I disconnect the mapped drive. The .csv file updates with the previous servers .old data - all fine really. But it would be great to have the drives disconnecting automatically. 

I am starting to understand a bit more in the sense that Z is just being mapped temporarily each time in order to run the .bat file.

Does that make any sense? Just trying to explain it a bit clearer for ya.


----------



## foxidrive (Oct 20, 2012)

I think it is a matter of timing.

It temporarily maps a free drive letter and immediately prints the server name into the file. 
Then it does a full DIR on the entire server, after which it prints the .old folders.
This can take some minutes if the drive is large, or even longer.
After that it unmaps that drive and maps the next drive.

I think you are interrupting the process.


----------



## xonewbxo (Nov 14, 2012)

yeah I will give it a bash now with all the servers and see how I get on.
Really appreciate the help you've given..


----------

